-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX-2270 Storing the currentUsername for server objects between refre…
…shing (#2307)
- Loading branch information
Showing
8 changed files
with
187 additions
and
34 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
53 changes: 53 additions & 0 deletions
53
Src/WitsmlExplorer.Frontend/contexts/loggedInUsernamesContext.tsx
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 { | ||
LoggedInUsernamesAction, | ||
loggedInUsernamesReducer | ||
} from "contexts/loggedInUsernamesReducer"; | ||
import { ReactNode, createContext, useContext, useReducer } from "react"; | ||
|
||
export interface LoggedInUsername { | ||
serverId: string; | ||
username: string; | ||
} | ||
|
||
interface LoggedInUsernamesContextType { | ||
loggedInUsernames: LoggedInUsername[]; | ||
dispatchLoggedInUsernames: (action: LoggedInUsernamesAction) => void; | ||
} | ||
|
||
const LoggedInUsernamesContext = | ||
createContext<LoggedInUsernamesContextType>(null); | ||
|
||
interface LoggedInUsernamesProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
/** | ||
* This context is utilized to store the currentUsername for server objects in between refreshes for caching purposes. | ||
* @param param0 | ||
* @returns | ||
*/ | ||
export function LoggedInUsernamesProvider({ | ||
children | ||
}: LoggedInUsernamesProviderProps) { | ||
const [loggedInUsernames, dispatchLoggedInUsernames] = useReducer( | ||
loggedInUsernamesReducer, | ||
[] | ||
); | ||
|
||
return ( | ||
<LoggedInUsernamesContext.Provider | ||
value={{ loggedInUsernames, dispatchLoggedInUsernames }} | ||
> | ||
{children} | ||
</LoggedInUsernamesContext.Provider> | ||
); | ||
} | ||
|
||
export function useLoggedInUsernames() { | ||
const context = useContext(LoggedInUsernamesContext); | ||
if (!context) | ||
throw new Error( | ||
`useLoggedInUsernames() has to be used within <LoggedInUsernamesProvider>` | ||
); | ||
return context; | ||
} |
46 changes: 46 additions & 0 deletions
46
Src/WitsmlExplorer.Frontend/contexts/loggedInUsernamesReducer.tsx
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,46 @@ | ||
import { LoggedInUsername } from "contexts/loggedInUsernamesContext"; | ||
|
||
export enum LoggedInUsernamesActionType { | ||
AddLoggedInUsername = "AddLoggedInUsername" | ||
} | ||
|
||
export interface LoggedInUsernamesAction { | ||
type: any; | ||
payload: any; | ||
} | ||
|
||
export interface AddLoggedInUsernameAction extends LoggedInUsernamesAction { | ||
type: LoggedInUsernamesActionType.AddLoggedInUsername; | ||
payload: LoggedInUsername; | ||
} | ||
|
||
export function loggedInUsernamesReducer( | ||
state: LoggedInUsername[], | ||
action: LoggedInUsernamesAction | ||
): LoggedInUsername[] { | ||
switch (action.type) { | ||
case LoggedInUsernamesActionType.AddLoggedInUsername: | ||
return addLoggedInUsername(state, action); | ||
default: { | ||
throw new Error(`Unsupported action type ${action.type}`); | ||
} | ||
} | ||
} | ||
|
||
const addLoggedInUsername = ( | ||
state: LoggedInUsername[], | ||
{ payload }: LoggedInUsernamesAction | ||
): LoggedInUsername[] => { | ||
const loggedInUsernames = structuredClone(state); | ||
const newLoggedInUsername: LoggedInUsername = structuredClone(payload); | ||
const index = loggedInUsernames.findIndex( | ||
(loggedInUsername) => | ||
loggedInUsername.serverId === newLoggedInUsername.serverId | ||
); | ||
if (index === -1) { | ||
loggedInUsernames.push(newLoggedInUsername); | ||
} else if (index >= 0) { | ||
loggedInUsernames.splice(index, 1, newLoggedInUsername); | ||
} | ||
return loggedInUsernames; | ||
}; |
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