From d7db44dc0fe0b585c358efb58c5aa4c136836000 Mon Sep 17 00:00:00 2001 From: NancyAanchal Date: Thu, 18 Jul 2024 10:01:22 +0545 Subject: [PATCH] ensured new rows are created --- .../src/components/StreakContext.tsx | 73 ++++++++++--------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/nepalingo-web/src/components/StreakContext.tsx b/nepalingo-web/src/components/StreakContext.tsx index 5b1f18a..b41c149 100644 --- a/nepalingo-web/src/components/StreakContext.tsx +++ b/nepalingo-web/src/components/StreakContext.tsx @@ -24,25 +24,24 @@ export const StreakProvider = ({ children }: { children: ReactNode }) => { const { user } = useAuth(); const [currentStreak, setCurrentStreak] = useState(0); const [longestStreak, setLongestStreak] = useState(0); - const [lastUpdateDate, setLastUpdateDate] = useState(null); + const [streakEndDate, setStreakEndDate] = useState(null); const fetchStreakData = async () => { if (user) { const { data, error } = await supabase .from("user_daily_streaks") .select("*") - .eq("user_id", user.id); + .eq("user_id", user.id) + .single(); - if (error) { + if (error && error.code !== "PGRST116") { console.error("Error fetching streak data:", error); - return; } - if (data && data.length > 0) { - const streakData = data[0]; - setCurrentStreak(streakData.current_streak); - setLongestStreak(streakData.longest_streak); - setLastUpdateDate(streakData.streak_end_date); + if (data) { + setCurrentStreak(data.current_streak); + setLongestStreak(data.longest_streak); + setStreakEndDate(data.streak_end_date); } } }; @@ -55,38 +54,42 @@ export const StreakProvider = ({ children }: { children: ReactNode }) => { const currentDate = new Date().toISOString().split("T")[0]; // Get current date if (user) { - // Call Supabase function to update streak - const { data } = await supabase.rpc("update_streak", { - user_id: user.id, - }); - - if (data && currentDate !== lastUpdateDate) { - // Check if last update date is different from current date - const newStreak = - lastUpdateDate === null || lastUpdateDate !== currentDate - ? 1 // Reset streak if last update was not today - : currentStreak + 1; // Increment streak - - const newLongestStreak = Math.max(newStreak, longestStreak); + let newStreak = 1; + let newLongestStreak = 1; + + if (streakEndDate) { + const lastDate = new Date(streakEndDate).toISOString().split("T")[0]; + if (lastDate === currentDate) { + return; // No update needed if the user has already visited today + } else if ( + new Date(currentDate).getTime() - new Date(lastDate).getTime() === + 86400000 + ) { + newStreak = currentStreak + 1; + newLongestStreak = Math.max(newStreak, longestStreak); + } + } - // Update local state - setCurrentStreak(newStreak); - setLongestStreak(newLongestStreak); - setLastUpdateDate(currentDate); + // Update state + setCurrentStreak(newStreak); + setLongestStreak(newLongestStreak); + setStreakEndDate(currentDate); - // Update database - const { error } = await supabase.from("user_daily_streaks").upsert({ + // Upsert database + const { error } = await supabase + .from("user_daily_streaks") + .upsert({ user_id: user.id, streak_start_date: newStreak === 1 ? currentDate : undefined, - streak_end_date: currentDate, + streak_end_date: currentDate, // Update streak_end_date with currentDate current_streak: newStreak, longest_streak: newLongestStreak, created_at: new Date().toISOString(), - }); + }) + .select(); - if (error) { - console.error("Error updating streak data:", error); - } + if (error) { + console.error("Error updating streak data:", error); } } }; @@ -102,6 +105,8 @@ export const StreakProvider = ({ children }: { children: ReactNode }) => { export const useStreak = () => { const context = useContext(StreakContext); - + if (context === undefined) { + throw new Error("useStreak must be used within a StreakProvider"); + } return context; };