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>
|
||||
</Button>
|
||||
</View>
|
||||
<AddChoreDialog isVisible={isVisible} setIsVisible={setIsVisible} />
|
||||
{isVisible && <AddChoreDialog isVisible={isVisible} setIsVisible={setIsVisible} />}
|
||||
</LinearGradient>
|
||||
);
|
||||
};
|
||||
|
||||
@ -36,6 +36,7 @@ const defaultTodo = {
|
||||
rotate: false,
|
||||
repeatType: "Every week",
|
||||
assignees: [],
|
||||
repeatDays: []
|
||||
};
|
||||
|
||||
const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
||||
@ -48,14 +49,12 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
||||
);
|
||||
const { width, height } = Dimensions.get("screen");
|
||||
const [points, setPoints] = useState<number>(todo.points);
|
||||
const [selectedDays, setSelectedDays] = useState([]);
|
||||
|
||||
const { data: members } = useGetFamilyMembers();
|
||||
|
||||
const handleClose = () => {
|
||||
setTodo(defaultTodo);
|
||||
setSelectedAssignees([]);
|
||||
setSelectedDays([]);
|
||||
addChoreDialogProps.setIsVisible(false);
|
||||
};
|
||||
|
||||
@ -71,12 +70,20 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
||||
|
||||
const handleRepeatDaysChange = (day: string, set: boolean) => {
|
||||
if (set) {
|
||||
setSelectedDays((prevState) => [...prevState, day]);
|
||||
const updatedTodo = {
|
||||
...todo,
|
||||
repeatDays: [...todo.repeatDays, day]
|
||||
}
|
||||
setTodo(updatedTodo);
|
||||
} else {
|
||||
const array = selectedDays;
|
||||
const array = todo.repeatDays ?? [];
|
||||
let index = array.indexOf(day);
|
||||
array.splice(index, 1);
|
||||
setSelectedDays(array);
|
||||
const updatedTodo = {
|
||||
...todo,
|
||||
repeatDays: array
|
||||
}
|
||||
setTodo(updatedTodo);
|
||||
}
|
||||
}
|
||||
|
||||
@ -123,8 +130,7 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
||||
updateToDo({
|
||||
...todo,
|
||||
points: points,
|
||||
assignees: selectedAssignees,
|
||||
repeatDays: selectedDays,
|
||||
assignees: selectedAssignees
|
||||
});
|
||||
} else {
|
||||
addToDo({
|
||||
@ -132,7 +138,7 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
|
||||
done: false,
|
||||
points: points,
|
||||
assignees: selectedAssignees,
|
||||
repeatDays: selectedDays
|
||||
repeatDays: todo.repeatDays ?? []
|
||||
});
|
||||
}
|
||||
handleClose();
|
||||
|
||||
@ -26,11 +26,14 @@ export default RepeatFreq;
|
||||
const RepeatOption = ({ value, handleRepeatDaysChange, repeatDays }: { value: string, handleRepeatDaysChange: Function, repeatDays: string[] }) => {
|
||||
const [isSet, setisSet] = useState(repeatDays.includes(value));
|
||||
|
||||
useEffect(() => {
|
||||
handleRepeatDaysChange(value, isSet);
|
||||
}, [isSet])
|
||||
|
||||
const handleDayChange = () => {
|
||||
handleRepeatDaysChange(value, !isSet)
|
||||
setisSet(!isSet);
|
||||
}
|
||||
|
||||
return (
|
||||
<TouchableOpacity onPress={() => setisSet(!isSet)}>
|
||||
<TouchableOpacity onPress={handleDayChange}>
|
||||
<View
|
||||
center
|
||||
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 {addDays, addMonths, addWeeks, compareAsc, format, getDay, subDays} from "date-fns";
|
||||
|
||||
const daysOfWeek = [
|
||||
export const daysOfWeek = [
|
||||
DAYS_OF_WEEK_ENUM.MONDAY,
|
||||
DAYS_OF_WEEK_ENUM.TUESDAY,
|
||||
DAYS_OF_WEEK_ENUM.WEDNESDAY,
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import { useQuery } from "react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
import { useAuthContext } from "@/contexts/AuthContext";
|
||||
import {UserProfile} from "@/hooks/firebase/types/profileTypes";
|
||||
import {IToDo} from "@/hooks/firebase/types/todoData";
|
||||
|
||||
export const useGetTodos = () => {
|
||||
@ -23,6 +22,7 @@ export const useGetTodos = () => {
|
||||
...data,
|
||||
id: doc.id,
|
||||
date: data.date ? new Date(data.date.seconds * 1000) : null,
|
||||
repeatDays: data.repeatDays ?? []
|
||||
};
|
||||
}) as IToDo[];
|
||||
}
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
import { useMutation, useQueryClient } from "react-query";
|
||||
import firestore from "@react-native-firebase/firestore";
|
||||
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 = () => {
|
||||
const queryClients = useQueryClient()
|
||||
@ -9,10 +11,94 @@ export const useUpdateTodo = () => {
|
||||
mutationKey: ["updateTodo"],
|
||||
mutationFn: async (todoData: Partial<IToDo>) => {
|
||||
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()
|
||||
.collection("Todos")
|
||||
.doc(todoData.id)
|
||||
.update(todoData);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user