From 7ef30feb9e737487e5285e3bd521a413d23fde06 Mon Sep 17 00:00:00 2001 From: Akash Hamirwasia Date: Wed, 26 Jan 2022 08:14:04 +0000 Subject: [PATCH] Add show/hide secret value toggle button in new secret form --- pages/projects/[projectId]/_secrets.tsx | 78 ++++++++++++++++++------- 1 file changed, 58 insertions(+), 20 deletions(-) diff --git a/pages/projects/[projectId]/_secrets.tsx b/pages/projects/[projectId]/_secrets.tsx index afa2287..f7b41a7 100644 --- a/pages/projects/[projectId]/_secrets.tsx +++ b/pages/projects/[projectId]/_secrets.tsx @@ -1,9 +1,21 @@ import type { Project } from '@prisma/client'; -import { Box, Flex, Text, Button, IconButton, Tooltip, Input, useToast } from '@chakra-ui/react'; +import { + Box, + Flex, + Text, + Button, + IconButton, + Tooltip, + Input, + InputGroup, + InputRightElement, + useToast, +} from '@chakra-ui/react'; import { PlusIcon, TrashIcon } from '@heroicons/react/outline'; import { useState } from 'react'; import { useRouter } from 'next/router'; import axios from 'axios'; +import { useForm } from 'react-hook-form'; import { SectionHeading, confirmDialog } from '@/components'; @@ -18,36 +30,42 @@ type Props = { [key: string]: any; }; +type NewSecretFormData = { + name: string; + value: string; +}; + export default function Secrets({ project, ...props }: Props) { const [showNewForm, setShowNewForm] = useState(false); - const [newSecretName, setNewSecretName] = useState(''); - const [newSecretValue, setNewSecretValue] = useState(''); - const [creatingSecret, setCreatingSecret] = useState(false); const [deletingSecret, setDeletingSecret] = useState(''); + const [showValue, setShowValue] = useState(false); + const { + register, + getValues, + handleSubmit, + setValue, + formState: { isSubmitting: creatingSecret }, + } = useForm(); const toast = useToast(); const router = useRouter(); const closeCreation = () => { setShowNewForm(false); - setNewSecretName(''); - setNewSecretValue(''); + setValue('name', ''); + setValue('value', ''); }; - const createSecret = async (e) => { - e.preventDefault(); + const createSecret = async () => { if (creatingSecret) return; - if (project.Secret.some(({ name }) => name === newSecretName)) { + const newSecret = getValues(); + + if (project.Secret.some(({ name }) => name === newSecret.name)) { toast({ status: "error", title: "Secret with this name already exists" }); return; } - setCreatingSecret(true); - await axios.post(`/api/projects/${project.id}/secrets/create`, { - name: newSecretName, - value: newSecretValue, - }); - setCreatingSecret(false); + await axios.post(`/api/projects/${project.id}/secrets/create`, newSecret); closeCreation(); router.replace(router.asPath, undefined, { scroll: false }); }; @@ -115,13 +133,33 @@ export default function Secrets({ project, ...props }: Props) { ))} {showNewForm && ( - -
+ + - setNewSecretName(e.target.value)} /> - setNewSecretValue(e.target.value)} mx={8} /> + + + + + + + -