- added functionality to save sighting sessions to black board

This commit is contained in:
2025-10-27 08:28:44 +00:00
parent 9975e6a6ca
commit 18534ceb2c
5 changed files with 87 additions and 21 deletions

View File

@@ -1,5 +1,5 @@
import { createContext, useContext, type SetStateAction } from "react";
import type { NPEDUser, ReducedSightingType } from "../types/types";
import type { DedupedSightings, NPEDUser, ReducedSightingType } from "../types/types";
type UserContextValue = {
user: NPEDUser | null;
@@ -10,6 +10,8 @@ type UserContextValue = {
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);

View File

@@ -1,17 +1,34 @@
import { useState, type ReactNode } from "react";
import type { NPEDUser, ReducedSightingType } from "../../types/types";
import { useEffect, useReducer, useState, type ReactNode } from "react";
import type { DedupedSightings, NPEDUser, ReducedSightingType } from "../../types/types";
import { NPEDUserContext } from "../NPEDUserContext";
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
import { initialState, reducer } from "../reducers/NPEDContextReducer";
type NPEDUserProviderType = {
children: ReactNode;
};
export const NPEDUserProvider = ({ children }: NPEDUserProviderType) => {
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);
const [savedSightings, setSavedSightings] = useState<DedupedSightings | []>([]);
useEffect(() => {
const fetchData = async () => {
const result = await mutation.mutateAsync({
operation: "VIEW",
path: "sessionStats",
});
if (!result.result) return;
setSavedSightings(result?.result);
};
fetchData();
}, []);
console.log(savedSightings);
return (
<NPEDUserContext.Provider
value={{
@@ -23,6 +40,8 @@ export const NPEDUserProvider = ({ children }: NPEDUserProviderType) => {
setSessionList,
sessionPaused,
setSessionPaused,
savedSightings,
setSavedSightings,
}}
>
{children}

View File

@@ -0,0 +1,20 @@
import type { NPEDACTION, NPEDSTATE } from "../../types/types";
export const initialState = {
sessionStarted: false,
sessionList: [],
sessionPaused: false,
savedSightings: [],
};
export function reducer(state: NPEDSTATE, action: NPEDACTION) {
switch (action.type) {
case "SESSION":
return {
...state,
sessionStarted: action.payload,
};
default:
return { ...state };
}
}