From 4dc297abbf1bb80947b276d9d09ec06d82f4f70a Mon Sep 17 00:00:00 2001 From: Mohammer5 Date: Thu, 14 Mar 2024 10:17:48 +0800 Subject: [PATCH] feat(delete action): add alerts for success / failure --- .../sectionList/SectionListWrapper.tsx | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/components/sectionList/SectionListWrapper.tsx b/src/components/sectionList/SectionListWrapper.tsx index b1f21860..18f5bdf0 100644 --- a/src/components/sectionList/SectionListWrapper.tsx +++ b/src/components/sectionList/SectionListWrapper.tsx @@ -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' @@ -39,14 +40,37 @@ export const SectionListWrapper = ({ useSelectedModels() const [detailsId, setDetailsId] = useState() const [sharingDialogId, setSharingDialogId] = useState() + 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) {