Skip to content

Commit

Permalink
Fix event deletion bug
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoGresse committed Nov 5, 2023
1 parent a4971ee commit 96fe0f0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/events/new/NewEventDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
})
}}>
<DialogContent>
Expand Down
2 changes: 1 addition & 1 deletion src/events/page/settings/EventSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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('../../')
}}>
<DialogContentText id="alert-dialog-description">
{' '}
Expand Down
16 changes: 9 additions & 7 deletions src/services/hooks/firestoreQueryHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ export const useFirestoreCollection = <T>(query: Query<T>, subscribe: boolean =
}, [])

useEffect(() => {
const unsubFunc = load()

return unsubFunc
return load()
}, [load])

return {
Expand All @@ -82,9 +80,15 @@ export const useFirestoreDocument = <T>(ref: DocumentReference<T>, subscribe: bo
const isError = error !== ''

const docTransformer = (querySnapshot: DocumentSnapshot<T, DocumentData>) => {
const d = querySnapshot.data()

if (d === undefined) {
return
}

setData({
id: querySnapshot.id,
...(querySnapshot.data() as T),
...(d as T),
})
}

Expand Down Expand Up @@ -114,9 +118,7 @@ export const useFirestoreDocument = <T>(ref: DocumentReference<T>, subscribe: bo
}, [])

useEffect(() => {
const unsubFunc = load()

return unsubFunc
return load()
}, [])

return {
Expand Down
6 changes: 4 additions & 2 deletions src/services/hooks/useEvent.ts
Original file line number Diff line number Diff line change
@@ -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<DocumentData> => {
const ref = doc(collections.events, eventId)
return useFirestoreDocument(ref, true)
const ref: DocumentReference<Event> = doc(collections.events, eventId) as DocumentReference<Event>
return useFirestoreDocument<Event>(ref, true)
}

0 comments on commit 96fe0f0

Please sign in to comment.