Skip to content

Commit

Permalink
Refactor useDelete and use params
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfranklin committed Nov 12, 2024
1 parent 299d436 commit 05f631c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 44 deletions.
4 changes: 2 additions & 2 deletions react/src/components/DeleteMapModal/DeleteMapModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions react/src/hooks/features/useDeleteFeature.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useQueryClient } from 'react-query';
import { useDeleteWithParams } from '@hazmapper/requests';
import { useDelete } from '@hazmapper/requests';

type DeleteFeatureParams = {
projectId: number;
Expand All @@ -9,7 +9,7 @@ type DeleteFeatureParams = {
export function useDeleteFeature() {
const queryClient = useQueryClient();

return useDeleteWithParams<void, DeleteFeatureParams>({
return useDelete<void, DeleteFeatureParams>({
endpoint: ({ projectId, featureId }) =>
`/projects/${projectId}/features/${featureId}/`,
options: {
Expand Down
12 changes: 8 additions & 4 deletions react/src/hooks/projects/useProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,15 @@ export function useProjectsWithDesignSafeInformation(): UseQueryResult<
} as UseQueryResult<Project[]>;
}

export const useDeleteProject = (projectId: number) => {
type DeleteProjectParams = {
projectId: number;
};

export const useDeleteProject = () => {
const queryClient = useQueryClient();
const endpoint = `/projects/${projectId}/`;
return useDelete<void>({
endpoint,

return useDelete<void, DeleteProjectParams>({
endpoint: ({ projectId }) => `/projects/${projectId}/`,
apiService: ApiService.Geoapi,
options: {
onSuccess: () => {
Expand Down
39 changes: 3 additions & 36 deletions react/src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,40 +157,7 @@ export function usePost<RequestType, ResponseType>({
return useMutation<ResponseType, AxiosError, RequestType>(postUtil, options);
}

type UseDeleteParams<ResponseType> = {
endpoint: string;
options?: UseMutationOptions<ResponseType, AxiosError>;
apiService?: ApiService;
};

export function useDelete<ResponseType>({
endpoint,
options = {},
apiService = ApiService.Geoapi,
}: UseDeleteParams<ResponseType>) {
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<ResponseType>(
`${baseUrl}${endpoint}`,
{
headers: headers,
}
);
return response.data;
};

return useMutation<ResponseType, AxiosError>(deleteUtil, options);
}

type UseDeleteWithParams<ResponseType, Variables> = {
type UseDeleteParams<ResponseType, Variables> = {
endpoint: string | ((variables: Variables) => string);
options?: Omit<
UseMutationOptions<ResponseType, AxiosError, Variables>,
Expand All @@ -199,11 +166,11 @@ type UseDeleteWithParams<ResponseType, Variables> = {
apiService?: ApiService;
};

export function useDeleteWithParams<ResponseType, Variables>({
export function useDelete<ResponseType, Variables>({
endpoint,
options = {},
apiService = ApiService.Geoapi,
}: UseDeleteWithParams<ResponseType, Variables>) {
}: UseDeleteParams<ResponseType, Variables>) {
const client = axios;
const state = store.getState();
const configuration = useAppConfiguration();
Expand Down

0 comments on commit 05f631c

Please sign in to comment.