Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete form when form status is returned #26

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,12 @@ class Api {
});
};

deleteObservationForm = async (id: string): Promise<Form> =>
await this.delete({
resource: Resources.FORMS,
id,
});

getTenantUsers = async ({
filter,
page,
Expand Down
4 changes: 1 addition & 3 deletions src/components/other/DeleteCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ const DeleteCard = ({
</IconContainer>
<Title>{title}</Title>
<Description>
{descriptionFirstPart}
<Name>{name}</Name>
{descriptionSecondPart}
{descriptionFirstPart} <Name>{name}</Name> {descriptionSecondPart}
</Description>
<BottomRow>
<StyledButton
Expand Down
28 changes: 28 additions & 0 deletions src/pages/ObservationForm/hooks/useData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { useMutation, useQuery } from 'react-query';
import { useNavigate, useParams } from 'react-router-dom';
import { DeleteInfoProps } from '../../../types';
import {
buttonsTitles,
deleteDescriptionFirstPart,
deleteDescriptionSecondPart,
deleteTitles,
} from '../../../utils/texts';
import { getMapQueryString } from '../functions';
import { FormProps, FormServerProps } from '../types';
import { default as api, default as Api } from './../../../api';
Expand All @@ -22,6 +29,26 @@ export const useData = () => {
},
);

const deleteForm = 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 ? deleteForm.mutateAsync : undefined,
};

const disabled = !!observationForm && !observationForm?.canEdit;
const mapQueryString = getMapQueryString(disabled);

Expand Down Expand Up @@ -111,6 +138,7 @@ export const useData = () => {

return {
handleSubmit,
deleteInfo,
loading: isFetching || specieLoading,
mapQueryString,
initialValues,
Expand Down
11 changes: 10 additions & 1 deletion src/pages/ObservationForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -120,6 +128,7 @@ const ObservationForm = () => {
validationSchema={validateForm}
disabled={disabled}
submitButtonText={buttonsTitles.submit}
deleteInfo={deleteInfo}
/>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/pages/RequestForm/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/pages/TenantUserForm.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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}`,
Expand Down
8 changes: 5 additions & 3 deletions src/utils/texts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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šą?',
};

Expand Down
Loading