From 4c061dc19ca233c61c2fb454bc6de346b5d45cf7 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 17 Jun 2024 09:47:57 +0200 Subject: [PATCH] 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. --- src/components/account-dialog.tsx | 42 ++++++++++++++++++------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/components/account-dialog.tsx b/src/components/account-dialog.tsx index 7add37e..edc9c4d 100644 --- a/src/components/account-dialog.tsx +++ b/src/components/account-dialog.tsx @@ -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(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 ( +
+

No account

+
+ ); } useEffect(() => { @@ -39,7 +41,13 @@ export function AccountDialog() { return (
- >} /> + > + } + isDisconnected={!haveToken} + />
); }