Files
Mav-Mobile-UI/src/context/providers/IntegrationsContextProvider.tsx

59 lines
1.5 KiB
TypeScript
Raw Normal View History

import { useEffect, useReducer, type ReactNode } from "react";
2025-10-27 09:35:59 +00:00
import { IntegrationsContext } from "../IntegrationsContext";
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
2025-10-27 09:35:59 +00:00
import { initialState, reducer } from "../reducers/IntegrationsContextReducer";
2025-08-29 10:07:59 +01:00
2025-10-27 09:35:59 +00:00
type IntegrationsProviderType = {
2025-08-29 10:07:59 +01:00
children: ReactNode;
};
2025-10-27 09:35:59 +00:00
export const IntegrationsProvider = ({ children }: IntegrationsProviderType) => {
const [state, dispatch] = useReducer(reducer, initialState);
const { mutation } = useCameraBlackboard();
2025-08-29 10:07:59 +01:00
useEffect(() => {
let isMounted = true;
const fetchData = async () => {
try {
const result = await mutation.mutateAsync({
operation: "VIEW",
path: "sessionStats",
});
if (!isMounted) return;
const catResult = await mutation.mutateAsync({
operation: "VIEW",
path: "CategoryPopup",
});
if (!isMounted) return;
if (!result?.result || typeof result.result === "string") return;
dispatch({ type: "UPDATE", payload: result.result });
dispatch({ type: "NPEDCATENABLED", payload: catResult.result });
} catch (error) {
console.error("Error in fetchData:", error);
}
};
fetchData();
return () => {
isMounted = false;
};
}, []);
2025-08-29 10:07:59 +01:00
return (
2025-10-27 09:35:59 +00:00
<IntegrationsContext.Provider
value={{
2025-10-27 09:35:59 +00:00
state,
dispatch,
}}
>
2025-08-29 10:07:59 +01:00
{children}
2025-10-27 09:35:59 +00:00
</IntegrationsContext.Provider>
2025-08-29 10:07:59 +01:00
);
};