- refactored state for sessionlist, and session active and pause states

This commit is contained in:
2025-10-27 11:04:53 +00:00
parent 251a2f5e7b
commit 2d5b264041
7 changed files with 44 additions and 41 deletions

View File

@@ -1,15 +1,7 @@
import { createContext, useContext, type ActionDispatch, type SetStateAction } from "react";
import type { DedupedSightings, NPEDACTION, NPEDSTATE, ReducedSightingType } from "../types/types";
import { createContext, useContext, type ActionDispatch } from "react";
import type { NPEDACTION, NPEDSTATE } 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]>;
};

View File

@@ -1,5 +1,4 @@
import { useEffect, useReducer, useState, type ReactNode } from "react";
import type { DedupedSightings, ReducedSightingType } from "../../types/types";
import { useEffect, useReducer, type ReactNode } from "react";
import { IntegrationsContext } from "../IntegrationsContext";
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
import { initialState, reducer } from "../reducers/IntegrationsContextReducer";
@@ -11,10 +10,6 @@ type IntegrationsProviderType = {
export const IntegrationsProvider = ({ children }: IntegrationsProviderType) => {
const [state, dispatch] = useReducer(reducer, initialState);
const { mutation } = useCameraBlackboard();
const [sessionStarted, setSessionStarted] = useState(false);
const [sessionList, setSessionList] = useState<ReducedSightingType[]>([]);
const [sessionPaused, setSessionPaused] = useState(false);
const [savedSightings, setSavedSightings] = useState<DedupedSightings | []>([]);
useEffect(() => {
const fetchData = async () => {
@@ -22,23 +17,15 @@ export const IntegrationsProvider = ({ children }: IntegrationsProviderType) =>
operation: "VIEW",
path: "sessionStats",
});
if (!result.result) return;
setSavedSightings(result?.result);
if (!result.result || typeof result.result === "string") return;
dispatch({ type: "UPDATE", payload: result?.result });
};
fetchData();
}, []);
return (
<IntegrationsContext.Provider
value={{
setSessionStarted,
sessionStarted,
sessionList,
setSessionList,
sessionPaused,
setSessionPaused,
savedSightings,
setSavedSightings,
state,
dispatch,
}}

View File

@@ -25,6 +25,21 @@ export function reducer(state: NPEDSTATE, action: NPEDACTION) {
...state,
npedUser: action.payload,
};
case "SESSIONPAUSE":
return {
...state,
sessionPaused: action.payload,
};
case "ADD":
return {
...state,
sessionList: [...state.sessionList, action.payload],
};
case "UPDATE":
return {
...state,
sessionList: action.payload,
};
default:
return { ...state };
}