diff --git a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteButton/i18n.json b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteButton/i18n.json deleted file mode 100644 index c3f96b496b..0000000000 --- a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteButton/i18n.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "namespace": "localUnitDeleteButton", - "strings": { - "localUnitsDelete": "Delete", - "deleteSuccessMessage": "{localUnitName} was deleted.", - "deleteFailureMessage": "Failed to validate {localUnitName}", - "deleteLocalUnitHeading": "Delete Local Unit Confirmation", - "deleteLocalUnitMessage": "Are you sure you want to delete \"{localUnitName}\"?" - } -} diff --git a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteButton/index.tsx b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteButton/index.tsx deleted file mode 100644 index b08217aee2..0000000000 --- a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteButton/index.tsx +++ /dev/null @@ -1,104 +0,0 @@ -import { - ButtonVariant, - ConfirmButton, -} from '@ifrc-go/ui'; -import { useTranslation } from '@ifrc-go/ui/hooks'; -import { resolveToString } from '@ifrc-go/ui/utils'; - -import usePermissions from '#hooks/domain/usePermissions'; -import useAlert from '#hooks/useAlert'; -import { useLazyRequest } from '#utils/restRequest'; - -import i18n from './i18n.json'; - -const hideDelete = true; - -interface Props { - countryId: number; - localUnitId: number; - onActionSuccess: () => void; - localUnitName: string | null | undefined; - disabled?: boolean; - variant?: ButtonVariant; -} - -function LocalUnitDeleteButton(props: Props) { - const strings = useTranslation(i18n); - const { - countryId, - localUnitId, - localUnitName, - onActionSuccess, - disabled, - variant = 'secondary', - } = props; - - const { isCountryAdmin, isSuperUser } = usePermissions(); - const alert = useAlert(); - - const hasDeletePermission = isSuperUser || isCountryAdmin(countryId); - - const { - pending: validateLocalUnitPending, - trigger: validateLocalUnit, - } = useLazyRequest({ - method: 'DELETE', - url: '/api/v2/local-units/{id}/', - pathVariables: { id: localUnitId }, - onSuccess: () => { - const validationMessage = resolveToString( - strings.deleteSuccessMessage, - { localUnitName }, - ); - alert.show( - validationMessage, - { variant: 'success' }, - ); - onActionSuccess(); - }, - onFailure: (response) => { - const { - value: { messageForNotification }, - debugMessage, - } = response; - - alert.show( - resolveToString( - strings.deleteFailureMessage, - { localUnitName }, - ), - { - variant: 'danger', - description: messageForNotification, - debugMessage, - }, - ); - }, - }); - - if (hideDelete) { - return null; - } - - return ( - - {strings.localUnitsDelete} - - ); -} - -export default LocalUnitDeleteButton; diff --git a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteModal/i18n.json b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteModal/i18n.json new file mode 100644 index 0000000000..8434576c15 --- /dev/null +++ b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteModal/i18n.json @@ -0,0 +1,11 @@ +{ + "namespace": "localUnitDeleteModal", + "strings": { + "submitLabel": "Submit", + "deleteSuccessMessage": "{localUnitName} has been successfully deleted.", + "deleteFailureMessage": "Unable to delete {localUnitName}. Please try again.", + "chooseDeleteReasonMessage": "Choose the reason to delete local unit.", + "deleteReasonExplanation": "Explain the reason why the local unit is being deleted.", + "deleteLocalUnitHeading": "Are you sure you want to delete \"{localUnitName}\"?" + } +} diff --git a/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteModal/index.tsx b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteModal/index.tsx new file mode 100644 index 0000000000..d5476a0f45 --- /dev/null +++ b/app/src/views/CountryNsOverviewContextAndStructure/NationalSocietyLocalUnits/LocalUnitDeleteModal/index.tsx @@ -0,0 +1,188 @@ +import { + useCallback, + useMemo, +} from 'react'; +import { + Button, + Modal, + RadioInput, + TextArea, +} from '@ifrc-go/ui'; +import { useTranslation } from '@ifrc-go/ui/hooks'; +import { resolveToString } from '@ifrc-go/ui/utils'; +import { + createSubmitHandler, + getErrorObject, + ObjectSchema, + PartialForm, + requiredStringCondition, + useForm, +} from '@togglecorp/toggle-form'; + +import type { GlobalEnums } from '#contexts/domain'; +import useGlobalEnums from '#hooks/domain/useGlobalEnums'; +import useAlert from '#hooks/useAlert'; +import { + GoApiBody, + useLazyRequest, +} from '#utils/restRequest'; + +import i18n from './i18n.json'; +import styles from './styles.module.css'; + +type DeprecateReason = NonNullable[number]; + +type LocalUnitDeprecateBody = GoApiBody<'/api/v2/local-units/{id}/deprecate/', 'POST'>; + +type DeprecateFormType = PartialForm; +type FormSchema = ObjectSchema; +type FormSchemaFields = ReturnType; + +const deprecateReasonKeySelector = ( + item: DeprecateReason, +) => item.key; + +const deprecateReasonLabelSelector = ( + item: DeprecateReason, +) => item.value; + +const defaultFormValue: DeprecateFormType = { +}; + +const schema: FormSchema = { + fields: (): FormSchemaFields => ({ + deprecated_reason: { + required: true, + }, + deprecated_reason_overview: { + required: true, + requiredValidation: requiredStringCondition, + }, + }), +}; + +interface Props { + localUnitId: number; + onDeleteActionSuccess?: () => void; + onClose: () => void; + localUnitName: string; +} + +function LocalUnitDeleteModal(props: Props) { + const strings = useTranslation(i18n); + const { + localUnitId, + localUnitName, + onDeleteActionSuccess, + onClose, + } = props; + + const { + local_units_deprecate_reason: deprecateReasonOptions, + } = useGlobalEnums(); + + const { + value, + error: formError, + setFieldValue, + setError, + validate, + } = useForm(schema, { value: defaultFormValue }); + + const error = useMemo( + () => getErrorObject(formError), + [formError], + ); + + const alert = useAlert(); + + const { + pending: deprecateLocalUnitPending, + trigger: deprecateLocalUnit, + } = useLazyRequest({ + method: 'POST', + url: '/api/v2/local-units/{id}/deprecate/', + body: (body: LocalUnitDeprecateBody) => body, + pathVariables: { id: localUnitId }, + onSuccess: () => { + const validationMessage = resolveToString( + strings.deleteSuccessMessage, + { localUnitName }, + ); + alert.show( + validationMessage, + { variant: 'success' }, + ); + if (onDeleteActionSuccess) { + onDeleteActionSuccess(); + } + }, + onFailure: (response) => { + const { + value: { messageForNotification }, + } = response; + + alert.show( + resolveToString( + strings.deleteFailureMessage, + { localUnitName }, + ), + { + variant: 'danger', + description: messageForNotification, + }, + ); + }, + }); + + const handleFormSubmit = useCallback( + (formValues: DeprecateFormType) => { + deprecateLocalUnit(formValues as LocalUnitDeprecateBody); + }, + [deprecateLocalUnit], + ); + + return ( + + {strings.submitLabel} + + )} + > + +