From ef0b8985002d103682786ff478d69430012dfd6d Mon Sep 17 00:00:00 2001 From: Janaka-Steph Date: Thu, 24 Aug 2023 15:26:57 +0200 Subject: [PATCH] Delete registry (#332) --- src/modules/settings/api/deleteRegistry.ts | 13 ++++++++ .../settings/components/Registries.tsx | 33 +++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 src/modules/settings/api/deleteRegistry.ts diff --git a/src/modules/settings/api/deleteRegistry.ts b/src/modules/settings/api/deleteRegistry.ts new file mode 100644 index 00000000..826c1ca4 --- /dev/null +++ b/src/modules/settings/api/deleteRegistry.ts @@ -0,0 +1,13 @@ +import type { AxiosResponse } from "axios"; +import axios from "axios"; +import type { Message } from "modules/service/types"; + +import useSettingStore from "../../../shared/store/setting"; +import type { Registries } from "../types"; + +const deleteRegistry = async (data: Registries): Promise> => { + const backendUrl = useSettingStore.getState().backendUrl; + return axios.delete(`${backendUrl}/v1/registries/?url=${data.url}`); +}; + +export default deleteRegistry; diff --git a/src/modules/settings/components/Registries.tsx b/src/modules/settings/components/Registries.tsx index 866998a6..2e8682df 100644 --- a/src/modules/settings/components/Registries.tsx +++ b/src/modules/settings/components/Registries.tsx @@ -8,6 +8,7 @@ import PrimaryButton from "shared/components/PrimaryButton"; import Spinner from "shared/components/Spinner"; import addRegistry from "../api/addRegistry"; +import deleteRegistry from "../api/deleteRegistry"; import fetchRegistries from "../api/fetchRegistries"; const Registries = () => { @@ -15,11 +16,14 @@ const Registries = () => { const { isLoading, data: response, refetch } = useQuery(["registries"], fetchRegistries); - const { mutate, isLoading: isSubmitting } = useMutation(addRegistry); + const { mutate: mutateAddRegistry, isLoading: isLoadingAddRegistry } = useMutation(addRegistry); + const { mutate: mutateDeleteRegistry } = useMutation(deleteRegistry); + + const registries = response?.data || []; const onSubmit = (e: FormEvent) => { e.preventDefault(); - mutate( + mutateAddRegistry( { url: registryUrl }, { onSuccess: () => { @@ -34,7 +38,20 @@ const Registries = () => { ); }; - const registries = response?.data || []; + const handleDelete = (url: string) => { + mutateDeleteRegistry( + { url }, + { + onSuccess: () => { + refetch(); + toast.success("Registry deleted successfully"); + }, + onError: () => { + toast.error("Something went wrong while deleting registry"); + }, + }, + ); + }; return (
@@ -59,7 +76,11 @@ const Registries = () => { defaultValue={registry.url} readOnly /> - @@ -84,9 +105,9 @@ const Registries = () => { - {isSubmitting ? "Updating..." : "Add Registry"} + {isLoadingAddRegistry ? "Updating..." : "Add Registry"}