From 05f631ca80b58d4f0c2252aaf97cd7b6032698de Mon Sep 17 00:00:00 2001 From: Nathan Franklin Date: Mon, 11 Nov 2024 18:11:16 -0600 Subject: [PATCH 1/5] Refactor useDelete and use params --- .../DeleteMapModal/DeleteMapModal.tsx | 4 +- react/src/hooks/features/useDeleteFeature.ts | 4 +- react/src/hooks/projects/useProjects.ts | 12 ++++-- react/src/requests.ts | 39 ++----------------- 4 files changed, 15 insertions(+), 44 deletions(-) diff --git a/react/src/components/DeleteMapModal/DeleteMapModal.tsx b/react/src/components/DeleteMapModal/DeleteMapModal.tsx index ee814844..4fb2db1e 100644 --- a/react/src/components/DeleteMapModal/DeleteMapModal.tsx +++ b/react/src/components/DeleteMapModal/DeleteMapModal.tsx @@ -20,13 +20,13 @@ const DeleteMapModal = ({ isLoading: isDeletingProject, isError, isSuccess, - } = useDeleteProject(project.id); + } = useDeleteProject(); const handleClose = () => { parentToggle(); }; const handleDeleteProject = () => { - deleteProject(undefined, {}); + deleteProject({ projectId: project.id }); }; return ( diff --git a/react/src/hooks/features/useDeleteFeature.ts b/react/src/hooks/features/useDeleteFeature.ts index 990a6cc1..63252e86 100644 --- a/react/src/hooks/features/useDeleteFeature.ts +++ b/react/src/hooks/features/useDeleteFeature.ts @@ -1,5 +1,5 @@ import { useQueryClient } from 'react-query'; -import { useDeleteWithParams } from '@hazmapper/requests'; +import { useDelete } from '@hazmapper/requests'; type DeleteFeatureParams = { projectId: number; @@ -9,7 +9,7 @@ type DeleteFeatureParams = { export function useDeleteFeature() { const queryClient = useQueryClient(); - return useDeleteWithParams({ + return useDelete({ endpoint: ({ projectId, featureId }) => `/projects/${projectId}/features/${featureId}/`, options: { diff --git a/react/src/hooks/projects/useProjects.ts b/react/src/hooks/projects/useProjects.ts index 025cbc91..486ced4d 100644 --- a/react/src/hooks/projects/useProjects.ts +++ b/react/src/hooks/projects/useProjects.ts @@ -81,11 +81,15 @@ export function useProjectsWithDesignSafeInformation(): UseQueryResult< } as UseQueryResult; } -export const useDeleteProject = (projectId: number) => { +type DeleteProjectParams = { + projectId: number; +}; + +export const useDeleteProject = () => { const queryClient = useQueryClient(); - const endpoint = `/projects/${projectId}/`; - return useDelete({ - endpoint, + + return useDelete({ + endpoint: ({ projectId }) => `/projects/${projectId}/`, apiService: ApiService.Geoapi, options: { onSuccess: () => { diff --git a/react/src/requests.ts b/react/src/requests.ts index 11dc937a..ff1d6881 100644 --- a/react/src/requests.ts +++ b/react/src/requests.ts @@ -157,40 +157,7 @@ export function usePost({ return useMutation(postUtil, options); } -type UseDeleteParams = { - endpoint: string; - options?: UseMutationOptions; - apiService?: ApiService; -}; - -export function useDelete({ - endpoint, - options = {}, - apiService = ApiService.Geoapi, -}: UseDeleteParams) { - const client = axios; - const state = store.getState(); - const configuration = useAppConfiguration(); - - useEnsureAuthenticatedUserHasValidTapisToken(); - - const baseUrl = getBaseApiUrl(apiService, configuration); - const headers = getHeaders(apiService, state.auth); - - const deleteUtil = async () => { - const response = await client.delete( - `${baseUrl}${endpoint}`, - { - headers: headers, - } - ); - return response.data; - }; - - return useMutation(deleteUtil, options); -} - -type UseDeleteWithParams = { +type UseDeleteParams = { endpoint: string | ((variables: Variables) => string); options?: Omit< UseMutationOptions, @@ -199,11 +166,11 @@ type UseDeleteWithParams = { apiService?: ApiService; }; -export function useDeleteWithParams({ +export function useDelete({ endpoint, options = {}, apiService = ApiService.Geoapi, -}: UseDeleteWithParams) { +}: UseDeleteParams) { const client = axios; const state = store.getState(); const configuration = useAppConfiguration(); From 8364d1735380403ad7f5884e0859cca4797a0662 Mon Sep 17 00:00:00 2001 From: Nathan Franklin Date: Mon, 11 Nov 2024 18:36:22 -0600 Subject: [PATCH 2/5] Rename isPublic to isPublicView for clarity --- react/src/AppRouter.tsx | 2 +- .../components/AssetsPanel/AssetsPanel.tsx | 10 ++++----- .../FeatureFileTree/FeatureFileTree.test.tsx | 6 ++--- .../FeatureFileTree/FeatureFileTree.tsx | 6 ++--- .../ManageMapProjectModal.tsx | 6 ++--- .../MapProjectNavBar.test.tsx | 4 ++-- .../MapProjectNavBar/MapProjectNavBar.tsx | 6 ++--- react/src/hooks/features/useFeatures.ts | 9 ++++---- react/src/hooks/projects/useProjects.ts | 6 ++--- react/src/hooks/tileServers/useTileServers.ts | 8 +++---- react/src/pages/MapProject/MapProject.tsx | 22 +++++++++---------- 11 files changed, 43 insertions(+), 42 deletions(-) diff --git a/react/src/AppRouter.tsx b/react/src/AppRouter.tsx index dab56191..5fe9e83b 100644 --- a/react/src/AppRouter.tsx +++ b/react/src/AppRouter.tsx @@ -65,7 +65,7 @@ function AppRouter() { } /> - } /> + } /> } /> = ({ - isPublic, + isPublicView, featureCollection, projectId, }) => { @@ -81,8 +81,8 @@ const AssetsPanel: React.FC = ({
diff --git a/react/src/components/FeatureFileTree/FeatureFileTree.test.tsx b/react/src/components/FeatureFileTree/FeatureFileTree.test.tsx index acf52002..9f9b40f1 100644 --- a/react/src/components/FeatureFileTree/FeatureFileTree.test.tsx +++ b/react/src/components/FeatureFileTree/FeatureFileTree.test.tsx @@ -26,7 +26,7 @@ const renderWithRouter = (ui: React.ReactElement, { route = '/' } = {}) => { describe('FeatureFileTree', () => { const defaultProps = { featureCollection: featureCollection, - isPublic: false, + isPublicView: false, projectId: 1, }; @@ -73,7 +73,7 @@ describe('FeatureFileTree', () => { it('does not show delete button for public projects', () => { const { queryByTestId } = renderWithRouter( - , + , { route: '/?selectedFeature=1' } ); @@ -84,7 +84,7 @@ describe('FeatureFileTree', () => { it('does not show delete button when no feature is selected', () => { const { queryByTestId } = renderWithRouter( - + ); // Verify delete button is not present diff --git a/react/src/components/FeatureFileTree/FeatureFileTree.tsx b/react/src/components/FeatureFileTree/FeatureFileTree.tsx index 1652597f..c41d17a0 100644 --- a/react/src/components/FeatureFileTree/FeatureFileTree.tsx +++ b/react/src/components/FeatureFileTree/FeatureFileTree.tsx @@ -31,7 +31,7 @@ interface FeatureFileTreeProps { /** * Whether or not the map project is public. */ - isPublic: boolean; + isPublicView: boolean; /** * active project id @@ -44,7 +44,7 @@ interface FeatureFileTreeProps { */ const FeatureFileTree: React.FC = ({ featureCollection, - isPublic, + isPublicView, projectId, }) => { const { mutate: deleteFeature, isLoading } = useDeleteFeature(); @@ -169,7 +169,7 @@ const FeatureFileTree: React.FC = ({ )} {featureNode.name} - {!isPublic && isSelected && ( + {!isPublicView && isSelected && (