From 1c0570d1a8b4abafc5b4aec5be04354b4accd019 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 8 Jun 2025 13:12:51 +0200 Subject: [PATCH] Fix additional use of localstorage in the server side --- app/[slug]/dashboard/guests/page.tsx | 19 ++++++++----------- app/ui/arrangements/arrangement.tsx | 1 - app/ui/dashboard/nav-links.tsx | 16 ++++++++++------ app/ui/dashboard/sidenav.tsx | 14 +++++++++++--- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/app/[slug]/dashboard/guests/page.tsx b/app/[slug]/dashboard/guests/page.tsx index 0859d21..137cd10 100644 --- a/app/[slug]/dashboard/guests/page.tsx +++ b/app/[slug]/dashboard/guests/page.tsx @@ -22,28 +22,31 @@ import { Invitation, InvitationSerializer } from '@/app/lib/invitation'; export default function Page() { const [slug, setSlug] = useState("default"); - useEffect(() => { setSlug(getSlug()) }, []); + + useEffect(() => { + setSlug(getSlug()); + refreshGroups(); + refreshGuests(); + refreshInvitations(); + }, []); const toast = useRef(null); function refreshGuests() { new AbstractApi().getAll(new GuestSerializer(), (objects: Guest[]) => { setGuests(objects); - setGuestsLoaded(true); }); } function refreshGroups() { new AbstractApi().getAll(new GroupSerializer(), (objects: Group[]) => { setGroups(objects); - setGroupsLoaded(true); }); } function refreshInvitations() { new AbstractApi().getAll(new InvitationSerializer(), (objects: Invitation[]) => { setInvitations(objects); - setInvitationsLoaded(true); }); } @@ -75,23 +78,17 @@ export default function Page() { }); } - const [groupsLoaded, setGroupsLoaded] = useState(false); const [groups, setGroups] = useState>([]); const [groupBeingEdited, setGroupBeingEdited] = useState(undefined); const [groupAffinitiesBeingEditted, setGroupAffinitiesBeingEditted] = useState(undefined); - const [guestsLoaded, setGuestsLoaded] = useState(false); const [guests, setGuests] = useState>([]); const [guestBeingEdited, setGuestBeingEdited] = useState(undefined); - const [invitationsLoaded, setInvitationsLoaded] = useState(false); const [invitations, setInvitations] = useState>([]); - const [invitationBeingEdited, setInvitationBeingEdited] = useState(undefined); - !groupsLoaded && refreshGroups(); - !guestsLoaded && refreshGuests(); - !invitationsLoaded && refreshInvitations(); + return (
diff --git a/app/ui/arrangements/arrangement.tsx b/app/ui/arrangements/arrangement.tsx index d8bf0b1..d2ec567 100644 --- a/app/ui/arrangements/arrangement.tsx +++ b/app/ui/arrangements/arrangement.tsx @@ -5,7 +5,6 @@ import { AbstractApi } from '@/app/api/abstract-api'; import { TableArrangement } from '@/app/lib/definitions'; import { TableSimulation, TableSimulationSerializer } from '@/app/lib/tableSimulation'; -import { getSlug } from '@/app/lib/utils'; import { Table } from '@/app/ui/components/table'; import { lusitana } from '@/app/ui/fonts'; import { useState, useEffect } from 'react'; diff --git a/app/ui/dashboard/nav-links.tsx b/app/ui/dashboard/nav-links.tsx index ef3e4ea..336d798 100644 --- a/app/ui/dashboard/nav-links.tsx +++ b/app/ui/dashboard/nav-links.tsx @@ -11,19 +11,23 @@ import Link from 'next/link'; import { usePathname } from 'next/navigation'; import clsx from 'clsx'; import { getSlug } from '@/app/lib/utils'; +import { useEffect, useState } from 'react'; // Map of links to display in the side navigation. // Depending on the size of the application, this would be stored in a database. -const links = [ - { name: 'Guests', href: `/${getSlug()}/dashboard/guests`, icon: UserGroupIcon }, - { name: 'Expenses', href: `/${getSlug()}/dashboard/expenses`, icon: BanknotesIcon }, - { name: 'Table distributions', href: `/${getSlug()}/dashboard/tables`, icon: RectangleGroupIcon }, -]; - export default function NavLinks() { const pathname = usePathname(); + const [slug, setSlug] = useState("default"); + useEffect(() => { setSlug(getSlug()) }, []); + + const links = [ + { name: 'Guests', href: `/${slug}/dashboard/guests`, icon: UserGroupIcon }, + { name: 'Expenses', href: `/${slug}/dashboard/expenses`, icon: BanknotesIcon }, + { name: 'Table distributions', href: `/${slug}/dashboard/tables`, icon: RectangleGroupIcon }, + ]; + return ( <> {links.map((link) => { diff --git a/app/ui/dashboard/sidenav.tsx b/app/ui/dashboard/sidenav.tsx index fbba770..067de4b 100644 --- a/app/ui/dashboard/sidenav.tsx +++ b/app/ui/dashboard/sidenav.tsx @@ -9,15 +9,23 @@ import { gloriaHallelujah } from '@/app/ui/fonts'; import { logout } from '@/app/api/authentication'; import { useRouter } from 'next/navigation'; import { getSlug } from '@/app/lib/utils'; +import { useEffect, useState } from 'react'; export default function SideNav() { const router = useRouter(); + const [slug, setSlug] = useState("default"); + useEffect(() => { setSlug(getSlug()) }, []); + + const [currentUser, setCurrentUser] = useState<{ email: string } | null>(null); + useEffect(() => { setCurrentUser(JSON.parse(localStorage.getItem('currentUser') || '{}')) }, []); + + return (

Wedding Planner

@@ -26,14 +34,14 @@ export default function SideNav() {
- Logged in as {JSON.parse(localStorage.getItem('currentUser') || '{}').email} + Logged in as {currentUser?.email}