2025-10-27 08:28:44 +00:00
|
|
|
import { useEffect, useReducer, useState, type ReactNode } from "react";
|
|
|
|
|
import type { DedupedSightings, NPEDUser, ReducedSightingType } from "../../types/types";
|
2025-08-29 10:07:59 +01:00
|
|
|
import { NPEDUserContext } from "../NPEDUserContext";
|
2025-10-27 08:28:44 +00:00
|
|
|
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
|
|
|
|
|
import { initialState, reducer } from "../reducers/NPEDContextReducer";
|
2025-08-29 10:07:59 +01:00
|
|
|
|
|
|
|
|
type NPEDUserProviderType = {
|
|
|
|
|
children: ReactNode;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const NPEDUserProvider = ({ children }: NPEDUserProviderType) => {
|
2025-10-27 08:28:44 +00:00
|
|
|
const [state, dispatch] = useReducer(reducer, initialState);
|
|
|
|
|
const { mutation } = useCameraBlackboard();
|
2025-08-29 10:07:59 +01:00
|
|
|
const [user, setUser] = useState<NPEDUser | null>(null);
|
2025-09-25 10:38:49 +01:00
|
|
|
const [sessionStarted, setSessionStarted] = useState(false);
|
2025-10-08 15:46:54 +01:00
|
|
|
const [sessionList, setSessionList] = useState<ReducedSightingType[]>([]);
|
2025-10-24 12:10:10 +01:00
|
|
|
const [sessionPaused, setSessionPaused] = useState(false);
|
2025-10-27 08:28:44 +00:00
|
|
|
const [savedSightings, setSavedSightings] = useState<DedupedSightings | []>([]);
|
2025-08-29 10:07:59 +01:00
|
|
|
|
2025-10-27 08:28:44 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchData = async () => {
|
|
|
|
|
const result = await mutation.mutateAsync({
|
|
|
|
|
operation: "VIEW",
|
|
|
|
|
path: "sessionStats",
|
|
|
|
|
});
|
|
|
|
|
if (!result.result) return;
|
|
|
|
|
setSavedSightings(result?.result);
|
|
|
|
|
};
|
|
|
|
|
fetchData();
|
|
|
|
|
}, []);
|
|
|
|
|
console.log(savedSightings);
|
2025-08-29 10:07:59 +01:00
|
|
|
return (
|
2025-09-25 10:38:49 +01:00
|
|
|
<NPEDUserContext.Provider
|
|
|
|
|
value={{
|
|
|
|
|
user,
|
|
|
|
|
setUser,
|
|
|
|
|
setSessionStarted,
|
|
|
|
|
sessionStarted,
|
|
|
|
|
sessionList,
|
|
|
|
|
setSessionList,
|
2025-10-24 12:10:10 +01:00
|
|
|
sessionPaused,
|
|
|
|
|
setSessionPaused,
|
2025-10-27 08:28:44 +00:00
|
|
|
savedSightings,
|
|
|
|
|
setSavedSightings,
|
2025-09-25 10:38:49 +01:00
|
|
|
}}
|
|
|
|
|
>
|
2025-08-29 10:07:59 +01:00
|
|
|
{children}
|
|
|
|
|
</NPEDUserContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
};
|