Skip to content

Commit

Permalink
fix: reduce page load times
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-nowicki committed Jul 2, 2024
1 parent 943c0a2 commit c842133
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
6 changes: 6 additions & 0 deletions app/league/all/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,6 +19,11 @@ const Leagues = (): JSX.Element => {
const [leagues, setLeagues] = useState<ILeague[]>([]);
const [currentWeek, setCurrentWeek] = useState<IGameWeek['week']>(1);
const { user } = useDataStore((state) => state);
const { getUser } = useAuthContext();

if (!user.id || user.id === '') {
getUser();
}

/**
* Fetches the user's leagues.
Expand Down
25 changes: 7 additions & 18 deletions context/AuthContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,7 +21,7 @@ type AuthContextType = {
setIsSignedIn: React.Dispatch<React.SetStateAction<boolean>>;
loginAccount: (user: UserCredentials) => Promise<void | Error>; // eslint-disable-line no-unused-vars
logoutAccount: () => Promise<void>;
getUser: () => Promise<IUser | undefined>;
getUser: () => Promise<void | IUser>;
};

export const AuthContext = createContext<AuthContextType | null>(null);
Expand All @@ -38,19 +38,9 @@ export const AuthContextProvider = ({
children: React.ReactNode;
}): JSX.Element => {
const [isSignedIn, setIsSignedIn] = useState<boolean>(false);
const { updateUser, resetUser, user } = useDataStore<DataStore>(
(state) => state,
);
const { updateUser, resetUser } = useDataStore<DataStore>((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.
Expand All @@ -59,7 +49,6 @@ export const AuthContextProvider = ({
const loginAccount = async (user: UserCredentials): Promise<void | Error> => {
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);
Expand All @@ -84,9 +73,9 @@ export const AuthContextProvider = ({

/**
* Get user data from the session
* @returns {Promise<void>}
* @returns {Promise<void | IUser>} - The user data or an error.
*/
const getUser = useCallback(async () => {
const getUser = async (): Promise<void | IUser> => {
if (!isSessionInLocalStorage()) {
router.push('/login');
return;
Expand All @@ -101,7 +90,7 @@ export const AuthContextProvider = ({
resetUser();
setIsSignedIn(false);
}
}, [user]);
};

/**
* Helper function to validate session data in local storage
Expand Down

0 comments on commit c842133

Please sign in to comment.