mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 00:24:53 +00:00
- Implemented spending of the saved points
This commit is contained in:
@ -140,7 +140,7 @@ export const useUpdateTodo = () => {
|
||||
Sunday: 0,
|
||||
};
|
||||
|
||||
const currentDay = getCurrentDay(todoUpdate.date);
|
||||
const currentDay = getSelectedDateDay(todoUpdate.date);
|
||||
const updatedPointsPerDay = todoData.done
|
||||
? pointsPerDay[currentDay] + todoUpdate.points
|
||||
: pointsPerDay[currentDay] - todoUpdate.points;
|
||||
@ -185,7 +185,7 @@ export const useUpdateTodo = () => {
|
||||
})
|
||||
};
|
||||
|
||||
const getCurrentDay = (date) => {
|
||||
const getSelectedDateDay = (date) => {
|
||||
let selectedDate = new Date(date.seconds * 1000);
|
||||
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
||||
return days[selectedDate.getDay()];
|
||||
|
||||
@ -3,6 +3,7 @@ import {FirebaseAuthTypes} from "@react-native-firebase/auth";
|
||||
import {useMutation, useQueryClient} from "@tanstack/react-query";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import {UserProfile} from "@/hooks/firebase/types/profileTypes";
|
||||
import {Alert} from "react-native";
|
||||
|
||||
export const useUpdateUserData = () => {
|
||||
const {user: currentUser, refreshProfileData} = useAuthContext();
|
||||
@ -54,4 +55,28 @@ export const useUpdateUserData = () => {
|
||||
queryClient.invalidateQueries({queryKey: ["events"]})
|
||||
},
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
export const useSpendPoints = () => {
|
||||
const { user: currentUser } = useAuthContext();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["spendPoints"],
|
||||
mutationFn: async (pointsToSpend: number) => {
|
||||
const userRef = firestore().collection("Profiles").doc(currentUser?.uid);
|
||||
let userSnapshot = await userRef.get();
|
||||
let userData = userSnapshot.data() as UserProfile;
|
||||
|
||||
if (!userData) return;
|
||||
|
||||
const allTimePoints = userData.allTimePoints || 0;
|
||||
if (allTimePoints < pointsToSpend) {
|
||||
return;
|
||||
}
|
||||
|
||||
await userRef.update({...userData,allTimePoints: allTimePoints - pointsToSpend,})
|
||||
|
||||
Alert.alert("Success", `You spent ${pointsToSpend} points!`);
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user