feat: implement delete secret via modal logic

This commit is contained in:
lemmyMwaura
2024-07-26 19:48:30 +03:00
parent 391ed0723e
commit 8fd3e50d04
@@ -1,3 +1,4 @@
import { useState, useCallback } from "react";
import { Controller, useForm } from "react-hook-form"; import { Controller, useForm } from "react-hook-form";
import { subject } from "@casl/ability"; import { subject } from "@casl/ability";
import { faCheck, faCopy, faTrash, faXmark } from "@fortawesome/free-solid-svg-icons"; import { faCheck, faCopy, faTrash, faXmark } from "@fortawesome/free-solid-svg-icons";
@@ -12,6 +13,8 @@ import { ProjectPermissionActions, ProjectPermissionSub } from "@app/context";
import { useToggle } from "@app/hooks"; import { useToggle } from "@app/hooks";
import { SecretType } from "@app/hooks/api/types"; import { SecretType } from "@app/hooks/api/types";
import { DeleteSecretModal } from "../ManageSecretModals";
type Props = { type Props = {
defaultValue?: string | null; defaultValue?: string | null;
secretName: string; secretName: string;
@@ -59,6 +62,11 @@ export const SecretEditRow = ({
} }
}); });
const [isDeleting, setIsDeleting] = useToggle(); const [isDeleting, setIsDeleting] = useToggle();
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const toggleModal = useCallback(() => {
setIsModalOpen((prev) => !prev)
}, [])
const handleFormReset = () => { const handleFormReset = () => {
reset(); reset();
@@ -94,18 +102,23 @@ export const SecretEditRow = ({
reset({ value }); reset({ value });
}; };
const handleDeleteSecret = async () => { const handleDeleteSecret = useCallback(async () => {
setIsDeleting.on(); setIsDeleting.on();
setIsModalOpen(false);
try { try {
await onSecretDelete(environment, secretName, secretId); await onSecretDelete(environment, secretName, secretId);
reset({ value: null }); reset({ value: null });
} finally { } finally {
setIsDeleting.off(); setIsDeleting.off();
} }
}; }, [onSecretDelete, environment, secretName, secretId, reset, setIsDeleting]);
return ( return (
<div className="group flex w-full cursor-text items-center space-x-2"> <div className="group flex w-full cursor-text items-center space-x-2">
<DeleteSecretModal isModalOpen={isModalOpen} toggleModal={toggleModal} handleDeleteSecret={handleDeleteSecret} />
<div className="flex-grow border-r border-r-mineshaft-600 pr-2 pl-1"> <div className="flex-grow border-r border-r-mineshaft-600 pr-2 pl-1">
<Controller <Controller
disabled={isImportedSecret && !defaultValue} disabled={isImportedSecret && !defaultValue}
@@ -193,7 +206,7 @@ export const SecretEditRow = ({
variant="plain" variant="plain"
ariaLabel="delete-value" ariaLabel="delete-value"
className="h-full" className="h-full"
onClick={handleDeleteSecret} onClick={toggleModal}
isDisabled={isDeleting || !isAllowed} isDisabled={isDeleting || !isAllowed}
> >
<FontAwesomeIcon icon={faTrash} /> <FontAwesomeIcon icon={faTrash} />