From 39dd2ee126754b4db61eb8f81b2747a9c2d4df93 Mon Sep 17 00:00:00 2001 From: "vishalkondle.dev" Date: Sun, 7 Jul 2024 20:13:36 +0530 Subject: [PATCH] fix: event color | update and delete event --- app/(private)/calendar/page.tsx | 147 +++++++++++++++++++++++----- components/Calendar/Event.tsx | 54 +++++----- components/Calendar/SelectedDay.tsx | 80 ++++++++------- lib/constants.tsx | 3 +- models/Event.ts | 12 ++- 5 files changed, 206 insertions(+), 90 deletions(-) diff --git a/app/(private)/calendar/page.tsx b/app/(private)/calendar/page.tsx index 9f8dece..9f1c922 100644 --- a/app/(private)/calendar/page.tsx +++ b/app/(private)/calendar/page.tsx @@ -13,19 +13,31 @@ import { Modal, Paper, rem, + Select, + SelectProps, Stack, Text, TextInput, + ThemeIcon, } from '@mantine/core'; import { DatePicker, DatePickerInput, DateTimePicker, MonthPickerInput } from '@mantine/dates'; import { useForm } from '@mantine/form'; import { useDisclosure, useMediaQuery, useViewportSize } from '@mantine/hooks'; -import { IconCalendar, IconChevronLeft, IconChevronRight } from '@tabler/icons-react'; +import { + IconCalendar, + IconCheck, + IconChevronLeft, + IconChevronRight, + IconTagFilled, + IconTrash, +} from '@tabler/icons-react'; import dayjs from 'dayjs'; import { useState } from 'react'; -import { apiCall, failure, success } from '@/lib/client_functions'; +import { apiCall, failure, openModal, success } from '@/lib/client_functions'; import useFetchData from '@/hooks/useFetchData'; import { SelectedDay } from '@/components/Calendar'; +import { COLORS } from '@/lib/constants'; +import { EventDocument } from '@/models/Event'; const CalendarPage = () => { const [date, setDate] = useState(dayjs().startOf('day').toDate()); @@ -42,10 +54,12 @@ const CalendarPage = () => { const form = useForm({ initialValues: { + _id: '', title: '', from: dayjs(date).set('hour', 9).toDate(), to: dayjs(date).set('hour', 10).toDate(), isAllDay: false, + color: 'blue', }, validate: { title: (value) => (!value ? 'Title is required' : null), @@ -54,7 +68,7 @@ const CalendarPage = () => { }, }); - const handleNewEvent = async (values: typeof form.values) => { + const handleEvent = async (values: typeof form.values) => { if (form.values.isAllDay) { values.from = dayjs(values.from).startOf('day').toDate(); values.to = dayjs(values.to).endOf('day').toDate(); @@ -64,8 +78,13 @@ const CalendarPage = () => { return; } try { - await apiCall('/api/events', values, 'POST'); - modalHandlers.close(); + if (form.values._id) { + await apiCall('/api/events', values, 'PUT'); + } else { + const { _id, ...rest } = values; + await apiCall('/api/events', rest, 'POST'); + } + modalHandlers.toggle(); form.reset(); success('Event created successfully'); refetch(); @@ -74,11 +93,45 @@ const CalendarPage = () => { } }; - const newEvent = () => { - modalHandlers.open(); - form.setFieldValue('from', dayjs(date).set('hour', 9).toDate()); - form.setFieldValue('to', dayjs(date).set('hour', 10).toDate()); - form.setFieldValue('isAllDay', false); + const newEvent = (time?: number) => { + form.reset(); + if (time !== undefined) { + form.setFieldValue('from', dayjs(date).set('hour', time).toDate()); + form.setFieldValue( + 'to', + dayjs(date) + .set('hour', time + 1) + .toDate() + ); + } else { + form.setFieldValue('from', dayjs(date).set('hour', 9).toDate()); + form.setFieldValue('to', dayjs(date).set('hour', 10).toDate()); + } + modalHandlers.toggle(); + }; + + const handleOpenEvent = (event: EventDocument) => { + form.reset(); + form.setFieldValue('from', dayjs(event.from).set('hour', 9).toDate()); + form.setFieldValue('to', dayjs(event.to).set('hour', 10).toDate()); + form.setFieldValue('isAllDay', event.isAllDay); + form.setFieldValue('title', event.title); + form.setFieldValue('color', event.color); + form.setFieldValue('_id', String(event?._id)); + modalHandlers.toggle(); + }; + + const handleDeleteEvent = () => { + openModal('Do you want to delete this event?', async () => { + try { + await apiCall(`/api/events?_id=${form.values._id}`, null, 'DELETE'); + success('Event deleted successfully'); + refetch(); + modalHandlers.toggle(); + } catch (error) { + failure(String(error) || 'Something went wrong'); + } + }); }; const renderDay = (day: Date) => ( @@ -92,8 +145,8 @@ const CalendarPage = () => { .slice(0, 2) .map((event) => ( { return ( -
{day.getDate()}
+ {day.getDate()}
); }; + const renderSelectOption: SelectProps['renderOption'] = ({ option, checked }) => ( + + + + + {option.label} + {checked && } + + ); + if (loading) return <>; return ( { styles={{ month: { width: '100%' }, monthRow: { width: '100%' }, - day: { width: '100%', height: rem((height * 0.54) / 5), padding: 0 }, + day: { width: '100%', height: rem((height * 0.65) / 5), padding: 0 }, levelsGroup: { minWidth: '100%' }, calendarHeader: { visibility: 'hidden', display: 'none' }, }} /> - -
handleNewEvent(values))}> + { + modalHandlers.toggle(); + form.reset(); + }} + title={form.values?._id ? 'Edit Event' : 'New Event'} + > + handleEvent(values))}> - - + + + + + + +