From 56770e3b6c372337754ea1c2962b0e3db2d89169 Mon Sep 17 00:00:00 2001 From: Matthew <92887765+mjanderson1227@users.noreply.github.com> Date: Thu, 26 Dec 2024 14:20:28 -0600 Subject: [PATCH 1/4] Created a new action for editing navigation items --- apps/web/src/actions/admin/modify-nav-item.ts | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/apps/web/src/actions/admin/modify-nav-item.ts b/apps/web/src/actions/admin/modify-nav-item.ts index 5dc3a48e..740aacb7 100644 --- a/apps/web/src/actions/admin/modify-nav-item.ts +++ b/apps/web/src/actions/admin/modify-nav-item.ts @@ -7,7 +7,12 @@ import { revalidatePath } from "next/cache"; const metadataSchema = z.object({ name: z.string().min(1), - url: z.string(), + url: z.string().min(1), +}); + +const editMetadataSchema = metadataSchema.extend({ + existingName: z.string().min(1), + enabled: z.boolean(), }); // Maybe a better way to do this for revalidation? Who knows. @@ -26,6 +31,28 @@ export const setItem = adminAction return { success: true }; }); +export const editItem = adminAction + .schema(editMetadataSchema) + .action(async ({ parsedInput: { name, url, existingName } }) => { + const pipe = kv.pipeline(); + + if (existingName != name) { + pipe.srem("config:navitemslist", encodeURIComponent(existingName)); + } + + pipe.sadd("config:navitemslist", encodeURIComponent(name)); + pipe.hset(`config:navitems:${encodeURIComponent(name)}`, { + url, + name, + enabled: true, + }); + + await pipe.exec(); + + revalidatePath(navAdminPage); + return { success: true }; + }); + export const removeItem = adminAction .schema(z.string()) .action(async ({ parsedInput: name, ctx: { user, userId } }) => { From 1ba817e19215646f550daf7ab7ef7e844c8c686d Mon Sep 17 00:00:00 2001 From: Matthew <92887765+mjanderson1227@users.noreply.github.com> Date: Thu, 26 Dec 2024 14:21:42 -0600 Subject: [PATCH 2/4] Adds ability to edit navigation items from the admin dashboard --- .../src/app/admin/toggles/landing/page.tsx | 4 +- .../admin/toggles/NavItemsManager.tsx | 110 +++++++++++++++++- 2 files changed, 107 insertions(+), 7 deletions(-) diff --git a/apps/web/src/app/admin/toggles/landing/page.tsx b/apps/web/src/app/admin/toggles/landing/page.tsx index 3cc22a64..7d47f644 100644 --- a/apps/web/src/app/admin/toggles/landing/page.tsx +++ b/apps/web/src/app/admin/toggles/landing/page.tsx @@ -1,6 +1,6 @@ import { NavItemsManager, - NavItemDialog, + AddNavItemDialog, } from "@/components/admin/toggles/NavItemsManager"; import { getAllNavItems } from "@/lib/utils/server/redis"; @@ -13,7 +13,7 @@ export default async function Page() { Navbar Items
- +
{ toast.success("NavItem deleted successfully!"); }, + onError: () => { + toast.error("Error deleting NavItem"); + }, }); return ( @@ -79,10 +83,12 @@ export function NavItemsManager({ navItems }: NavItemsManagerProps) { name={item.name} /> - - + + + + + + Edit Item + + Edit an existing item shown in the non-dashboard navbar + + +
+
+ + setName(e.target.value)} + id="name" + placeholder="A Cool Hyperlink" + className="col-span-3" + value={name} + /> +
+
+ + setUrl(e.target.value)} + id="name" + placeholder="https://example.com/" + className="col-span-3" + value={url} + /> +
+
+ + + +
+ + ); +} From 8ca33955f859d9e7d0bda63cd1d83e0aad00f1f8 Mon Sep 17 00:00:00 2001 From: Christian Walker Date: Thu, 9 Jan 2025 01:46:26 -0600 Subject: [PATCH 3/4] add extra feedback --- .../admin/toggles/NavItemsManager.tsx | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/apps/web/src/components/admin/toggles/NavItemsManager.tsx b/apps/web/src/components/admin/toggles/NavItemsManager.tsx index b02312f6..240fd04e 100644 --- a/apps/web/src/components/admin/toggles/NavItemsManager.tsx +++ b/apps/web/src/components/admin/toggles/NavItemsManager.tsx @@ -22,7 +22,7 @@ import { import { Button } from "@/components/shadcn/ui/button"; import { Input } from "@/components/shadcn/ui/input"; import { Label } from "@/components/shadcn/ui/label"; -import { Plus } from "lucide-react"; +import { Plus, Loader2 } from "lucide-react"; import { useState } from "react"; import { useAction, useOptimisticAction } from "next-safe-action/hooks"; import { @@ -91,6 +91,8 @@ export function NavItemsManager({ navItems }: NavItemsManagerProps) { /> @@ -222,7 +235,7 @@ function EditNavItemDialog({ const [url, setUrl] = useState(existingUrl); const [open, setOpen] = useState(false); - const { execute } = useAction(editItem, { + const { execute, status: editStatus } = useAction(editItem, { onSuccess: () => { console.log("Success"); setOpen(false); @@ -232,6 +245,7 @@ function EditNavItemDialog({ toast.error("Error editing NavItem"); }, }); + const isLoading = editStatus === "executing"; return ( @@ -273,6 +287,7 @@ function EditNavItemDialog({ From 8675c485cd88d2484cd8c030ab8e8951c4a372e5 Mon Sep 17 00:00:00 2001 From: Christian Walker Date: Thu, 9 Jan 2025 12:27:29 -0600 Subject: [PATCH 4/4] fix toast loading --- apps/web/src/components/admin/toggles/NavItemsManager.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/web/src/components/admin/toggles/NavItemsManager.tsx b/apps/web/src/components/admin/toggles/NavItemsManager.tsx index 240fd04e..237590d6 100644 --- a/apps/web/src/components/admin/toggles/NavItemsManager.tsx +++ b/apps/web/src/components/admin/toggles/NavItemsManager.tsx @@ -42,9 +42,11 @@ interface NavItemsManagerProps { export function NavItemsManager({ navItems }: NavItemsManagerProps) { const { execute, result, status } = useAction(removeItem, { onSuccess: () => { + toast.dismiss(); toast.success("NavItem deleted successfully!"); }, onError: () => { + toast.dismiss(); toast.error("Error deleting NavItem"); }, });