-
Notifications
You must be signed in to change notification settings - Fork 1
#356 / #330 Bug: reduce page load times & look into useCallback #363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chris-nowicki
wants to merge
6
commits into
develop
Choose a base branch
from
chris/bug-render-time
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+13
−19
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c842133
fix: reduce page load times
chris-nowicki 7cb8691
Merge branch 'develop' into chris/bug-render-time
chris-nowicki 81ec3e0
fix: move user check to authcontext provider
chris-nowicki d5e29d9
Merge branch 'develop' into chris/bug-render-time
shashilo dfbc852
Merge branch 'develop' into chris/bug-render-time
chris-nowicki 3a390de
Merge branch 'develop' into chris/bug-render-time
chris-nowicki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, useEffect } 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<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); | ||
|
|
@@ -43,14 +43,6 @@ export const AuthContextProvider = ({ | |
| ); | ||
| 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 +51,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); | ||
|
|
@@ -84,9 +75,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> => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will need unit tests for this file. I do not see a test file at all.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. roger that. I will add one. |
||
| if (!isSessionInLocalStorage()) { | ||
| router.push('/login'); | ||
| return; | ||
|
|
@@ -101,7 +92,7 @@ export const AuthContextProvider = ({ | |
| resetUser(); | ||
| setIsSignedIn(false); | ||
| } | ||
| }, [user]); | ||
| }; | ||
|
|
||
| /** | ||
| * Helper function to validate session data in local storage | ||
|
|
@@ -118,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( | ||
| () => ({ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This entire empty user will be fixed with this change? I had to add it to 2 other pages because that's how it works for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shashilo I am confused. need more info. I fixed this to remove it from page.tsx and moved that into the useEffect of authContextProvider as that is the only place I could get it to work and the provider wraps around all pages.