-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user email to account page (#566)
* feat: add user email to account page * feat: user email; update loading pattern * feat: user provider * feat: global app state * feat: show user email; abstract user hook
- Loading branch information
1 parent
63fe8bc
commit a34d976
Showing
11 changed files
with
164 additions
and
12 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
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,91 @@ | ||
import { createContext, useEffect, useReducer } from 'react' | ||
import { useUser } from '../hooks/use-user' | ||
|
||
/** | ||
* @typedef {{ | ||
* user: { | ||
* info: { | ||
* email: string | ||
* github: string | ||
* issuer: string | ||
* _id: string | ||
* } | ||
* } | ||
* }} State | ||
* */ | ||
|
||
/** | ||
* @typedef {{ | ||
* info: { | ||
* email: string | ||
* github: string | ||
* issuer: string | ||
* _id: string | ||
* } | ||
* }} UserInfo | ||
* */ | ||
|
||
/** | ||
* @typedef {{ | ||
* type: 'updateUser' | ||
* payload: any | ||
* }} Action | ||
* */ | ||
|
||
const initialState = { | ||
user: { | ||
info: { | ||
email: '', | ||
github: '', | ||
issuer: '', | ||
_id: '' | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {State} state | ||
* @param {UserInfo} userInfo | ||
*/ | ||
const updateUser = (state, userInfo) => ({ ...state, ...{ user: userInfo }}) | ||
|
||
/** | ||
* @param {State} state | ||
* @param {Action} action | ||
*/ | ||
const reducer = (state, action) => { | ||
switch (action.type) { | ||
case 'updateUser': | ||
return updateUser(state, action.payload) | ||
default: | ||
throw new Error(); | ||
} | ||
} | ||
|
||
export const AppContext = createContext({ | ||
state: initialState, | ||
/** | ||
* @param {any} _dispatch | ||
*/ | ||
dispatch: (_dispatch) => {} | ||
}) | ||
|
||
/** | ||
* @param {Object} props | ||
* @param {JSX.Element} [props.children] | ||
*/ | ||
const StateProvider = ({ children }) => { | ||
const [state, dispatch] = useReducer(reducer, initialState) | ||
|
||
// init app state with user | ||
const user = useUser() | ||
useEffect(() => dispatch({ type: 'updateUser', payload: user }), [ user ]) | ||
|
||
return ( | ||
<AppContext.Provider value={{ state: state, dispatch: dispatch }}> | ||
{ children } | ||
</AppContext.Provider> | ||
) | ||
} | ||
|
||
export default StateProvider |
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,11 @@ | ||
import { useQuery } from 'react-query' | ||
import { useLoggedIn } from '../lib/user' | ||
import { getInfo } from '../lib/api' | ||
|
||
export const useUser = () => { | ||
const { isLoggedIn } = useLoggedIn() | ||
const { data } = useQuery('get-info', getInfo, { | ||
enabled: isLoggedIn | ||
}) | ||
return data | ||
} |
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