From b645eeb1b81ff15a1f2995245a93d0345568cdcc Mon Sep 17 00:00:00 2001 From: mbaharip <62494292+mbahArip@users.noreply.github.com> Date: Sun, 7 Apr 2024 00:35:16 +0700 Subject: [PATCH] Fix password handle --- src/app/@navbar.tsx | 4 +++- src/app/@password.tsx | 19 ++++++++++++++++--- src/app/[...rest]/page.tsx | 1 + src/app/actions.ts | 7 ++++--- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/app/@navbar.tsx b/src/app/@navbar.tsx index 77175df..0524176 100644 --- a/src/app/@navbar.tsx +++ b/src/app/@navbar.tsx @@ -3,7 +3,7 @@ import { TooltipTrigger } from "@radix-ui/react-tooltip"; import { useTheme } from "next-themes"; import Link from "next/link"; -import { usePathname } from "next/navigation"; +import { usePathname, useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import toast from "react-hot-toast"; import { cn } from "~/utils"; @@ -52,6 +52,7 @@ import { ClearPassword } from "./actions"; export default function Navbar() { const pathname = usePathname(); + const router = useRouter(); const isDesktop = useMediaQuery("(min-width: 768px)"); const { theme, themes, setTheme } = useTheme(); @@ -72,6 +73,7 @@ export default function Navbar() { loading: "Clearing all saved password...", success: () => { setOpen(false); + router.refresh(); return "All saved password cleared successfully!"; }, error: (error) => error.message, diff --git a/src/app/@password.tsx b/src/app/@password.tsx index 3e3d0e7..7bc2cac 100644 --- a/src/app/@password.tsx +++ b/src/app/@password.tsx @@ -9,18 +9,25 @@ import Icon from "~/components/Icon"; import { Button } from "~/components/ui/button"; import { Input } from "~/components/ui/input"; -import { CheckSitePassword, SetPassword, SetSitePassword } from "./actions"; +import { + CheckPassword, + CheckSitePassword, + SetPassword, + SetSitePassword, +} from "./actions"; type Props = { path: string; + checkPaths?: { path: string; id: string }[]; errorMessage?: string; }; -export default function Password({ path, errorMessage }: Props) { +export default function Password({ path, checkPaths, errorMessage }: Props) { const router = useRouter(); const [input, setInput] = useState(""); const [submitLoading, setSubmitLoading] = useState(false); const [loading, setLoading] = useState(true); + const onSubmit = async (e: React.FormEvent) => { e.preventDefault(); setSubmitLoading(true); @@ -37,13 +44,19 @@ export default function Password({ path, errorMessage }: Props) { const check = await CheckSitePassword(); if (!check.success) throw new Error(check.message); } else { + if (!checkPaths) + throw new Error("No path found, try to refresh the page."); const set = await SetPassword(path, input); if (!set.success) throw new Error(set.message); + + const check = await CheckPassword(checkPaths); + if (!check.success) throw new Error(check.message); } toast.success("Password accepted! Refreshing...", { id: "password", }); + console.log("Password accepted! Refreshing..."); router.refresh(); } catch (error) { const e = error as Error; @@ -51,9 +64,9 @@ export default function Password({ path, errorMessage }: Props) { toast.error(e.message, { id: "password", }); + setSubmitLoading(false); } finally { setInput(""); - setSubmitLoading(false); } }; diff --git a/src/app/[...rest]/page.tsx b/src/app/[...rest]/page.tsx index 42f946a..97baa60 100644 --- a/src/app/[...rest]/page.tsx +++ b/src/app/[...rest]/page.tsx @@ -88,6 +88,7 @@ export default async function RestPage({ params: { rest } }: Props) { return ( ); diff --git a/src/app/actions.ts b/src/app/actions.ts index 9a4ddd8..1dc7f91 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -256,9 +256,10 @@ export async function CheckPassword( }, ); if (savedPassword !== passwordFile) - throw new Error( - `Password for '${currentFolder.path}' is incorrect, please re-enter the password`, - ); + throw { + message: `Password for '${currentFolder.path}' is incorrect, please re-enter the password`, + path: currentFolder.id, + }; return { success: true,