From 35e1e807368060cb6a453e9747680b87a633fb3d Mon Sep 17 00:00:00 2001 From: pheralb Date: Mon, 21 Nov 2022 15:51:33 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=92=EF=B8=8F=20Add=20new=20options:=20del?= =?UTF-8?q?ete=20&=20edit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/functions/create/index.tsx | 2 +- src/components/functions/delete/index.tsx | 112 ++++++++++++++++++++ src/components/functions/edit/index.tsx | 119 ++++++++++++++++++++++ 3 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 src/components/functions/delete/index.tsx create mode 100644 src/components/functions/edit/index.tsx diff --git a/src/components/functions/create/index.tsx b/src/components/functions/create/index.tsx index eb66d6f..426e4c9 100644 --- a/src/components/functions/create/index.tsx +++ b/src/components/functions/create/index.tsx @@ -29,7 +29,7 @@ const Create = () => { icon: "🥳", style: { borderRadius: "10px", - background: "#28283E", + background: "#121212", color: "#fff", }, }); diff --git a/src/components/functions/delete/index.tsx b/src/components/functions/delete/index.tsx new file mode 100644 index 0000000..9a4acd3 --- /dev/null +++ b/src/components/functions/delete/index.tsx @@ -0,0 +1,112 @@ +import { useState, useEffect } from "react"; +import { useForm } from "react-hook-form"; +import { trpc } from "@/utils/trpc"; +import { useRouter } from "next/router"; +import { Button } from "@/ui"; +import toast from "react-hot-toast"; +import Alert from "@/ui/alert"; +import { BiTrash } from "react-icons/bi"; +import { nanoid } from "nanoid"; + +interface DeleteProps { + id: number; +} + +const Delete = (props: DeleteProps) => { + const { + handleSubmit, + register, + getValues, + formState: { errors }, + } = useForm(); + const [loading, setLoading] = useState(false); + const router = useRouter(); + const [disabled, setDisabled] = useState(false); + const [validate, setValidate] = useState(String); + + useEffect(() => { + const random = nanoid(5); + setValidate(random); + if (validate === random) { + setDisabled(true); + } + }, []); + + const { mutate, error } = trpc.useMutation(["links.delete-link"], { + onSuccess: () => { + router.reload(); + setLoading(false); + toast("Link deleted successfully", { + icon: "🥳", + style: { + borderRadius: "10px", + background: "#121212", + color: "#fff", + }, + }); + }, + onError: () => { + setLoading(false); + alert("Error"); + }, + }); + + const onSubmit = () => { + if (validate === getValues("validate")) { + setLoading(true); + mutate({ + linkId: props.id, + }); + } else { + toast("The values do not match. Check the validation.", { + icon: "🤔", + style: { + borderRadius: "10px", + background: "#121212", + color: "#fff", + }, + }); + } + }; + + return ( +
+ {error && ( + +

{error.message}

+
+ )} + +

+ Are you sure you want to delete this link? This action is + irreversible. +

+
+
+

Enter the following to confirm:

+

{validate}

+
+ +
+
+
+ +
+
+ ); +}; + +export default Delete; diff --git a/src/components/functions/edit/index.tsx b/src/components/functions/edit/index.tsx new file mode 100644 index 0000000..98b9256 --- /dev/null +++ b/src/components/functions/edit/index.tsx @@ -0,0 +1,119 @@ +import { useState, useEffect } from "react"; +import { useForm } from "react-hook-form"; +import { EditLinkInput } from "@/schema/link.schema"; +import { trpc } from "@/utils/trpc"; +import { useRouter } from "next/router"; +import { Button } from "@/ui"; +import toast from "react-hot-toast"; +import Alert from "@/ui/alert"; +import { BiCheck } from "react-icons/bi"; + +interface EditProps { + id: number; + slug: string; + url: string; + description: string; +} + +const Edit = (props: EditProps) => { + const { + handleSubmit, + setValue, + register, + formState: { errors }, + } = useForm(); + const [loading, setLoading] = useState(false); + const router = useRouter(); + const [disabled, setDisabled] = useState(false); + + const { mutate, error } = trpc.useMutation(["links.edit-link"], { + onSuccess: () => { + router.reload(); + setLoading(false); + toast("Link edited successfully", { + icon: "🥳", + style: { + borderRadius: "10px", + background: "#121212", + color: "#fff", + }, + }); + }, + onError: () => { + setLoading(false); + alert("Error"); + }, + }); + + const onSubmit = (values: EditLinkInput) => { + setValue("slug", props.slug); + setLoading(true); + mutate({ + slug: props.slug, + url: values.url, + description: values.description, + }); + }; + + return ( +
+ {error && ( + +

{error.message}

+
+ )} +
+ + + {errors.url && {errors.url.message}} +
+
+ +