From 82b96ff148c28142e8ef7c5c63cec7ec0c692828 Mon Sep 17 00:00:00 2001 From: rohan-chaturvedi Date: Thu, 16 Nov 2023 12:51:23 +0530 Subject: [PATCH] feat: handle isActive field, misc ux improvements --- .../components/environments/SecretRow.tsx | 111 +++++++++++++++--- 1 file changed, 92 insertions(+), 19 deletions(-) diff --git a/frontend/components/environments/SecretRow.tsx b/frontend/components/environments/SecretRow.tsx index bd8550e29..b8f9aeafe 100644 --- a/frontend/components/environments/SecretRow.tsx +++ b/frontend/components/environments/SecretRow.tsx @@ -22,7 +22,7 @@ import { FaUserEdit, } from 'react-icons/fa' import { Button } from '../common/Button' -import { Dialog, Transition } from '@headlessui/react' +import { Dialog, Switch, Transition } from '@headlessui/react' import { GetSecretTags } from '@/graphql/queries/secrets/getSecretTags.gql' import { CreateNewSecretTag } from '@/graphql/mutations/environments/createSecretTag.gql' import { LogSecretRead } from '@/graphql/mutations/environments/readSecret.gql' @@ -485,13 +485,21 @@ const OverrideDialog = (props: { }) => { const { secretId, secretName, environment, override } = props - const [createOverride] = useMutation(CreateNewPersonalSecret) - const [removeOverride] = useMutation(RemovePersonalSecret) + const [createOverride, { loading: createLoading }] = useMutation(CreateNewPersonalSecret) + const [removeOverride, { loading: removeLoading }] = useMutation(RemovePersonalSecret) const [value, setValue] = useState(override?.value || '') + const [isActive, setIsActive] = useState(override ? override.isActive : true) + const [saved, setSaved] = useState(true) const [isOpen, setIsOpen] = useState(false) + const reset = () => { + console.log('reset') + setValue(override?.value || '') + setIsActive(override?.isActive || true) + } + const closeModal = () => { setIsOpen(false) } @@ -500,14 +508,37 @@ const OverrideDialog = (props: { setIsOpen(true) } + const valueUpdated = () => { + if (override === null && value === '') return false + else { + return override?.value !== value + } + } + + const isActiveUpdated = () => { + if (override === null && value === '') return false + else { + return isActive !== override?.isActive + } + } + + const saveRequired = valueUpdated() || isActiveUpdated() + + useEffect(() => { + if (saveRequired) setSaved(false) + }, [saveRequired]) + + const toggleIsActive = () => setIsActive(!isActive) + const handleDeleteOverride = async () => { await removeOverride({ variables: { secretId, }, }) - setValue('') + reset() toast.success('Removed personal secret') + closeModal() } const handleUpdateOverride = async () => { @@ -519,25 +550,37 @@ const OverrideDialog = (props: { newPersonalSecret: { secretId, value: encryptedValue, + isActive, }, }, }) toast.success('Saved personal secret') + setSaved(true) + closeModal() } } const handleClose = () => { + if (saveRequired && !saved) reset() closeModal() } + const activeOverride = override?.value?.length && override?.isActive + return ( <>
-
@@ -587,7 +630,7 @@ const OverrideDialog = (props: { -
+
-
- {override && ( - + )} + - )} - +
@@ -820,7 +893,7 @@ export default function SecretRow(props: {