- Implemented saving of points per week per day for user

- Implemented saving of the number of completed todos per user
- Changed "To do's" labels to "To Do"
This commit is contained in:
Dejan
2024-12-25 22:41:53 +01:00
parent 406f541163
commit c14910447e
7 changed files with 38 additions and 9 deletions

View File

@ -130,16 +130,37 @@ export const useUpdateTodo = () => {
? userWeeklyPoints + todoUpdate.points
: userWeeklyPoints - todoUpdate.points;
let pointsPerDay = userData.weeklyDayPoints || {
Monday: 0,
Tuesday: 0,
Wednesday: 0,
Thursday: 0,
Friday: 0,
Saturday: 0,
Sunday: 0,
};
const currentDay = getCurrentDay();
const updatedPointsPerDay = todoData.done
? pointsPerDay[currentDay] + todoUpdate.points
: pointsPerDay[currentDay] - todoUpdate.points;
pointsPerDay[currentDay] = Math.max(0, updatedPointsPerDay);
let userAllTimePoints = userData.allTimePoints ?? 0;
const allTimePoints = todoData.done
? userAllTimePoints + todoUpdate.points
: userAllTimePoints - todoUpdate.points;
const weeklyCompletedTodos = todoData.done
? (userData.weeklyCompletedTodos || 0) + 1
: (userData.weeklyCompletedTodos || 0) - 1;
// Update the user's points in Firestore
userData = {
...userData,
weeklyPoints: weeklyPoints >= 0 ? weeklyPoints : 0,
allTimePoints: allTimePoints >= 0 ? allTimePoints : 0
weeklyDayPoints: updatedPointsPerDay,
allTimePoints: allTimePoints >= 0 ? allTimePoints : 0,
weeklyCompletedTodos: weeklyCompletedTodos >= 0 ? weeklyCompletedTodos : 0
}
userBatch.update(userRef, userData);
});
@ -161,4 +182,10 @@ export const useUpdateTodo = () => {
queryClients.invalidateQueries({queryKey: ["todos"]})
}
})
};
const getCurrentDay = () => {
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const now = new Date();
return days[now.getDay()];
};