Skip to content

Commit

Permalink
feat(delete action): add alerts for success / failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Mar 14, 2024
1 parent 609a352 commit 4dc297a
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/components/sectionList/SectionListWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FetchError } from '@dhis2/app-runtime'
import { FetchError, useAlert } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import { SharingDialog } from '@dhis2/ui'
import React, { useCallback, useState } from 'react'
import { BaseListModel, canEditModel, useSchemaFromHandle, useDeleteModelMutation } from '../../lib'
Expand Down Expand Up @@ -39,14 +40,37 @@ export const SectionListWrapper = ({
useSelectedModels()
const [detailsId, setDetailsId] = useState<string | undefined>()
const [sharingDialogId, setSharingDialogId] = useState<string | undefined>()
const { show: showDeletionSuccess } = useAlert(
i18n.t('Successfully deleted the {{model}}', {
model: schema.singular,
}),
{ success: true }
)
const { show: showDeletionFailure } = useAlert(
i18n.t('Failed deleting the {{model}}!', {
model: schema.singular,
}),
{ success: false }
)
const deleteModelMutation = useDeleteModelMutation()
const deleteModel = useCallback(async (id: string) => {
await deleteModelMutation.mutateAsync({
id,
schema,
})
refetch()
}, [deleteModelMutation, schema, refetch])
try {
await deleteModelMutation.mutateAsync({
id,
schema,
})
refetch()
showDeletionSuccess()
} catch (e) {
showDeletionFailure()
}
}, [
deleteModelMutation,
schema,
refetch,
showDeletionSuccess,
showDeletionFailure,
])

const SectionListMessage = () => {
if (error) {
Expand Down

0 comments on commit 4dc297a

Please sign in to comment.