- refactored NPED Login & logout

This commit is contained in:
2025-10-27 09:35:59 +00:00
parent 18534ceb2c
commit 251a2f5e7b
11 changed files with 76 additions and 76 deletions

View File

@@ -0,0 +1,22 @@
import { createContext, useContext, type ActionDispatch, type SetStateAction } from "react";
import type { DedupedSightings, NPEDACTION, NPEDSTATE, ReducedSightingType } from "../types/types";
type IntegrationsValue = {
sessionStarted: boolean;
sessionPaused: boolean;
setSessionPaused: React.Dispatch<SetStateAction<boolean>>;
setSessionStarted: React.Dispatch<SetStateAction<boolean>>;
sessionList: ReducedSightingType[];
setSessionList: React.Dispatch<SetStateAction<ReducedSightingType[]>>;
savedSightings: DedupedSightings;
setSavedSightings: React.Dispatch<SetStateAction<DedupedSightings>>;
state: NPEDSTATE;
dispatch: ActionDispatch<[action: NPEDACTION]>;
};
export const IntegrationsContext = createContext<IntegrationsValue | undefined>(undefined);
export const useIntegrationsContext = () => {
const ctx = useContext(IntegrationsContext);
if (!ctx) throw new Error("useNPEDContext must be used within <IntegrationsProvider>");
return ctx;
};

View File

@@ -1,22 +0,0 @@
import { createContext, useContext, type SetStateAction } from "react";
import type { DedupedSightings, NPEDUser, ReducedSightingType } from "../types/types";
type UserContextValue = {
user: NPEDUser | null;
setUser: React.Dispatch<SetStateAction<NPEDUser | null>>;
sessionStarted: boolean;
sessionPaused: boolean;
setSessionPaused: React.Dispatch<SetStateAction<boolean>>;
setSessionStarted: React.Dispatch<SetStateAction<boolean>>;
sessionList: ReducedSightingType[];
setSessionList: React.Dispatch<SetStateAction<ReducedSightingType[]>>;
savedSightings: DedupedSightings;
setSavedSightings: React.Dispatch<SetStateAction<DedupedSightings>>;
};
export const NPEDUserContext = createContext<UserContextValue | undefined>(undefined);
export const useNPEDContext = () => {
const ctx = useContext(NPEDUserContext);
if (!ctx) throw new Error("useNPEDContext must be used within <NPEDUserProvider>");
return ctx;
};

View File

@@ -1,17 +1,16 @@
import { useEffect, useReducer, useState, type ReactNode } from "react";
import type { DedupedSightings, NPEDUser, ReducedSightingType } from "../../types/types";
import { NPEDUserContext } from "../NPEDUserContext";
import type { DedupedSightings, ReducedSightingType } from "../../types/types";
import { IntegrationsContext } from "../IntegrationsContext";
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
import { initialState, reducer } from "../reducers/NPEDContextReducer";
import { initialState, reducer } from "../reducers/IntegrationsContextReducer";
type NPEDUserProviderType = {
type IntegrationsProviderType = {
children: ReactNode;
};
export const NPEDUserProvider = ({ children }: NPEDUserProviderType) => {
export const IntegrationsProvider = ({ children }: IntegrationsProviderType) => {
const [state, dispatch] = useReducer(reducer, initialState);
const { mutation } = useCameraBlackboard();
const [user, setUser] = useState<NPEDUser | null>(null);
const [sessionStarted, setSessionStarted] = useState(false);
const [sessionList, setSessionList] = useState<ReducedSightingType[]>([]);
const [sessionPaused, setSessionPaused] = useState(false);
@@ -28,12 +27,10 @@ export const NPEDUserProvider = ({ children }: NPEDUserProviderType) => {
};
fetchData();
}, []);
console.log(savedSightings);
return (
<NPEDUserContext.Provider
<IntegrationsContext.Provider
value={{
user,
setUser,
setSessionStarted,
sessionStarted,
sessionList,
@@ -42,9 +39,11 @@ export const NPEDUserProvider = ({ children }: NPEDUserProviderType) => {
setSessionPaused,
savedSightings,
setSavedSightings,
state,
dispatch,
}}
>
{children}
</NPEDUserContext.Provider>
</IntegrationsContext.Provider>
);
};

View File

@@ -5,15 +5,26 @@ export const initialState = {
sessionList: [],
sessionPaused: false,
savedSightings: [],
npedUser: null,
};
export function reducer(state: NPEDSTATE, action: NPEDACTION) {
switch (action.type) {
case "SESSION":
case "SESSIONSTART":
return {
...state,
sessionStarted: action.payload,
};
case "LOGIN":
return {
...state,
npedUser: action.payload,
};
case "LOGOUT":
return {
...state,
npedUser: action.payload,
};
default:
return { ...state };
}