From f866250c25dcfef7424f3868a3e614a373fc5a47 Mon Sep 17 00:00:00 2001 From: Lorenzo Migliorero Date: Thu, 25 Jul 2024 15:19:03 +0200 Subject: [PATCH] feat: new table and crud operations --- .../settings/ssh-keys/add-ssh-key.tsx | 165 ++++++++++++++++++ .../settings/ssh-keys/delete-ssh-key.tsx | 61 +++++++ .../settings/ssh-keys/show-ssh-keys.tsx | 96 ++++++++++ .../settings/ssh-keys/update-ssh-key.tsx | 148 ++++++++++++++++ components/layouts/settings-layout.tsx | 7 + pages/dashboard/settings/ssh-keys.tsx | 41 +++++ server/api/root.ts | 2 + server/api/routers/ssh-key.ts | 63 +++++++ server/api/services/ssh-key.ts | 70 ++++++++ server/db/schema/index.ts | 1 + server/db/schema/ssh-key.ts | 57 ++++++ server/db/validations/index.ts | 22 +++ 12 files changed, 733 insertions(+) create mode 100644 components/dashboard/settings/ssh-keys/add-ssh-key.tsx create mode 100644 components/dashboard/settings/ssh-keys/delete-ssh-key.tsx create mode 100644 components/dashboard/settings/ssh-keys/show-ssh-keys.tsx create mode 100644 components/dashboard/settings/ssh-keys/update-ssh-key.tsx create mode 100644 pages/dashboard/settings/ssh-keys.tsx create mode 100644 server/api/routers/ssh-key.ts create mode 100644 server/api/services/ssh-key.ts create mode 100644 server/db/schema/ssh-key.ts create mode 100644 server/db/validations/index.ts diff --git a/components/dashboard/settings/ssh-keys/add-ssh-key.tsx b/components/dashboard/settings/ssh-keys/add-ssh-key.tsx new file mode 100644 index 000000000..4e761c4af --- /dev/null +++ b/components/dashboard/settings/ssh-keys/add-ssh-key.tsx @@ -0,0 +1,165 @@ +import { AlertBlock } from "@/components/shared/alert-block"; +import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { Textarea } from "@/components/ui/textarea"; +import { sshKeyCreate } from "@/server/db/validations"; +import { api } from "@/utils/api"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { type ReactNode, useState } from "react"; +import { flushSync } from "react-dom"; +import { useForm } from "react-hook-form"; +import { toast } from "sonner"; +import type { z } from "zod"; + +type SSHKey = z.infer; + +interface Props { + children: ReactNode; +} + +export const AddSSHKey = ({ children }: Props) => { + const utils = api.useUtils(); + + const [isOpen, setIsOpen] = useState(false); + + const { mutateAsync, isError, error, isLoading } = + api.sshKey.create.useMutation(); + + const form = useForm({ + resolver: zodResolver(sshKeyCreate), + }); + + const onSubmit = async (data: SSHKey) => { + await mutateAsync(data) + .then(async () => { + toast.success("SSH key created successfully"); + await utils.sshKey.all.invalidate(); + /* + Flushsync is needed for a bug witht he react-hook-form reset method + https://github.com/orgs/react-hook-form/discussions/7589#discussioncomment-10060621 + */ + flushSync(() => form.reset()); + setIsOpen(false); + }) + .catch(() => { + toast.error("Error to create the SSH key"); + }); + }; + + return ( + + + {children} + + + + SSH Key + + In this section you can add an SSH key + + + {isError && {error?.message}} + +
+ + { + return ( + + Name + + + + + + ); + }} + /> + + { + return ( + + Description + + + + + + ); + }} + /> + ( + +
+ Private Key +
+ +