From f24824250bba1c96bbbe49cddc24a8d61244a4ab Mon Sep 17 00:00:00 2001 From: Remko Date: Thu, 18 Apr 2024 11:47:42 +0200 Subject: [PATCH 1/3] Added reference to organization --- pwa/src/apiService/resources/organization.ts | 10 +++++++++ pwa/src/hooks/organization.ts | 21 ++++++++++++++++--- .../EditOrganizationTemplate.tsx | 9 ++++++++ .../organizationsForm/OrganizationForm.tsx | 17 ++++++++++++++- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/pwa/src/apiService/resources/organization.ts b/pwa/src/apiService/resources/organization.ts index fa979f6e..2f74cd98 100644 --- a/pwa/src/apiService/resources/organization.ts +++ b/pwa/src/apiService/resources/organization.ts @@ -28,6 +28,16 @@ export default class Organization { return data; }; + public delete = async (variables: { id: string }): Promise => { + const { id } = variables; + + const { data } = await this._send(this._instance, "DELETE", `/admin/organizations/${id}`, undefined, { + loading: "Removing organization...", + success: "Organization successfully removed.", + }); + return data; + }; + public createOrUpdate = async (variables: { payload: any; id?: string }): Promise => { const { payload, id } = variables; diff --git a/pwa/src/hooks/organization.ts b/pwa/src/hooks/organization.ts index 2531f715..51d075c9 100644 --- a/pwa/src/hooks/organization.ts +++ b/pwa/src/hooks/organization.ts @@ -2,11 +2,13 @@ import * as React from "react"; import { QueryClient, useMutation, useQuery } from "react-query"; import APIService from "../apiService/apiService"; import APIContext from "../apiService/apiContext"; -import { addItem, updateItem } from "../services/mutateQueries"; +import { addItem, deleteItem, updateItem } from "../services/mutateQueries"; import { navigate } from "gatsby"; +import { useDeletedItemsContext } from "../context/deletedItems"; export const useOrganization = (queryClient: QueryClient) => { const API: APIService | null = React.useContext(APIContext); + const { isDeleted, addDeletedItem, removeDeletedItem } = useDeletedItemsContext(); const getAll = () => useQuery("organizations", API.Organization.getAll, { @@ -29,7 +31,20 @@ export const useOrganization = (queryClient: QueryClient) => { onError: (error) => { console.warn(error.message); }, - enabled: !!organizationId, + enabled: !!organizationId && !isDeleted(organizationId), + }); + + const remove = () => + useMutation(API.Organization.delete, { + onMutate: ({ id }) => addDeletedItem(id), + onSuccess: async (_, variables) => { + deleteItem(queryClient, "organizations", variables.id); + navigate("/settings/organizations"); + }, + onError: (error, { id }) => { + removeDeletedItem(id); + console.warn(error.message); + }, }); const createOrEdit = (organizationId?: string) => @@ -50,5 +65,5 @@ export const useOrganization = (queryClient: QueryClient) => { }, }); - return { getAll, getAllSelectOptions, getOne, createOrEdit }; + return { getAll, getAllSelectOptions, getOne, createOrEdit, remove }; }; diff --git a/pwa/src/templates/templateParts/organizationsForm/EditOrganizationTemplate.tsx b/pwa/src/templates/templateParts/organizationsForm/EditOrganizationTemplate.tsx index df7399a9..ac5d6f54 100644 --- a/pwa/src/templates/templateParts/organizationsForm/EditOrganizationTemplate.tsx +++ b/pwa/src/templates/templateParts/organizationsForm/EditOrganizationTemplate.tsx @@ -35,6 +35,7 @@ export const EditOrganizationTemplate: React.FC const queryClient = useQueryClient(); const _useOrganizations = useOrganization(queryClient); const getOrganization = _useOrganizations.getOne(organizationId); + const deleteOrganization = _useOrganizations.remove(); const getLogs = useLog(queryClient).getAllFromChannel("organization", organizationId, currentLogsPage); @@ -54,6 +55,12 @@ export const EditOrganizationTemplate: React.FC ); }; + const handleDeleteDatabase = () => { + const confirmDeletion = confirm("Are you sure you want to delete this organization?"); + + confirmDeletion && deleteOrganization.mutate({ id: organizationId }); + }; + return ( {getOrganization.isSuccess && ( @@ -63,6 +70,8 @@ export const EditOrganizationTemplate: React.FC {...{ formId }} disabled={isLoading.organizationForm} handleToggleDashboard={{ handleToggle: toggleFromDashboard, isActive: !!dashboardCard }} + handleDelete={handleDeleteDatabase} + /> diff --git a/pwa/src/templates/templateParts/organizationsForm/OrganizationForm.tsx b/pwa/src/templates/templateParts/organizationsForm/OrganizationForm.tsx index d71a2558..1246e051 100644 --- a/pwa/src/templates/templateParts/organizationsForm/OrganizationForm.tsx +++ b/pwa/src/templates/templateParts/organizationsForm/OrganizationForm.tsx @@ -31,7 +31,7 @@ export const OrganizationForm: React.FC = ({ organization } = useForm(); const handleSetFormValues = (organization: any): void => { - const basicFields: string[] = ["name", "description"]; + const basicFields: string[] = ["name", "description", "reference"]; basicFields.forEach((field) => setValue(field, organization[field])); }; @@ -66,6 +66,21 @@ export const OrganizationForm: React.FC = ({ organization + + + {t("Reference")} + + + + + +
{t("Description")} From 29bdbbc03a974298d46bfba22f9ddcb1afbeb884 Mon Sep 17 00:00:00 2001 From: Remko Date: Thu, 18 Apr 2024 11:49:05 +0200 Subject: [PATCH 2/3] cleanup --- .../organizationsForm/EditOrganizationTemplate.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pwa/src/templates/templateParts/organizationsForm/EditOrganizationTemplate.tsx b/pwa/src/templates/templateParts/organizationsForm/EditOrganizationTemplate.tsx index ac5d6f54..d50db683 100644 --- a/pwa/src/templates/templateParts/organizationsForm/EditOrganizationTemplate.tsx +++ b/pwa/src/templates/templateParts/organizationsForm/EditOrganizationTemplate.tsx @@ -55,7 +55,7 @@ export const EditOrganizationTemplate: React.FC ); }; - const handleDeleteDatabase = () => { + const handleDeleteOrganization = () => { const confirmDeletion = confirm("Are you sure you want to delete this organization?"); confirmDeletion && deleteOrganization.mutate({ id: organizationId }); @@ -70,7 +70,7 @@ export const EditOrganizationTemplate: React.FC {...{ formId }} disabled={isLoading.organizationForm} handleToggleDashboard={{ handleToggle: toggleFromDashboard, isActive: !!dashboardCard }} - handleDelete={handleDeleteDatabase} + handleDelete={handleDeleteOrganization} /> From 7be7ed2d4dce7606e2161cf70d1f4c48d3aa2bd1 Mon Sep 17 00:00:00 2001 From: Remko Date: Thu, 18 Apr 2024 11:49:31 +0200 Subject: [PATCH 3/3] cleanup --- .../templateParts/organizationsForm/OrganizationForm.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pwa/src/templates/templateParts/organizationsForm/OrganizationForm.tsx b/pwa/src/templates/templateParts/organizationsForm/OrganizationForm.tsx index 1246e051..261d4cc5 100644 --- a/pwa/src/templates/templateParts/organizationsForm/OrganizationForm.tsx +++ b/pwa/src/templates/templateParts/organizationsForm/OrganizationForm.tsx @@ -73,7 +73,7 @@ export const OrganizationForm: React.FC = ({ organization {...{ register, errors }} name="reference" validation={enrichValidation({ required: true })} - disabled={isLoading.databaseForm} + disabled={isLoading.organizationForm} ariaLabel={t("Enter reference")} />