feat(account-dialog): handle case when userContext not present

This commit updates the account-dialog component to properly handle when there's no userContext. Previously, a default user data was set when no userContext was found, this has been replaced with a simple message saying 'No account'. Also, checks for an authentication token in localStorage have been included. These changes aim towards better handling of edge cases and unauthenticated scenarios.
This commit is contained in:
Mathis H (Avnyr) 2024-06-17 09:47:57 +02:00
parent 3b1a3e93e0
commit 4c061dc19c
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -4,27 +4,29 @@ import { AccountInfo } from "@/components/account-info";
import { UserDataContext } from "@/components/providers/userdata-provider";
import { Skeleton } from "@/components/ui/skeleton";
import type { IUserData } from "@/interfaces/userdata.interface";
import {Dispatch, SetStateAction, useContext, useEffect, useState} from "react";
import {
type Dispatch,
type SetStateAction,
useContext,
useEffect,
useState,
} from "react";
const localStorage = typeof window !== "undefined" ? window.localStorage : null;
export function AccountDialog() {
const userContext = useContext(UserDataContext);
const token = localStorage?.getItem("sub") || "";
const haveToken = token.length >= 16 || false;
console.log(haveToken);
const [isLoaded, setIsLoaded] = useState<boolean>(false);
if (!userContext?.userData) {
userContext?.setUserData({
age: 0,
city: "Chambéry",
created_at: "jaj",
dollarAvailables: 34,
email: "mherriot@tutanota.com",
id: "",
isActive: false,
lastName: "Herriot",
pseudo: "Avnyr",
roleId: "",
updated_at: "",
firstName: "Mathis",
});
if (!userContext) {
return (
<div>
<p>No account</p>
</div>
);
}
useEffect(() => {
@ -39,7 +41,13 @@ export function AccountDialog() {
return (
<div>
<AccountInfo userData={userContext?.userData as IUserData} setUserData={userContext?.setUserData as Dispatch<SetStateAction<IUserData | undefined>>} />
<AccountInfo
userData={userContext?.userData as IUserData}
setUserData={
userContext?.setUserData as Dispatch<SetStateAction<IUserData | undefined>>
}
isDisconnected={!haveToken}
/>
</div>
);
}