mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 08:24:55 +00:00
- Added validation on todo creation to avoid creating todos with Every week repeat type, but no selected days
- Reworked and improved the creation of repeat todo
This commit is contained in:
@ -120,6 +120,10 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
|||||||
Alert.alert('Alert', 'Cannot have a todo without any assignees');
|
Alert.alert('Alert', 'Cannot have a todo without any assignees');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (todo?.repeatType === REPEAT_TYPE.EVERY_WEEK && todo?.repeatDays?.length === 0) {
|
||||||
|
Alert.alert('Alert', 'Please select a specific day for the Weekly recurrence');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import {useMutation, useQueryClient} from "react-query";
|
|||||||
import firestore from "@react-native-firebase/firestore";
|
import firestore from "@react-native-firebase/firestore";
|
||||||
import { useAuthContext } from "@/contexts/AuthContext";
|
import { useAuthContext } from "@/contexts/AuthContext";
|
||||||
import { DAYS_OF_WEEK_ENUM, IToDo, REPEAT_TYPE } from "@/hooks/firebase/types/todoData";
|
import { DAYS_OF_WEEK_ENUM, IToDo, REPEAT_TYPE } from "@/hooks/firebase/types/todoData";
|
||||||
import {addDays, addMonths, addWeeks, addYears, compareAsc, format, subDays} from "date-fns";
|
import { addMonths, addWeeks, addYears } from "date-fns";
|
||||||
|
|
||||||
export const daysOfWeek = [
|
export const daysOfWeek = [
|
||||||
DAYS_OF_WEEK_ENUM.MONDAY,
|
DAYS_OF_WEEK_ENUM.MONDAY,
|
||||||
@ -13,6 +13,66 @@ export const daysOfWeek = [
|
|||||||
DAYS_OF_WEEK_ENUM.SATURDAY,
|
DAYS_OF_WEEK_ENUM.SATURDAY,
|
||||||
DAYS_OF_WEEK_ENUM.SUNDAY];
|
DAYS_OF_WEEK_ENUM.SUNDAY];
|
||||||
|
|
||||||
|
const getNextDailyDates = (date) => {
|
||||||
|
const today = new Date();
|
||||||
|
const dates = [];
|
||||||
|
|
||||||
|
for (let weekOffset = 0; weekOffset < 2; weekOffset++) {
|
||||||
|
const targetDate = addWeeks(date, weekOffset);
|
||||||
|
if (targetDate > today) {
|
||||||
|
dates.push(targetDate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dates;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getNextWeeklyDates = (selectedDays, date) => {
|
||||||
|
const today = new Date();
|
||||||
|
const currentDay = date.getDay();
|
||||||
|
const dates = [];
|
||||||
|
|
||||||
|
for (let week = 0; week < 2; week++) {
|
||||||
|
selectedDays.forEach((day) => {
|
||||||
|
const targetDay = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'].indexOf(day);
|
||||||
|
const diff = (targetDay - currentDay + 7) % 7 + week * 7; // Move to the next week
|
||||||
|
const nextDate = new Date(today);
|
||||||
|
nextDate.setDate(date.getDate() + diff);
|
||||||
|
dates.push(nextDate);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return dates;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getNextMonthlyDates = (date) => {
|
||||||
|
const today = new Date();
|
||||||
|
const dates = [];
|
||||||
|
|
||||||
|
for (let monthOffset = 0; monthOffset < 12; monthOffset++) {
|
||||||
|
const targetDate = addMonths(date, monthOffset);
|
||||||
|
if (targetDate > today) {
|
||||||
|
dates.push(targetDate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dates;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getNextYearlyDates = (date) => {
|
||||||
|
const today = new Date();
|
||||||
|
const dates = [];
|
||||||
|
|
||||||
|
for (let yearOffset = 0; yearOffset < 5; yearOffset++) {
|
||||||
|
const nextYear = addYears(date, yearOffset);
|
||||||
|
if (nextYear > today) {
|
||||||
|
dates.push(nextYear);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dates;
|
||||||
|
};
|
||||||
|
|
||||||
export const useCreateTodo = () => {
|
export const useCreateTodo = () => {
|
||||||
const { user: currentUser, profileData } = useAuthContext();
|
const { user: currentUser, profileData } = useAuthContext();
|
||||||
const queryClients = useQueryClient();
|
const queryClients = useQueryClient();
|
||||||
@ -28,93 +88,50 @@ export const useCreateTodo = () => {
|
|||||||
.collection("Todos")
|
.collection("Todos")
|
||||||
.add(originalTodo);
|
.add(originalTodo);
|
||||||
} else {
|
} else {
|
||||||
// Create the one original to do
|
|
||||||
const newDoc = firestore().collection('Todos').doc();
|
|
||||||
let originalTodo = {...todoData, id: newDoc.id, familyId: profileData?.familyId, creatorId: currentUser?.uid, connectedTodoId: newDoc.id};
|
|
||||||
|
|
||||||
originalTodo = resolveTodoAlternatingAssignees(todoData, originalTodo, 0);
|
let nextDates;
|
||||||
|
if (todoData.repeatType === REPEAT_TYPE.DAILY) {
|
||||||
|
nextDates = getNextDailyDates(todoData.date);
|
||||||
|
} else if (todoData.repeatType === REPEAT_TYPE.EVERY_WEEK) {
|
||||||
|
nextDates = getNextWeeklyDates(todoData.repeatDays, todoData.date);
|
||||||
|
} else if (todoData.repeatType === REPEAT_TYPE.ONCE_A_MONTH) {
|
||||||
|
nextDates = getNextMonthlyDates(todoData.date);
|
||||||
|
} else if (todoData.repeatType === REPEAT_TYPE.ONCE_A_YEAR) {
|
||||||
|
nextDates = getNextYearlyDates(todoData.date);
|
||||||
|
}
|
||||||
|
|
||||||
await firestore()
|
const todosRef = firestore().collection("Todos");
|
||||||
.collection("Todos")
|
const ruleDocRef = todosRef.doc();
|
||||||
.add(originalTodo);
|
const ruleDoc = await todosRef.add(
|
||||||
|
{...todoData,
|
||||||
|
id: ruleDocRef.id,
|
||||||
|
familyId: profileData?.familyId,
|
||||||
|
creatorId: currentUser?.uid,
|
||||||
|
connectedTodoId: ruleDocRef.id,}
|
||||||
|
);
|
||||||
|
|
||||||
const batch = firestore().batch();
|
const batch = firestore().batch();
|
||||||
|
nextDates.forEach((date, index) => {
|
||||||
|
const newDocRef = todosRef.doc();
|
||||||
|
|
||||||
if (todoData.repeatType === REPEAT_TYPE.DAILY) {
|
let assignee;
|
||||||
// for the next 52 weeks
|
if (todoData.assignees && todoData.rotate && todoData?.assignees?.length !== 0) {
|
||||||
for (let i = 1; i < 52; i++) {
|
assignee = todoData.assignees[index % todoData.assignees.length];
|
||||||
let date = originalTodo.date;
|
|
||||||
const nextWeek = addWeeks(date, i);
|
|
||||||
|
|
||||||
let docRef = firestore().collection("Todos").doc();
|
|
||||||
let newTodo = { ...originalTodo, id: docRef.id, date: nextWeek, connectedTodoId: newDoc.id };
|
|
||||||
newTodo = resolveTodoAlternatingAssignees(todoData, newTodo, i);
|
|
||||||
|
|
||||||
batch.set(docRef, newTodo);
|
|
||||||
}
|
}
|
||||||
} else if (todoData.repeatType === REPEAT_TYPE.EVERY_WEEK) {
|
|
||||||
|
|
||||||
let date = originalTodo.date;
|
const nextTodo = {
|
||||||
let repeatDays = originalTodo.repeatDays;
|
...todoData,
|
||||||
const dates = [];
|
date: date,
|
||||||
|
id: newDocRef.id,
|
||||||
const originalDateDay = format(date, 'EEEE');
|
familyId: profileData?.familyId,
|
||||||
const originalNumber = daysOfWeek.indexOf(originalDateDay);
|
creatorId: currentUser?.uid,
|
||||||
repeatDays?.forEach((day) => {
|
connectedTodoId: ruleDocRef.id,
|
||||||
let number = daysOfWeek.indexOf(day);
|
assignees: assignee ? [assignee] : null
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
let index = 1;
|
batch.set(newDocRef, nextTodo)
|
||||||
for (let i = 0; i < 52; i++) {
|
|
||||||
dates?.forEach((dateToAdd) => {
|
|
||||||
index ++;
|
|
||||||
let newTodoDate = addWeeks(dateToAdd, i);
|
|
||||||
if (compareAsc(newTodoDate, originalTodo.date) !== 0) {
|
|
||||||
|
|
||||||
let docRef = firestore().collection("Todos").doc();
|
|
||||||
let newTodo = { ...originalTodo, id: docRef.id, date: newTodoDate, connectedTodoId: newDoc.id };
|
|
||||||
newTodo = resolveTodoAlternatingAssignees(todoData, newTodo, index);
|
|
||||||
|
|
||||||
batch.set(docRef, newTodo);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else if (todoData.repeatType === REPEAT_TYPE.ONCE_A_MONTH) {
|
|
||||||
|
|
||||||
// for the next 12 months
|
|
||||||
for (let i = 1; i < 12; i++) {
|
|
||||||
let date = originalTodo.date;
|
|
||||||
const nextMonth = addMonths(date, i);
|
|
||||||
|
|
||||||
let docRef = firestore().collection("Todos").doc();
|
|
||||||
let newTodo = { ...originalTodo, id: docRef.id, date: nextMonth, connectedTodoId: newDoc.id };
|
|
||||||
newTodo = resolveTodoAlternatingAssignees(todoData, newTodo, i);
|
|
||||||
|
|
||||||
batch.set(docRef, newTodo);
|
|
||||||
}
|
|
||||||
} else if (todoData.repeatType === REPEAT_TYPE.ONCE_A_YEAR) {
|
|
||||||
|
|
||||||
// for the next 5 years
|
|
||||||
for (let i = 1; i < 5; i++) {
|
|
||||||
let date = originalTodo.date;
|
|
||||||
const nextMonth = addYears(date, i);
|
|
||||||
|
|
||||||
let docRef = firestore().collection("Todos").doc();
|
|
||||||
let newTodo = { ...originalTodo, id: docRef.id, date: nextMonth, connectedTodoId: newDoc.id };
|
|
||||||
newTodo = resolveTodoAlternatingAssignees(todoData, newTodo, i);
|
|
||||||
|
|
||||||
batch.set(docRef, newTodo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
await batch.commit();
|
await batch.commit();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user