Skip to content

Commit

Permalink
chore: show toasts and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tshuli committed Jan 30, 2024
1 parent dfa9bcd commit a3a296d
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions frontend/src/features/admin-form/assistance/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { parseModelOutput } from '~features/admin-form/assistance/utils'
import { useCreateFormField } from '~features/admin-form/create/builder-and-design/mutations/useCreateFormField'

import { useToast } from '../../../hooks/useToast'
import { adminFormKeys } from '../common/queries'
import { MagicFormBuilderModalOnCloseContext } from '../create/builder-and-design/BuilderAndDesignContent/FormBuilder'

Expand All @@ -22,6 +23,8 @@ export const useAssistanceMutations = () => {
throw new Error('Form ID is required')
}

const toast = useToast({ status: 'success', isClosable: true })

const queryClient = useQueryClient()

const onCloseContext = useContext(MagicFormBuilderModalOnCloseContext)
Expand All @@ -35,12 +38,29 @@ export const useAssistanceMutations = () => {
.then((data) => {
let formFields
if (data.content) {
formFields = JSON.parse(parseModelOutput(data.content))
try {
formFields = JSON.parse(parseModelOutput(data.content))
} catch (e) {
toast({
title: `Error creating form. Reason: ${e}`,
status: 'warning',
})
return
}
}
return createFieldsMutation.mutate(formFields, {
onSuccess: () => {
queryClient.invalidateQueries(adminFormKeys.id(formId))
onClose()
toast({
title: 'Successfully created form',
})
},
onError: () => {
toast({
title: 'Error creating form.',
status: 'warning',
})
},
})
}),
Expand All @@ -50,12 +70,29 @@ export const useAssistanceMutations = () => {
generateFormFields(ContentTypes.PDF, pdfContent).then((data) => {
let formFields
if (data.content) {
formFields = JSON.parse(parseModelOutput(data.content))
try {
formFields = JSON.parse(parseModelOutput(data.content))
} catch (e) {
toast({
title: `Error creating form. Reason: ${e}`,
status: 'warning',
})
return
}
}
return createFieldsMutation.mutate(formFields, {
onSuccess: () => {
queryClient.invalidateQueries(adminFormKeys.id(formId))
onClose()
toast({
title: 'Successfully created form',
})
},
onError: () => {
toast({
title: 'Error creating form.',
status: 'warning',
})
},
})
}),
Expand Down

0 comments on commit a3a296d

Please sign in to comment.