From 551d938da14de3974f7040c1c93fa18ec1ed9626 Mon Sep 17 00:00:00 2001 From: LWangllix Date: Wed, 16 Oct 2024 01:04:35 +0300 Subject: [PATCH 1/2] delete form when form status is returned --- src/api.ts | 6 +++++ src/components/other/DeleteCard.tsx | 4 +--- src/pages/ObservationForm/hooks/useData.ts | 28 ++++++++++++++++++++++ src/pages/ObservationForm/index.tsx | 11 ++++++++- src/pages/RequestForm/function.ts | 2 +- src/pages/TenantUserForm.tsx | 9 +++++-- src/utils/texts.ts | 8 ++++--- 7 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/api.ts b/src/api.ts index 7e17bbe..3d16630 100644 --- a/src/api.ts +++ b/src/api.ts @@ -441,6 +441,12 @@ class Api { }); }; + deleteObservationForm = async (id: string): Promise
=> + await this.delete({ + resource: Resources.FORMS, + id, + }); + getTenantUsers = async ({ filter, page, diff --git a/src/components/other/DeleteCard.tsx b/src/components/other/DeleteCard.tsx index a05d9bc..c71cc73 100644 --- a/src/components/other/DeleteCard.tsx +++ b/src/components/other/DeleteCard.tsx @@ -31,9 +31,7 @@ const DeleteCard = ({ {title} - {descriptionFirstPart} - {name} - {descriptionSecondPart} + {descriptionFirstPart} {name} {descriptionSecondPart} { }, ); + const removeUser = useMutation(() => api.deleteObservationForm(id), { + onError: () => { + handleErrorFromServerToast(); + }, + onSuccess: () => { + navigate(slugs.observationForms); + }, + retry: false, + }); + + const deleteInfo: DeleteInfoProps = { + deleteButtonText: buttonsTitles.removeForm, + deleteDescriptionFirstPart: deleteDescriptionFirstPart.entity, + deleteDescriptionSecondPart: deleteDescriptionSecondPart.form, + deleteTitle: deleteTitles.form, + deleteName: `#${observationForm?.id}`, + deleteFunction: + observationForm?.status === StatusTypes.RETURNED ? removeUser.mutateAsync : undefined, + }; + const disabled = !!observationForm && !observationForm?.canEdit; const mapQueryString = getMapQueryString(disabled); @@ -111,6 +138,7 @@ export const useData = () => { return { handleSubmit, + deleteInfo, loading: isFetching || specieLoading, mapQueryString, initialValues, diff --git a/src/pages/ObservationForm/index.tsx b/src/pages/ObservationForm/index.tsx index 2b19652..3c67475 100644 --- a/src/pages/ObservationForm/index.tsx +++ b/src/pages/ObservationForm/index.tsx @@ -25,7 +25,15 @@ import { useData } from './hooks/useData'; import { FormProps } from './types'; const ObservationForm = () => { - const { initialValues, id = '', disabled, loading, handleSubmit, mapQueryString } = useData(); + const { + initialValues, + id = '', + deleteInfo, + disabled, + loading, + handleSubmit, + mapQueryString, + } = useData(); const renderForm = (values: FormProps, errors: any, handleChange: any, setValues: any) => { const handleUpdateSpecie = (species: Species) => { @@ -120,6 +128,7 @@ const ObservationForm = () => { validationSchema={validateForm} disabled={disabled} submitButtonText={buttonsTitles.submit} + deleteInfo={deleteInfo} /> ); }; diff --git a/src/pages/RequestForm/function.ts b/src/pages/RequestForm/function.ts index 1ddfeeb..c305ab5 100644 --- a/src/pages/RequestForm/function.ts +++ b/src/pages/RequestForm/function.ts @@ -16,7 +16,7 @@ export const getDeleteInfo = ( return { deleteButtonText: type === RequestTypes.GET_ONCE ? buttonsTitles.deleteExcerpt : buttonsTitles.deleteRequest, - deleteDescriptionFirstPart: deleteDescriptionFirstPart.request, + deleteDescriptionFirstPart: deleteDescriptionFirstPart.entity, deleteDescriptionSecondPart: type === RequestTypes.GET_ONCE ? deleteDescriptionSecondPart.excerpt diff --git a/src/pages/TenantUserForm.tsx b/src/pages/TenantUserForm.tsx index 0058ab4..4d98b51 100644 --- a/src/pages/TenantUserForm.tsx +++ b/src/pages/TenantUserForm.tsx @@ -1,3 +1,9 @@ +import { + NumericTextField, + PhoneField, + SelectField, + TextField, +} from '@aplinkosministerija/design-system'; import { useMutation, useQuery } from 'react-query'; import { useNavigate, useParams } from 'react-router-dom'; import styled from 'styled-components'; @@ -21,7 +27,6 @@ import { } from '../utils/texts'; import { validateCreateTenantUser, validateUpdateTenantUser } from '../utils/validation'; import api from './../api'; -import { NumericTextField, PhoneField, SelectField, TextField } from '@aplinkosministerija/design-system'; interface TenantUserProps { firstName?: string; @@ -78,7 +83,7 @@ const TenantUserForm = () => { const deleteInfo: DeleteInfoProps = { deleteButtonText: buttonsTitles.removeTenantUser, - deleteDescriptionFirstPart: deleteDescriptionFirstPart.tenantUser, + deleteDescriptionFirstPart: deleteDescriptionFirstPart.entity, deleteDescriptionSecondPart: deleteDescriptionSecondPart.tenantUser, deleteTitle: deleteTitles.tenantUser, deleteName: `${user?.firstName} ${user?.lastName}`, diff --git a/src/utils/texts.ts b/src/utils/texts.ts index fc1725f..f856418 100644 --- a/src/utils/texts.ts +++ b/src/utils/texts.ts @@ -250,6 +250,7 @@ export const buttonsTitles = { edit: 'Atnaujinti', view: 'Peržiūrėti', removeTenantUser: 'Pašalinti darbuotoją', + removeForm: 'Pašalinti stebėjimą', deleteGroup: 'Ištrinti grupę', sarasas: 'Sąrašas', zemelapis: 'Žemėlapis', @@ -564,17 +565,18 @@ export const descriptions = { export const deleteTitles = { tenantUser: 'Pašalinti darbuotoją', request: 'Ištrinti prašymą', + form: 'Ištrinti stebėjimą', excerpt: 'Ištrinti išrašą', }; export const deleteDescriptionFirstPart = { - tenantUser: 'Ar esate tikri, kad norite pašalinti ', - request: 'Ar esate tikri, kad norite ištrinti ', + entity: 'Ar esate tikri, kad norite pašalinti', }; export const deleteDescriptionSecondPart = { - tenantUser: ' darbuotoją?', + tenantUser: 'darbuotoją?', request: 'prašymą?', + form: 'stebėjimą?', excerpt: 'išrašą?', }; From dfeb1ba61ca1935ac2b8a963870dfe350171aa4b Mon Sep 17 00:00:00 2001 From: LWangllix Date: Wed, 16 Oct 2024 01:06:33 +0300 Subject: [PATCH 2/2] update --- src/pages/ObservationForm/hooks/useData.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/ObservationForm/hooks/useData.ts b/src/pages/ObservationForm/hooks/useData.ts index bde1999..16c6d2c 100644 --- a/src/pages/ObservationForm/hooks/useData.ts +++ b/src/pages/ObservationForm/hooks/useData.ts @@ -29,7 +29,7 @@ export const useData = () => { }, ); - const removeUser = useMutation(() => api.deleteObservationForm(id), { + const deleteForm = useMutation(() => api.deleteObservationForm(id), { onError: () => { handleErrorFromServerToast(); }, @@ -46,7 +46,7 @@ export const useData = () => { deleteTitle: deleteTitles.form, deleteName: `#${observationForm?.id}`, deleteFunction: - observationForm?.status === StatusTypes.RETURNED ? removeUser.mutateAsync : undefined, + observationForm?.status === StatusTypes.RETURNED ? deleteForm.mutateAsync : undefined, }; const disabled = !!observationForm && !observationForm?.canEdit;