diff --git a/components/pages/calendar/ManuallyAddEventModal.tsx b/components/pages/calendar/ManuallyAddEventModal.tsx index 236e390..8659f42 100644 --- a/components/pages/calendar/ManuallyAddEventModal.tsx +++ b/components/pages/calendar/ManuallyAddEventModal.tsx @@ -1,323 +1,331 @@ import { - Avatar, - Colors, - DateTimePicker, - LoaderScreen, - Modal, - Picker, - Switch, - Text, - TextField, - View, + Avatar, + Colors, + DateTimePicker, + LoaderScreen, + Modal, + Picker, + Switch, + Text, + TextField, + View, } from "react-native-ui-lib"; -import { ScrollView, TouchableOpacity } from "react-native-gesture-handler"; -import { useSafeAreaInsets } from "react-native-safe-area-context"; -import {useEffect, useState} from "react"; -import { MaterialIcons } from "@expo/vector-icons"; -import { PickerMultiValue } from "react-native-ui-lib/src/components/picker/types"; -import { useAuthContext } from "@/contexts/AuthContext"; -import { useCreateEvent } from "@/hooks/firebase/useCreateEvent"; -import { EventData } from "@/hooks/firebase/types/eventData"; +import {ScrollView, TouchableOpacity} from "react-native-gesture-handler"; +import {useSafeAreaInsets} from "react-native-safe-area-context"; +import {useState} from "react"; +import {MaterialIcons} from "@expo/vector-icons"; +import {PickerMultiValue} from "react-native-ui-lib/src/components/picker/types"; +import {useAuthContext} from "@/contexts/AuthContext"; +import {useCreateEvent} from "@/hooks/firebase/useCreateEvent"; +import {EventData} from "@/hooks/firebase/types/eventData"; import {addHours} from "date-fns"; const daysOfWeek = [ - { label: "Monday", value: "monday" }, - { label: "Tuesday", value: "tuesday" }, - { label: "Wednesday", value: "wednesday" }, - { label: "Thursday", value: "thursday" }, - { label: "Friday", value: "friday" }, - { label: "Saturday", value: "saturday" }, - { label: "Sunday", value: "sunday" }, + {label: "Monday", value: "monday"}, + {label: "Tuesday", value: "tuesday"}, + {label: "Wednesday", value: "wednesday"}, + {label: "Thursday", value: "thursday"}, + {label: "Friday", value: "friday"}, + {label: "Saturday", value: "saturday"}, + {label: "Sunday", value: "sunday"}, ]; export const ManuallyAddEventModal = ({ - show, - close, - initialDate -}: { - show: boolean; - close: () => void; - initialDate?: Date + show, + close, + initialDate + }: { + show: boolean; + close: () => void; + initialDate?: Date }) => { - const { user } = useAuthContext(); - const insets = useSafeAreaInsets(); + const {user} = useAuthContext(); + const insets = useSafeAreaInsets(); - const [title, setTitle] = useState(""); + const [title, setTitle] = useState(""); - const [isAllDay, setIsAllDay] = useState(false); - const [startTime, setStartTime] = useState(initialDate ?? new Date()); - const [endTime, setEndTime] = useState(initialDate ? addHours(initialDate, 1) : new Date()); - - const [startDate, setStartDate] = useState(initialDate ?? new Date()); - const [endDate, setEndDate] = useState(initialDate ?? new Date()); - - const [repeatInterval, setRepeatInterval] = useState([]); - - const { mutateAsync: createEvent, isLoading, isError } = useCreateEvent(); - - const formatDateTime = (date: Date) => { - return date.toLocaleDateString("en-US", { - weekday: "long", - month: "short", - day: "numeric", + const [isAllDay, setIsAllDay] = useState(false); + const [startTime, setStartTime] = useState(() => { + const date = initialDate ?? new Date(); + date.setSeconds(0, 0); + return date; + }); + const [endTime, setEndTime] = useState(() => { + const date = initialDate ? addHours(initialDate, 1) : new Date(); + date.setSeconds(0, 0); + return date; }); - }; - const combineDateAndTime = (date: Date, time: Date): Date => { - const combined = new Date(date); - combined.setHours(time.getHours()); - combined.setMinutes(time.getMinutes()); - combined.setSeconds(0); - combined.setMilliseconds(0); - return combined; - }; + const [startDate, setStartDate] = useState(initialDate ?? new Date()); + const [endDate, setEndDate] = useState(initialDate ?? new Date()); - const handleSave = async () => { - let finalStartDate: Date; - let finalEndDate: Date; + const [repeatInterval, setRepeatInterval] = useState([]); - if (isAllDay) { - finalStartDate = new Date(startDate); - finalStartDate.setHours(0, 0, 0, 0); + const {mutateAsync: createEvent, isLoading, isError} = useCreateEvent(); - finalEndDate = new Date(startDate); - finalEndDate.setHours(23, 59, 59, 999); - } else { - finalStartDate = combineDateAndTime(startDate, startTime); - finalEndDate = combineDateAndTime(endDate, endTime); - } - - const eventData: Partial = { - title, - startDate: finalStartDate, - endDate: finalEndDate, - repeatDays: repeatInterval.map((x) => x.toString()), - allDay: isAllDay, + const formatDateTime = (date: Date) => { + return date.toLocaleDateString("en-US", { + weekday: "long", + month: "short", + day: "numeric", + }); }; - await createEvent(eventData); + const combineDateAndTime = (date: Date, time: Date): Date => { + const combined = new Date(date); + combined.setHours(time.getHours()); + combined.setMinutes(time.getMinutes()); + combined.setSeconds(0); + combined.setMilliseconds(0); + return combined; + }; - close(); - }; + const handleSave = async () => { + let finalStartDate: Date; + let finalEndDate: Date; - const getRepeatLabel = () => { - const selectedDays = repeatInterval; - const allDays = [ - "sunday", - "monday", - "tuesday", - "wednesday", - "thursday", - "friday", - "saturday", - ]; - const workDays = ["monday", "tuesday", "wednesday", "thursday", "friday"]; + if (isAllDay) { + finalStartDate = new Date(startDate); + finalStartDate.setHours(0, 0, 0, 0); - const isEveryWorkDay = workDays.every((day) => selectedDays.includes(day)); + finalEndDate = new Date(startDate); + finalEndDate.setHours(23, 59, 59, 999); + } else { + finalStartDate = combineDateAndTime(startDate, startTime); + finalEndDate = combineDateAndTime(endDate, endTime); + } - const isEveryDay = allDays.every((day) => selectedDays.includes(day)); + const eventData: Partial = { + title, + startDate: finalStartDate, + endDate: finalEndDate, + repeatDays: repeatInterval.map((x) => x.toString()), + allDay: isAllDay, + }; - if (isEveryDay) { - return "Every day"; - } else if ( - isEveryWorkDay && - !selectedDays.includes("saturday") && - !selectedDays.includes("sunday") - ) { - return "Every work day"; - } else { - return selectedDays - .map((item) => daysOfWeek.find((day) => day.value === item)?.label) - .join(", "); + await createEvent(eventData); + + close(); + }; + + const getRepeatLabel = () => { + const selectedDays = repeatInterval; + const allDays = [ + "sunday", + "monday", + "tuesday", + "wednesday", + "thursday", + "friday", + "saturday", + ]; + const workDays = ["monday", "tuesday", "wednesday", "thursday", "friday"]; + + const isEveryWorkDay = workDays.every((day) => selectedDays.includes(day)); + + const isEveryDay = allDays.every((day) => selectedDays.includes(day)); + + if (isEveryDay) { + return "Every day"; + } else if ( + isEveryWorkDay && + !selectedDays.includes("saturday") && + !selectedDays.includes("sunday") + ) { + return "Every work day"; + } else { + return selectedDays + .map((item) => daysOfWeek.find((day) => day.value === item)?.label) + .join(", "); + } + }; + + if (isLoading && !isError) { + return ( + + + + ); } - }; - if (isLoading && !isError) { return ( - - - - ); - } - - return ( - - - - - Cancel - - Add event - - Save - - - - - - - - - - - - All-day - - setIsAllDay(value)} - /> - - - - - {!isAllDay && ( - - )} - - - {!isAllDay && ( - - + + + Cancel + + Add event + + Save + + + + + + + + + + + + All-day + + setIsAllDay(value)} + /> + + + + + {!isAllDay && ( + + )} + + + {!isAllDay && ( + + + + + )} + + + + setRepeatInterval(items)} + placeholder="Doest not repeat" + style={{marginLeft: 10, flex: 1}} + mode={Picker.modes.MULTI} + getLabel={getRepeatLabel} + > + {daysOfWeek.map((option) => ( + + ))} + + + + + + + + + Other + {user?.email} + + + + - )} - - - - setRepeatInterval(items)} - placeholder="Doest not repeat" - style={{ marginLeft: 10, flex: 1 }} - mode={Picker.modes.MULTI} - getLabel={getRepeatLabel} - > - {daysOfWeek.map((option) => ( - - ))} - - - - - - - - - Other - {user?.email} - - - - - - - ); + + ); };