-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add context for login state display in nav [#134]
* Add <UserDataWrapper> component to base layout to provide access to logged in user via React Context * Wrap username or login link display into <UserOrLoginLink> Client Component; add to <Nav> component
- Loading branch information
Showing
4 changed files
with
85 additions
and
11 deletions.
There are no files selected for viewing
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,19 @@ | ||
"use client"; | ||
|
||
import React, { useContext } from "react"; | ||
|
||
import { UserContext } from "../user-data-wrapper"; | ||
|
||
export default function UserOrLoginLink(): React.ReactElement { | ||
const { user } = useContext(UserContext); | ||
return user ? ( | ||
<a href="/whoami"> | ||
<span role="img" aria-labelledby="userIcon"> | ||
👤 | ||
</span> | ||
{` ${user.username}`} | ||
</a> | ||
) : ( | ||
<a href="/login">LOGIN</a> | ||
); | ||
} |
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 @@ | ||
"use client"; | ||
|
||
import React, { createContext } from "react"; | ||
|
||
type User = { | ||
username: string; | ||
}; | ||
|
||
type Group = { | ||
isPublic: boolean; | ||
name: string; | ||
}; | ||
|
||
type UserState = { | ||
user: null | User; | ||
groupMemberships: Group[]; | ||
}; | ||
|
||
const initialState: UserState = { | ||
user: null, | ||
groupMemberships: [], | ||
}; | ||
|
||
export const UserContext = createContext(initialState); | ||
|
||
export default class UserDataWrapper extends React.Component< | ||
{ children: React.ReactNode }, | ||
UserState | ||
> { | ||
override state = initialState; | ||
|
||
override componentDidMount(): void { | ||
this.loadUser(); | ||
} | ||
|
||
async loadUser(): Promise<void> { | ||
const response = await fetch("/whoami", { | ||
headers: { Accept: "application/json" }, | ||
}); | ||
|
||
const whoami: UserState = await response.json(); | ||
|
||
this.setState((state) => ({ ...state, ...whoami })); | ||
} | ||
|
||
override render(): React.ReactNode { | ||
return ( | ||
<UserContext.Provider value={this.state}> | ||
{this.props.children} | ||
</UserContext.Provider> | ||
); | ||
} | ||
} |