mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 16:34:54 +00:00
- Fixed an issue with the Tod dialog
- Implementation of update todo and adding new days in the rule for a repeatable todo
This commit is contained in:
@ -31,7 +31,7 @@ const AddChore = () => {
|
|||||||
</Text>
|
</Text>
|
||||||
</Button>
|
</Button>
|
||||||
</View>
|
</View>
|
||||||
<AddChoreDialog isVisible={isVisible} setIsVisible={setIsVisible} />
|
{isVisible && <AddChoreDialog isVisible={isVisible} setIsVisible={setIsVisible} />}
|
||||||
</LinearGradient>
|
</LinearGradient>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -36,6 +36,7 @@ const defaultTodo = {
|
|||||||
rotate: false,
|
rotate: false,
|
||||||
repeatType: "Every week",
|
repeatType: "Every week",
|
||||||
assignees: [],
|
assignees: [],
|
||||||
|
repeatDays: []
|
||||||
};
|
};
|
||||||
|
|
||||||
const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
||||||
@ -48,14 +49,12 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
|||||||
);
|
);
|
||||||
const { width, height } = Dimensions.get("screen");
|
const { width, height } = Dimensions.get("screen");
|
||||||
const [points, setPoints] = useState<number>(todo.points);
|
const [points, setPoints] = useState<number>(todo.points);
|
||||||
const [selectedDays, setSelectedDays] = useState([]);
|
|
||||||
|
|
||||||
const { data: members } = useGetFamilyMembers();
|
const { data: members } = useGetFamilyMembers();
|
||||||
|
|
||||||
const handleClose = () => {
|
const handleClose = () => {
|
||||||
setTodo(defaultTodo);
|
setTodo(defaultTodo);
|
||||||
setSelectedAssignees([]);
|
setSelectedAssignees([]);
|
||||||
setSelectedDays([]);
|
|
||||||
addChoreDialogProps.setIsVisible(false);
|
addChoreDialogProps.setIsVisible(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -71,12 +70,20 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
|||||||
|
|
||||||
const handleRepeatDaysChange = (day: string, set: boolean) => {
|
const handleRepeatDaysChange = (day: string, set: boolean) => {
|
||||||
if (set) {
|
if (set) {
|
||||||
setSelectedDays((prevState) => [...prevState, day]);
|
const updatedTodo = {
|
||||||
|
...todo,
|
||||||
|
repeatDays: [...todo.repeatDays, day]
|
||||||
|
}
|
||||||
|
setTodo(updatedTodo);
|
||||||
} else {
|
} else {
|
||||||
const array = selectedDays;
|
const array = todo.repeatDays ?? [];
|
||||||
let index = array.indexOf(day);
|
let index = array.indexOf(day);
|
||||||
array.splice(index, 1);
|
array.splice(index, 1);
|
||||||
setSelectedDays(array);
|
const updatedTodo = {
|
||||||
|
...todo,
|
||||||
|
repeatDays: array
|
||||||
|
}
|
||||||
|
setTodo(updatedTodo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,8 +130,7 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
|||||||
updateToDo({
|
updateToDo({
|
||||||
...todo,
|
...todo,
|
||||||
points: points,
|
points: points,
|
||||||
assignees: selectedAssignees,
|
assignees: selectedAssignees
|
||||||
repeatDays: selectedDays,
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
addToDo({
|
addToDo({
|
||||||
@ -132,7 +138,7 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
|||||||
done: false,
|
done: false,
|
||||||
points: points,
|
points: points,
|
||||||
assignees: selectedAssignees,
|
assignees: selectedAssignees,
|
||||||
repeatDays: selectedDays
|
repeatDays: todo.repeatDays ?? []
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
handleClose();
|
handleClose();
|
||||||
|
|||||||
@ -26,11 +26,14 @@ export default RepeatFreq;
|
|||||||
const RepeatOption = ({ value, handleRepeatDaysChange, repeatDays }: { value: string, handleRepeatDaysChange: Function, repeatDays: string[] }) => {
|
const RepeatOption = ({ value, handleRepeatDaysChange, repeatDays }: { value: string, handleRepeatDaysChange: Function, repeatDays: string[] }) => {
|
||||||
const [isSet, setisSet] = useState(repeatDays.includes(value));
|
const [isSet, setisSet] = useState(repeatDays.includes(value));
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
handleRepeatDaysChange(value, isSet);
|
const handleDayChange = () => {
|
||||||
}, [isSet])
|
handleRepeatDaysChange(value, !isSet)
|
||||||
|
setisSet(!isSet);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TouchableOpacity onPress={() => setisSet(!isSet)}>
|
<TouchableOpacity onPress={handleDayChange}>
|
||||||
<View
|
<View
|
||||||
center
|
center
|
||||||
marginT-8
|
marginT-8
|
||||||
|
|||||||
@ -4,7 +4,7 @@ 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, compareAsc, format, getDay, subDays} from "date-fns";
|
import {addDays, addMonths, addWeeks, compareAsc, format, getDay, subDays} from "date-fns";
|
||||||
|
|
||||||
const daysOfWeek = [
|
export const daysOfWeek = [
|
||||||
DAYS_OF_WEEK_ENUM.MONDAY,
|
DAYS_OF_WEEK_ENUM.MONDAY,
|
||||||
DAYS_OF_WEEK_ENUM.TUESDAY,
|
DAYS_OF_WEEK_ENUM.TUESDAY,
|
||||||
DAYS_OF_WEEK_ENUM.WEDNESDAY,
|
DAYS_OF_WEEK_ENUM.WEDNESDAY,
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import { useQuery } from "react-query";
|
import { useQuery } 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 {UserProfile} from "@/hooks/firebase/types/profileTypes";
|
|
||||||
import {IToDo} from "@/hooks/firebase/types/todoData";
|
import {IToDo} from "@/hooks/firebase/types/todoData";
|
||||||
|
|
||||||
export const useGetTodos = () => {
|
export const useGetTodos = () => {
|
||||||
@ -23,6 +22,7 @@ export const useGetTodos = () => {
|
|||||||
...data,
|
...data,
|
||||||
id: doc.id,
|
id: doc.id,
|
||||||
date: data.date ? new Date(data.date.seconds * 1000) : null,
|
date: data.date ? new Date(data.date.seconds * 1000) : null,
|
||||||
|
repeatDays: data.repeatDays ?? []
|
||||||
};
|
};
|
||||||
}) as IToDo[];
|
}) as IToDo[];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
import { useMutation, useQueryClient } from "react-query";
|
import { useMutation, useQueryClient } from "react-query";
|
||||||
import firestore from "@react-native-firebase/firestore";
|
import firestore from "@react-native-firebase/firestore";
|
||||||
import { IToDo } from "@/hooks/firebase/types/todoData";
|
import {IToDo} from "@/hooks/firebase/types/todoData";
|
||||||
|
import {addDays, addWeeks, compareAsc, format, subDays} from "date-fns";
|
||||||
|
import {daysOfWeek} from "@/hooks/firebase/useCreateTodo";
|
||||||
|
|
||||||
export const useUpdateTodo = () => {
|
export const useUpdateTodo = () => {
|
||||||
const queryClients = useQueryClient()
|
const queryClients = useQueryClient()
|
||||||
@ -9,10 +11,94 @@ export const useUpdateTodo = () => {
|
|||||||
mutationKey: ["updateTodo"],
|
mutationKey: ["updateTodo"],
|
||||||
mutationFn: async (todoData: Partial<IToDo>) => {
|
mutationFn: async (todoData: Partial<IToDo>) => {
|
||||||
try {
|
try {
|
||||||
|
if (todoData.connectedTodoId) {
|
||||||
|
console.log("CONNECTED TODO");
|
||||||
|
const snapshot = await firestore()
|
||||||
|
.collection("Todos")
|
||||||
|
.where("connectedTodoId", "==", todoData.connectedTodoId)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
|
||||||
|
const connectedTodos = snapshot.docs.map((doc) => {
|
||||||
|
const data = doc.data();
|
||||||
|
|
||||||
|
return {
|
||||||
|
...data,
|
||||||
|
id: doc.id,
|
||||||
|
date: data.date ? new Date(data.date.seconds * 1000) : null,
|
||||||
|
ref: doc.ref
|
||||||
|
};
|
||||||
|
}) as IToDo[];
|
||||||
|
|
||||||
|
let filteredTodos = connectedTodos?.filter((item) => item.date >= todoData.date).sort((a,b) =>{
|
||||||
|
return b.date?.getSeconds() - a.date?.getSeconds();
|
||||||
|
});
|
||||||
|
console.log(filteredTodos);
|
||||||
|
|
||||||
|
let firstTodo = filteredTodos?.[0];
|
||||||
|
// resolve the repeating
|
||||||
|
const batch = firestore().batch();
|
||||||
|
const todosToAddCycles = filteredTodos?.length / firstTodo?.repeatDays?.length;
|
||||||
|
console.log(todosToAddCycles);
|
||||||
|
if (firstTodo?.repeatDays !== todoData.repeatDays) {
|
||||||
|
let newRepeatDays = todoData.repeatDays?.filter((element) => firstTodo?.repeatDays?.indexOf(element) === -1);
|
||||||
|
const dates = [];
|
||||||
|
|
||||||
|
let date = firstTodo?.date;
|
||||||
|
const originalDateDay = format(date, 'EEEE');
|
||||||
|
const originalNumber = daysOfWeek.indexOf(originalDateDay);
|
||||||
|
newRepeatDays?.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);
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("REPEAT")
|
||||||
|
console.log(newRepeatDays);
|
||||||
|
console.log(dates);
|
||||||
|
|
||||||
|
filteredTodos?.forEach((item) => {
|
||||||
|
|
||||||
|
batch.update(item.ref, {...todoData, date: item.date});
|
||||||
|
})
|
||||||
|
// TODO: for the next connected -> filtered todos - number of weeks
|
||||||
|
for (let i = 0; i < todosToAddCycles; i++) {
|
||||||
|
dates?.forEach((dateToAdd) => {
|
||||||
|
let newTodoDate = addWeeks(dateToAdd, i);
|
||||||
|
if (compareAsc(newTodoDate, firstTodo?.date) !== 0) {
|
||||||
|
const newTodo = { ...todoData, date: newTodoDate };
|
||||||
|
|
||||||
|
let docRef = firestore().collection("Todos").doc();
|
||||||
|
batch.set(docRef, newTodo);
|
||||||
|
console.log("ADD")
|
||||||
|
console.log(newTodo);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
filteredTodos?.forEach((item) => {
|
||||||
|
|
||||||
|
console.log("UPDATE");
|
||||||
|
batch.update(item.ref, {...todoData, date: item.date});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
await batch.commit();
|
||||||
|
} else {
|
||||||
|
console.log("Update");
|
||||||
|
console.log(todoData);
|
||||||
await firestore()
|
await firestore()
|
||||||
.collection("Todos")
|
.collection("Todos")
|
||||||
.doc(todoData.id)
|
.doc(todoData.id)
|
||||||
.update(todoData);
|
.update(todoData);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user