Skip to content

Commit

Permalink
ensured new rows are created
Browse files Browse the repository at this point in the history
  • Loading branch information
NancyAanchal committed Jul 18, 2024
1 parent b51340d commit d7db44d
Showing 1 changed file with 39 additions and 34 deletions.
73 changes: 39 additions & 34 deletions nepalingo-web/src/components/StreakContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null>(null);
const [streakEndDate, setStreakEndDate] = useState<string | null>(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);
}
}
};
Expand All @@ -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);
}
}
};
Expand All @@ -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;
};

0 comments on commit d7db44d

Please sign in to comment.