Skip to content

Commit

Permalink
made sure the database table is updated and fetched correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
NancyAanchal committed Jul 17, 2024
1 parent d5faf5f commit 83a1b6e
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions nepalingo-web/src/components/StreakContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,32 @@ export const StreakProvider = ({ children }: { children: ReactNode }) => {
user_id: user.id,
});

if (data) {
if (data && currentDate !== lastUpdateDate) {
// Check if last update date is different from current date
if (lastUpdateDate !== currentDate) {
// Calculate new streak
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);

// Update local state
setCurrentStreak(newStreak);
setLongestStreak(newLongestStreak);
setLastUpdateDate(currentDate);

// Update 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,
current_streak: newStreak,
longest_streak: newLongestStreak,
created_at: new Date().toISOString(),
});

if (error) {
console.error("Error updating streak data:", error);
}
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);

// Update local state
setCurrentStreak(newStreak);
setLongestStreak(newLongestStreak);
setLastUpdateDate(currentDate);

// Update 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,
current_streak: newStreak,
longest_streak: newLongestStreak,
created_at: new Date().toISOString(),
});

if (error) {
console.error("Error updating streak data:", error);
}
}
}
Expand Down

0 comments on commit 83a1b6e

Please sign in to comment.