2025-10-27 11:04:53 +00:00
|
|
|
import { useEffect, useReducer, type ReactNode } from "react";
|
2025-10-27 09:35:59 +00:00
|
|
|
import { IntegrationsContext } from "../IntegrationsContext";
|
2025-10-27 08:28:44 +00:00
|
|
|
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
|
2025-10-27 09:35:59 +00:00
|
|
|
import { initialState, reducer } from "../reducers/IntegrationsContextReducer";
|
2025-11-19 13:55:21 +00:00
|
|
|
import { useHotlistData } from "../../hooks/useHotListData";
|
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) => {
|
2025-10-27 08:28:44 +00:00
|
|
|
const [state, dispatch] = useReducer(reducer, initialState);
|
|
|
|
|
const { mutation } = useCameraBlackboard();
|
2025-11-19 13:55:21 +00:00
|
|
|
const { query } = useHotlistData();
|
2025-08-29 10:07:59 +01:00
|
|
|
|
2025-10-27 08:28:44 +00:00
|
|
|
useEffect(() => {
|
2025-11-14 15:01:01 +00:00
|
|
|
let isMounted = true;
|
|
|
|
|
|
2025-10-27 08:28:44 +00:00
|
|
|
const fetchData = async () => {
|
2025-11-14 15:01:01 +00:00
|
|
|
try {
|
2025-11-17 10:19:44 +00:00
|
|
|
await mutation.mutateAsync({
|
|
|
|
|
operation: "Load",
|
|
|
|
|
path: "",
|
|
|
|
|
value: null,
|
|
|
|
|
});
|
2025-11-14 15:01:01 +00:00
|
|
|
const result = await mutation.mutateAsync({
|
|
|
|
|
operation: "VIEW",
|
|
|
|
|
path: "sessionStats",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!isMounted) return;
|
|
|
|
|
|
|
|
|
|
const catResult = await mutation.mutateAsync({
|
|
|
|
|
operation: "VIEW",
|
|
|
|
|
path: "CategoryPopup",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!isMounted) return;
|
2025-10-27 11:04:53 +00:00
|
|
|
|
2025-12-15 13:17:43 +00:00
|
|
|
if (result?.result && typeof result.result !== "string") {
|
|
|
|
|
dispatch({ type: "UPDATE", payload: result.result });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (catResult?.result) {
|
|
|
|
|
dispatch({ type: "NPEDCATENABLED", payload: catResult.result });
|
|
|
|
|
} else {
|
|
|
|
|
console.log("Early return: catResult check failed");
|
|
|
|
|
}
|
2025-11-14 15:01:01 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error in fetchData:", error);
|
|
|
|
|
}
|
2025-10-27 08:28:44 +00:00
|
|
|
};
|
2025-11-14 15:01:01 +00:00
|
|
|
|
2025-10-27 08:28:44 +00:00
|
|
|
fetchData();
|
2025-11-14 15:01:01 +00:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
isMounted = false;
|
|
|
|
|
};
|
2025-10-27 08:28:44 +00:00
|
|
|
}, []);
|
2025-11-14 15:01:01 +00:00
|
|
|
|
2025-11-19 13:55:21 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchHotlistData = async () => {
|
|
|
|
|
dispatch({ type: "SETHOTLISTS", payload: query?.data?.hotlists });
|
|
|
|
|
};
|
|
|
|
|
fetchHotlistData();
|
|
|
|
|
}, [query?.data]);
|
|
|
|
|
|
2025-08-29 10:07:59 +01:00
|
|
|
return (
|
2025-10-27 09:35:59 +00:00
|
|
|
<IntegrationsContext.Provider
|
2025-09-25 10:38:49 +01:00
|
|
|
value={{
|
2025-10-27 09:35:59 +00:00
|
|
|
state,
|
|
|
|
|
dispatch,
|
2025-09-25 10:38:49 +01:00
|
|
|
}}
|
|
|
|
|
>
|
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
|
|
|
);
|
|
|
|
|
};
|