mirror of
https://github.com/urosran/cally.git
synced 2025-07-15 09:45:20 +00:00
- Implemented creation of the repeatable todos for every repeat type
This commit is contained in:
@ -1,7 +1,17 @@
|
||||
import { useMutation, useQueryClient } from "react-query";
|
||||
import {useMutation, useQueryClient} from "react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import { useAuthContext } from "@/contexts/AuthContext";
|
||||
import { IToDo } from "@/hooks/firebase/types/todoData";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import {DAYS_OF_WEEK_ENUM, IToDo, REPEAT_TYPE} from "@/hooks/firebase/types/todoData";
|
||||
import {addDays, addMonths, addWeeks, compareAsc, format, getDay, subDays} from "date-fns";
|
||||
|
||||
const daysOfWeek = [
|
||||
DAYS_OF_WEEK_ENUM.MONDAY,
|
||||
DAYS_OF_WEEK_ENUM.TUESDAY,
|
||||
DAYS_OF_WEEK_ENUM.WEDNESDAY,
|
||||
DAYS_OF_WEEK_ENUM.THURSDAY,
|
||||
DAYS_OF_WEEK_ENUM.FRIDAY,
|
||||
DAYS_OF_WEEK_ENUM.SATURDAY,
|
||||
DAYS_OF_WEEK_ENUM.SUNDAY];
|
||||
|
||||
export const useCreateTodo = () => {
|
||||
const { user: currentUser, profileData } = useAuthContext();
|
||||
@ -11,10 +21,78 @@ export const useCreateTodo = () => {
|
||||
mutationKey: ["createTodo"],
|
||||
mutationFn: async (todoData: Partial<IToDo>) => {
|
||||
try {
|
||||
// Create the one original to do
|
||||
const newDoc = firestore().collection('Todos').doc();
|
||||
let originalTodo = {...todoData, id: newDoc.id, familyId: profileData?.familyId, creatorId: currentUser?.uid}
|
||||
await firestore()
|
||||
.collection("Todos")
|
||||
.add({...todoData, id: newDoc.id, familyId: profileData?.familyId, creatorId: currentUser?.uid})
|
||||
.add(originalTodo);
|
||||
|
||||
if (todoData.repeatType !== REPEAT_TYPE.NONE) {
|
||||
const batch = firestore().batch();
|
||||
|
||||
if (todoData.repeatType === REPEAT_TYPE.EVERY_WEEK) {
|
||||
|
||||
let date = originalTodo.date;
|
||||
let repeatDays = originalTodo.repeatDays;
|
||||
const dates = [];
|
||||
|
||||
const originalDateDay = format(date, 'EEEE');
|
||||
const originalNumber = daysOfWeek.indexOf(originalDateDay);
|
||||
repeatDays?.forEach((day) => {
|
||||
let number = daysOfWeek.indexOf(day);
|
||||
let newDate;
|
||||
if (originalNumber > number) {
|
||||
let diff = originalNumber - number;
|
||||
newDate = subDays(date, diff);
|
||||
} else {
|
||||
let diff = number - originalNumber;
|
||||
newDate = addDays(date, diff);
|
||||
}
|
||||
dates.push(newDate);
|
||||
});
|
||||
|
||||
// TODO: for the next 52 weeks
|
||||
for (let i = 0; i < 4; i++) {
|
||||
dates?.forEach((dateToAdd) => {
|
||||
let newTodoDate = addWeeks(dateToAdd, i);
|
||||
if (compareAsc(newTodoDate, originalTodo.date) !== 0) {
|
||||
const newTodo = { ...originalTodo, date: newTodoDate, connectedTodoId: newDoc.id };
|
||||
|
||||
let docRef = firestore().collection("Todos").doc();
|
||||
batch.set(docRef, newTodo);
|
||||
console.log(newTodo);
|
||||
}
|
||||
})
|
||||
}
|
||||
} else if (todoData.repeatType === REPEAT_TYPE.ONCE_A_MONTH) {
|
||||
|
||||
// for the next 12 months
|
||||
for (let i = 0; i < 12; i++) {
|
||||
let date = originalTodo.date;
|
||||
const nextMonth = addMonths(date, i + 1);
|
||||
const newTodo = { ...originalTodo, date: nextMonth, connectedTodoId: newDoc.id };
|
||||
console.log(newTodo);
|
||||
|
||||
let docRef = firestore().collection("Todos").doc();
|
||||
batch.set(docRef, newTodo);
|
||||
}
|
||||
} else if (todoData.repeatType === REPEAT_TYPE.ONCE_A_YEAR) {
|
||||
|
||||
// for the next 5 years
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let date = originalTodo.date;
|
||||
const nextMonth = addMonths(date, i + 1);
|
||||
const newTodo = { ...originalTodo, date: nextMonth, connectedTodoId: newDoc.id };
|
||||
console.log(newTodo);
|
||||
|
||||
let docRef = firestore().collection("Todos").doc();
|
||||
batch.set(docRef, newTodo);
|
||||
}
|
||||
}
|
||||
|
||||
await batch.commit();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
|
Reference in New Issue
Block a user