-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
247 additions
and
169 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { createContext, useCallback, useEffect, useState } from "react"; | ||
import { UserDetails } from "./types/user"; | ||
import { getUser } from "./api"; | ||
|
||
export const UserContext = createContext<{ | ||
user: UserDetails | null | undefined; | ||
updateUser: () => Promise<void>; | ||
}>({ | ||
user: null, | ||
updateUser: async () => {}, | ||
}); | ||
|
||
function usersEqual( | ||
oldUser: UserDetails | null | undefined, | ||
newUser: UserDetails | null, | ||
) { | ||
if (oldUser === undefined) { | ||
return false; | ||
} | ||
if (oldUser === newUser) { | ||
return true; // both are null | ||
} | ||
if (oldUser === null || newUser === null) { | ||
return false; // one is null, the other is not | ||
} | ||
|
||
return ( | ||
oldUser.username === newUser.username && | ||
oldUser.type === newUser.type && | ||
oldUser.balance === newUser.balance | ||
); | ||
} | ||
|
||
function UserProvider(props: { children: React.ReactNode }) { | ||
const [user, setUser] = useState<UserDetails | null | undefined>(undefined); | ||
useEffect(() => { | ||
getUser().then(setUser); | ||
}, []); | ||
const updateUser = useCallback(async () => { | ||
const newUser = await getUser(); | ||
if (usersEqual(user, newUser)) { | ||
return; | ||
} | ||
setUser(newUser); | ||
}, [user]); | ||
return ( | ||
<UserContext.Provider value={{ user, updateUser }}> | ||
{props.children} | ||
</UserContext.Provider> | ||
); | ||
} | ||
|
||
export default UserProvider; |
This file contains 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 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 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 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
Oops, something went wrong.