From c84213336d1604d9624c94626977802cbc5a4f7d Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Tue, 2 Jul 2024 14:34:16 -0400 Subject: [PATCH 1/2] fix: reduce page load times --- app/league/all/page.tsx | 6 ++++++ context/AuthContextProvider.tsx | 25 +++++++------------------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/league/all/page.tsx b/app/league/all/page.tsx index 03f9e1b1..b6eabdcd 100644 --- a/app/league/all/page.tsx +++ b/app/league/all/page.tsx @@ -9,6 +9,7 @@ import { IGameWeek, ILeague } from '@/api/apiFunctions.interface'; import { getUserLeagues } from '@/utils/utils'; import { useDataStore } from '@/store/dataStore'; import { getGameWeek } from '@/api/apiFunctions'; +import { useAuthContext } from '@/context/AuthContextProvider'; /** * Renders the leagues component. @@ -18,6 +19,11 @@ const Leagues = (): JSX.Element => { const [leagues, setLeagues] = useState([]); const [currentWeek, setCurrentWeek] = useState(1); const { user } = useDataStore((state) => state); + const { getUser } = useAuthContext(); + + if (!user.id || user.id === '') { + getUser(); + } /** * Fetches the user's leagues. diff --git a/context/AuthContextProvider.tsx b/context/AuthContextProvider.tsx index 92ea24df..d210368e 100644 --- a/context/AuthContextProvider.tsx +++ b/context/AuthContextProvider.tsx @@ -2,8 +2,8 @@ // Licensed under the MIT License. 'use client'; -import React, { JSX, useCallback } from 'react'; -import { createContext, useContext, useEffect, useMemo, useState } from 'react'; +import React, { JSX } from 'react'; +import { createContext, useContext, useMemo, useState } from 'react'; import { account } from '@/api/config'; import { useRouter } from 'next/navigation'; import { useDataStore } from '@/store/dataStore'; @@ -21,7 +21,7 @@ type AuthContextType = { setIsSignedIn: React.Dispatch>; loginAccount: (user: UserCredentials) => Promise; // eslint-disable-line no-unused-vars logoutAccount: () => Promise; - getUser: () => Promise; + getUser: () => Promise; }; export const AuthContext = createContext(null); @@ -38,19 +38,9 @@ export const AuthContextProvider = ({ children: React.ReactNode; }): JSX.Element => { const [isSignedIn, setIsSignedIn] = useState(false); - const { updateUser, resetUser, user } = useDataStore( - (state) => state, - ); + const { updateUser, resetUser } = useDataStore((state) => state); const router = useRouter(); - useEffect(() => { - if (user.id === '' || user.email === '') { - getUser(); - return; - } - setIsSignedIn(true); - }, [user]); - /** * Authenticate and set session state * @param user - The user credentials. @@ -59,7 +49,6 @@ export const AuthContextProvider = ({ const loginAccount = async (user: UserCredentials): Promise => { try { await account.createEmailPasswordSession(user.email, user.password); - await getUser(); // Fetch user data and update state router.push('/league/all'); } catch (error) { console.error('Login error:', error); @@ -84,9 +73,9 @@ export const AuthContextProvider = ({ /** * Get user data from the session - * @returns {Promise} + * @returns {Promise} - The user data or an error. */ - const getUser = useCallback(async () => { + const getUser = async (): Promise => { if (!isSessionInLocalStorage()) { router.push('/login'); return; @@ -101,7 +90,7 @@ export const AuthContextProvider = ({ resetUser(); setIsSignedIn(false); } - }, [user]); + }; /** * Helper function to validate session data in local storage From 81ec3e0ee324709822c86e3c29f9e85bcaaaba29 Mon Sep 17 00:00:00 2001 From: Chris Nowicki <102450568+chris-nowicki@users.noreply.github.com> Date: Wed, 3 Jul 2024 09:36:26 -0400 Subject: [PATCH 2/2] fix: move user check to authcontext provider --- app/league/all/page.tsx | 10 ---------- context/AuthContextProvider.tsx | 13 +++++++++++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/league/all/page.tsx b/app/league/all/page.tsx index b6eabdcd..252aff28 100644 --- a/app/league/all/page.tsx +++ b/app/league/all/page.tsx @@ -9,7 +9,6 @@ import { IGameWeek, ILeague } from '@/api/apiFunctions.interface'; import { getUserLeagues } from '@/utils/utils'; import { useDataStore } from '@/store/dataStore'; import { getGameWeek } from '@/api/apiFunctions'; -import { useAuthContext } from '@/context/AuthContextProvider'; /** * Renders the leagues component. @@ -19,11 +18,6 @@ const Leagues = (): JSX.Element => { const [leagues, setLeagues] = useState([]); const [currentWeek, setCurrentWeek] = useState(1); const { user } = useDataStore((state) => state); - const { getUser } = useAuthContext(); - - if (!user.id || user.id === '') { - getUser(); - } /** * Fetches the user's leagues. @@ -48,10 +42,6 @@ const Leagues = (): JSX.Element => { }; useEffect(() => { - if (!user.id || user.id === '') { - return; - } - getLeagues(); getCurrentGameWeek(); }, [user]); diff --git a/context/AuthContextProvider.tsx b/context/AuthContextProvider.tsx index d210368e..2a37850a 100644 --- a/context/AuthContextProvider.tsx +++ b/context/AuthContextProvider.tsx @@ -2,7 +2,7 @@ // Licensed under the MIT License. 'use client'; -import React, { JSX } from 'react'; +import React, { JSX, useEffect } from 'react'; import { createContext, useContext, useMemo, useState } from 'react'; import { account } from '@/api/config'; import { useRouter } from 'next/navigation'; @@ -38,7 +38,9 @@ export const AuthContextProvider = ({ children: React.ReactNode; }): JSX.Element => { const [isSignedIn, setIsSignedIn] = useState(false); - const { updateUser, resetUser } = useDataStore((state) => state); + const { updateUser, resetUser, user } = useDataStore( + (state) => state, + ); const router = useRouter(); /** @@ -107,6 +109,13 @@ export const AuthContextProvider = ({ return true; }; + useEffect(() => { + if (!user.id || user.id === '') { + getUser(); + return; + } + }, [user]); + // Memoize context values to avoid unnecessary re-renders const contextValue = useMemo( () => ({