-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into dependabot/npm_and_yarn/ui/develop/develo…
…pment-dependencies-b9521e7f6e
- Loading branch information
Showing
16 changed files
with
168 additions
and
89 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 |
---|---|---|
@@ -1,5 +1,96 @@ | ||
import { createContext } from "react"; | ||
import { createContext, useReducer, useEffect, useCallback, useContext, ReactNode, Dispatch } from "react"; | ||
import { UserState } from "types"; | ||
const UserContext = createContext<UserState>({ isLoggedin: false, username: "", email: "" }); | ||
|
||
export { UserContext }; | ||
// Define the initial state | ||
const initialState: UserState = { isLoggedin: false, username: "", email: "" }; | ||
|
||
// Define action types | ||
type UserAction = { type: "LOGIN"; payload: { username: string; email: string } } | { type: "LOGOUT" }; | ||
|
||
// Define the reducer function | ||
const userReducer = (state: UserState, action: UserAction): UserState => { | ||
switch (action.type) { | ||
case "LOGIN": | ||
return { | ||
isLoggedin: true, | ||
username: action.payload.username, | ||
email: action.payload.email, | ||
}; | ||
case "LOGOUT": | ||
return { | ||
isLoggedin: false, | ||
username: "", | ||
email: "", | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
// Create the UserContext with initial state and a dummy dispatch function | ||
const UserContext = createContext<{ | ||
state: UserState; | ||
dispatch: Dispatch<UserAction>; | ||
}>({ | ||
state: initialState, | ||
dispatch: () => null, | ||
}); | ||
|
||
// Create the UserProvider component | ||
const UserProvider = ({ children }: { children: ReactNode }) => { | ||
const [state, dispatch] = useReducer(userReducer, initialState); | ||
|
||
return ( | ||
<UserContext.Provider value={{ state, dispatch }}> | ||
<UserInfoFetcher /> | ||
{children} | ||
</UserContext.Provider> | ||
); | ||
}; | ||
|
||
const UserInfoFetcher = () => { | ||
const { state: userState, dispatch } = useContext(UserContext); | ||
const setUserStateFromUserinfo = useCallback(() => { | ||
fetch("/oauth2/userinfo", { credentials: "include" }) | ||
.then((response) => { | ||
if (!response.ok) { | ||
throw new Error("unauthenticated"); | ||
} | ||
return response.json(); | ||
}) | ||
.then((userInfo) => { | ||
const newUserState = { | ||
isLoggedin: true, | ||
email: userInfo.email, | ||
username: userInfo.user || userInfo.preferredUsername, | ||
}; | ||
|
||
// Only update state if it has changed | ||
if ( | ||
newUserState.isLoggedin !== userState.isLoggedin || | ||
newUserState.email !== userState.email || | ||
newUserState.username !== userState.username | ||
) { | ||
dispatch({ type: "LOGIN", payload: newUserState }); | ||
} | ||
}) | ||
.catch(() => { | ||
if (userState.isLoggedin) { | ||
dispatch({ type: "LOGOUT" }); | ||
} | ||
}); | ||
}, [userState, dispatch]); | ||
|
||
useEffect(() => { | ||
setUserStateFromUserinfo(); | ||
const interval = setInterval(() => { | ||
setUserStateFromUserinfo(); | ||
}, 30000); | ||
|
||
return () => clearInterval(interval); | ||
}, [userState]); | ||
|
||
return <></>; | ||
}; | ||
|
||
export { UserContext, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { UserContext } from "./UserContext"; | ||
export { UserContext, UserProvider } from "./UserContext"; | ||
|
||
export { ReloadPrompt } from "./ReloadSWPrompt"; |
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.