update user state variable on change

This commit is contained in:
Oliver Bryan
2026-01-06 14:50:40 +00:00
parent 15c7320833
commit b7a53d2d8a
2 changed files with 16 additions and 4 deletions

View File

@@ -23,7 +23,9 @@ import { ResizablePanel, ResizablePanelGroup, ResizableSeparator } from "@/compo
import { issue, organisation, project } from "@/lib/server"; import { issue, organisation, project } from "@/lib/server";
function Index() { function Index() {
const user = JSON.parse(localStorage.getItem("user") || "{}") as UserRecord; const userData = JSON.parse(localStorage.getItem("user") || "{}") as UserRecord;
const [user, setUser] = useState<UserRecord>(userData);
const organisationsRef = useRef(false); const organisationsRef = useRef(false);
const [organisations, setOrganisations] = useState<OrganisationResponse[]>([]); const [organisations, setOrganisations] = useState<OrganisationResponse[]>([]);
@@ -35,6 +37,11 @@ function Index() {
const [issues, setIssues] = useState<IssueResponse[]>([]); const [issues, setIssues] = useState<IssueResponse[]>([]);
const [selectedIssue, setSelectedIssue] = useState<IssueResponse | null>(null); const [selectedIssue, setSelectedIssue] = useState<IssueResponse | null>(null);
const refetchUser = async () => {
const userData = JSON.parse(localStorage.getItem("user") || "{}") as UserRecord;
setUser(userData);
};
const refetchOrganisations = async (options?: { selectOrganisationId?: number }) => { const refetchOrganisations = async (options?: { selectOrganisationId?: number }) => {
try { try {
await organisation.byUser({ await organisation.byUser({
@@ -220,7 +227,11 @@ function Index() {
</DropdownMenuTrigger> </DropdownMenuTrigger>
<DropdownMenuContent align={"end"}> <DropdownMenuContent align={"end"}>
<DropdownMenuItem asChild className="flex items-end justify-end"> <DropdownMenuItem asChild className="flex items-end justify-end">
<AccountDialog /> <AccountDialog
onUpdate={async () => {
refetchUser();
}}
/>
</DropdownMenuItem> </DropdownMenuItem>
<DropdownMenuItem asChild className="flex items-end justify-end"> <DropdownMenuItem asChild className="flex items-end justify-end">
<OrganisationsDialog <OrganisationsDialog

View File

@@ -8,7 +8,7 @@ import { Label } from "@/components/ui/label";
import { UploadAvatar } from "@/components/upload-avatar"; import { UploadAvatar } from "@/components/upload-avatar";
import { user } from "@/lib/server"; import { user } from "@/lib/server";
function AccountDialog({ trigger }: { trigger?: ReactNode }) { function AccountDialog({ onUpdate, trigger }: { onUpdate?: () => void; trigger?: ReactNode }) {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [name, setName] = useState(""); const [name, setName] = useState("");
const [username, setUsername] = useState(""); const [username, setUsername] = useState("");
@@ -57,7 +57,8 @@ function AccountDialog({ trigger }: { trigger?: ReactNode }) {
setError(""); setError("");
localStorage.setItem("user", JSON.stringify(data)); localStorage.setItem("user", JSON.stringify(data));
setPassword(""); setPassword("");
window.location.reload(); onUpdate?.();
setOpen(false);
}, },
onError: (errorMessage) => { onError: (errorMessage) => {
setError(errorMessage); setError(errorMessage);