(
{ initialCalendarEvent, calendars, formData: formDataObject, error, notice }: ViewCalendarEventProps,
)
| 126 | } |
| 127 | |
| 128 | export default function ViewCalendarEvent( |
| 129 | { initialCalendarEvent, calendars, formData: formDataObject, error, notice }: ViewCalendarEventProps, |
| 130 | ) { |
| 131 | const isDeleting = useSignal<boolean>(false); |
| 132 | const calendarEvent = useSignal<CalendarEvent>(initialCalendarEvent); |
| 133 | |
| 134 | const formData = convertObjectToFormData(formDataObject); |
| 135 | |
| 136 | async function onClickDeleteEvent() { |
| 137 | const message = calendarEvent.peek().isRecurring |
| 138 | ? 'Are you sure you want to delete _all_ instances of this recurring event?' |
| 139 | : 'Are you sure you want to delete this event?'; |
| 140 | if (confirm(message)) { |
| 141 | if (isDeleting.value) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | isDeleting.value = true; |
| 146 | |
| 147 | try { |
| 148 | const requestBody: DeleteRequestBody = { |
| 149 | calendarIds: calendars.map((calendar) => calendar.uid!), |
| 150 | calendarView: 'day', |
| 151 | calendarStartDate: new Date().toISOString().substring(0, 10), |
| 152 | calendarEventId: calendarEvent.value.uid!, |
| 153 | calendarId: calendarEvent.value.calendarId, |
| 154 | }; |
| 155 | const response = await fetch(`/api/calendar/delete-event`, { |
| 156 | method: 'POST', |
| 157 | body: JSON.stringify(requestBody), |
| 158 | }); |
| 159 | const result = await response.json() as DeleteResponseBody; |
| 160 | |
| 161 | if (!result.success) { |
| 162 | throw new Error('Failed to delete event!'); |
| 163 | } |
| 164 | |
| 165 | window.location.href = '/calendar'; |
| 166 | } catch (error) { |
| 167 | console.error(error); |
| 168 | } |
| 169 | |
| 170 | isDeleting.value = false; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return ( |
| 175 | <> |
| 176 | <section class='flex flex-row items-center justify-between mb-4'> |
| 177 | <a href='/calendar' class='mr-2'>View calendar</a> |
| 178 | <section class='flex items-center'> |
| 179 | <button |
| 180 | class='inline-block justify-center gap-x-1.5 rounded-md bg-red-800 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-red-600 ml-2' |
| 181 | type='button' |
| 182 | title='Delete event' |
| 183 | onClick={() => onClickDeleteEvent()} |
| 184 | > |
| 185 | <img |
nothing calls this directly
no test coverage detected