From 96fe0f0691d2e79592156c5ac5ae9fa3ffd5a64a Mon Sep 17 00:00:00 2001 From: Hugo Gresse Date: Sun, 5 Nov 2023 14:28:42 +0100 Subject: [PATCH] Fix event deletion bug --- src/events/new/NewEventDialog.tsx | 4 +++- src/events/page/settings/EventSettings.tsx | 2 +- src/services/hooks/firestoreQueryHook.ts | 16 +++++++++------- src/services/hooks/useEvent.ts | 6 ++++-- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/events/new/NewEventDialog.tsx b/src/events/new/NewEventDialog.tsx index 5159aef..c07ccf9 100644 --- a/src/events/new/NewEventDialog.tsx +++ b/src/events/new/NewEventDialog.tsx @@ -10,6 +10,7 @@ import { collections } from '../../services/firebase' import LoadingButton from '@mui/lab/LoadingButton' import { NewEvent } from '../../types' import { serverTimestamp } from 'firebase/firestore' +import { useNotification } from '../../hooks/notificationHook' export type NewEventDialogProps = { isOpen: boolean @@ -25,6 +26,7 @@ const schema = yup export const NewEventDialog = ({ isOpen, onClose }: NewEventDialogProps) => { const userId = useSelector(selectUserIdOpenPlanner) + const { createNotification } = useNotification() const formContext = useForm() const { formState } = formContext @@ -66,8 +68,8 @@ export const NewEventDialog = ({ isOpen, onClose }: NewEventDialogProps) => { onClose(eventId) }) .catch((error: Error) => { + createNotification('Error while creating event: ' + error.message, { type: 'error' }) console.error('error creating event', error) - alert('Error creating event: ' + error.message) }) }}> diff --git a/src/events/page/settings/EventSettings.tsx b/src/events/page/settings/EventSettings.tsx index 9bb525e..79ff1e3 100644 --- a/src/events/page/settings/EventSettings.tsx +++ b/src/events/page/settings/EventSettings.tsx @@ -190,9 +190,9 @@ export const EventSettings = ({ event }: EventSettingsProps) => { handleAccept={async () => { await deleteSessionsAndSpeakers(event, false) await documentDeletion.mutate() - setLocation('../../') setDeleteOpen(false) createNotification('Event deleted', { type: 'success' }) + setLocation('../../') }}> {' '} diff --git a/src/services/hooks/firestoreQueryHook.ts b/src/services/hooks/firestoreQueryHook.ts index a1d8a1a..51944ac 100644 --- a/src/services/hooks/firestoreQueryHook.ts +++ b/src/services/hooks/firestoreQueryHook.ts @@ -60,9 +60,7 @@ export const useFirestoreCollection = (query: Query, subscribe: boolean = }, []) useEffect(() => { - const unsubFunc = load() - - return unsubFunc + return load() }, [load]) return { @@ -82,9 +80,15 @@ export const useFirestoreDocument = (ref: DocumentReference, subscribe: bo const isError = error !== '' const docTransformer = (querySnapshot: DocumentSnapshot) => { + const d = querySnapshot.data() + + if (d === undefined) { + return + } + setData({ id: querySnapshot.id, - ...(querySnapshot.data() as T), + ...(d as T), }) } @@ -114,9 +118,7 @@ export const useFirestoreDocument = (ref: DocumentReference, subscribe: bo }, []) useEffect(() => { - const unsubFunc = load() - - return unsubFunc + return load() }, []) return { diff --git a/src/services/hooks/useEvent.ts b/src/services/hooks/useEvent.ts index eb660ed..c9d7677 100644 --- a/src/services/hooks/useEvent.ts +++ b/src/services/hooks/useEvent.ts @@ -1,8 +1,10 @@ import { collections } from '../firebase' import { doc, DocumentData } from '@firebase/firestore' import { useFirestoreDocument, UseQueryResult } from './firestoreQueryHook' +import { DocumentReference } from 'firebase/firestore' +import { Event } from '../../types' export const useEvent = (eventId?: string): UseQueryResult => { - const ref = doc(collections.events, eventId) - return useFirestoreDocument(ref, true) + const ref: DocumentReference = doc(collections.events, eventId) as DocumentReference + return useFirestoreDocument(ref, true) }