From 7f015e7d715aa7425f514ef93011784451228b27 Mon Sep 17 00:00:00 2001 From: garethgeorge Date: Sun, 12 Nov 2023 14:46:04 -0800 Subject: [PATCH] feat: implement delete button for plan and repo UIs --- webui/src/views/AddPlanModel.tsx | 64 ++++++++++++++++++++++++++-- webui/src/views/AddRepoModel.tsx | 72 ++++++++++++++++++++++++++++++-- 2 files changed, 130 insertions(+), 6 deletions(-) diff --git a/webui/src/views/AddPlanModel.tsx b/webui/src/views/AddPlanModel.tsx index 75c0b0eb..13150845 100644 --- a/webui/src/views/AddPlanModel.tsx +++ b/webui/src/views/AddPlanModel.tsx @@ -27,11 +27,49 @@ export const AddPlanModal = ({ template: Partial | null; }) => { const [config, setConfig] = useRecoilState(configState); + const [deleteConfirmed, setDeleteConfirmed] = useState(false); const [confirmLoading, setConfirmLoading] = useState(false); const showModal = useShowModal(); const alertsApi = useAlertApi()!; const [form] = Form.useForm(); + const handleDestroy = async () => { + if (!deleteConfirmed) { + setDeleteConfirmed(true); + setTimeout(() => { + setDeleteConfirmed(false); + }, 2000); + return; + } + + setConfirmLoading(true); + + try { + let config = await fetchConfig(); + config.plans = config.plans || []; + + if (!template) { + throw new Error("template not found"); + } + + // Remove the plan from the config + const idx = config.plans.findIndex((r) => r.id === template.id); + if (idx === -1) { + throw new Error("failed to update config, plan to delete not found"); + } + + config.plans.splice(idx, 1); + + // Update config and notify success. + setConfig(await updateConfig(config)); + showModal(null); + } catch (e: any) { + alertsApi.error("Operation failed: " + e.message, 15); + } finally { + setConfirmLoading(false); + } + }; + const handleOk = async () => { setConfirmLoading(true); @@ -73,9 +111,29 @@ export const AddPlanModal = ({ + Cancel + , + template != null ? ( + + ) : null, + , + ]} >
{/* Plan.id */} diff --git a/webui/src/views/AddRepoModel.tsx b/webui/src/views/AddRepoModel.tsx index 144c0ece..f02657b7 100644 --- a/webui/src/views/AddRepoModel.tsx +++ b/webui/src/views/AddRepoModel.tsx @@ -30,11 +30,57 @@ export const AddRepoModel = ({ template: Partial | null; }) => { const [config, setConfig] = useRecoilState(configState); + const [deleteConfirmed, setDeleteConfirmed] = useState(false); const [confirmLoading, setConfirmLoading] = useState(false); const showModal = useShowModal(); const alertsApi = useAlertApi()!; const [form] = Form.useForm(); + const handleDestroy = async () => { + if (!deleteConfirmed) { + setDeleteConfirmed(true); + setTimeout(() => { + setDeleteConfirmed(false); + }, 2000); + return; + } + + setConfirmLoading(true); + + try { + let config = await fetchConfig(); + config.repos = config.repos || []; + + if (!template) { + throw new Error("template not found"); + } + + // Check if still in use by a plan + for (const plan of config.plans || []) { + if (plan.repo === template.id) { + throw new Error("Can't delete repo, still in use by plan " + plan.id); + } + } + + // Remove the plan from the config + const idx = config.repos.findIndex((r) => r.id === template.id); + if (idx === -1) { + throw new Error("failed to update config, plan to delete not found"); + } + + config.repos.splice(idx, 1); + + // Update config and notify success. + setConfig(await updateConfig(config)); + showModal(null); + } catch (e: any) { + alertsApi.error("Operation failed: " + e.message, 15); + } finally { + setDeleteConfirmed(false); + setConfirmLoading(false); + } + }; + const handleOk = async () => { setConfirmLoading(true); @@ -83,9 +129,29 @@ export const AddRepoModel = ({ + Cancel + , + template != null ? ( + + ) : null, + , + ]} > {/* Repo.id */}