Removed getHealth from getTypes and made it refresh every 5 seconds

This commit is contained in:
Lucas Oliveira 2024-01-05 21:17:01 -03:00
commit 4e29a92657
4 changed files with 24 additions and 18 deletions

View file

@ -18,7 +18,7 @@ import {
import { AuthContext } from "./contexts/authContext"; import { AuthContext } from "./contexts/authContext";
import { FlowsContext } from "./contexts/flowsContext"; import { FlowsContext } from "./contexts/flowsContext";
import { locationContext } from "./contexts/locationContext"; import { locationContext } from "./contexts/locationContext";
import { getVersion } from "./controllers/API"; import { getHealth, getVersion } from "./controllers/API";
import Router from "./routes"; import Router from "./routes";
import useAlertStore from "./stores/alertStore"; import useAlertStore from "./stores/alertStore";
import { useTypesStore } from "./stores/typesStore"; import { useTypesStore } from "./stores/typesStore";
@ -43,7 +43,7 @@ export default function App() {
const successOpen = useAlertStore((state) => state.successOpen); const successOpen = useAlertStore((state) => state.successOpen);
const setSuccessOpen = useAlertStore((state) => state.setSuccessOpen); const setSuccessOpen = useAlertStore((state) => state.setSuccessOpen);
const loading = useAlertStore((state) => state.loading); const loading = useAlertStore((state) => state.loading);
const fetchError = useAlertStore((state) => state.fetchError); const [fetchError, setFetchError] = useState(false);
// Initialize state variable for the list of alerts // Initialize state variable for the list of alerts
const [alertsList, setAlertsList] = useState< const [alertsList, setAlertsList] = useState<
@ -147,6 +147,20 @@ export default function App() {
}); });
}, [isAuthenticated]); }, [isAuthenticated]);
useEffect(() => {
// Timer to call getHealth every 5 seconds
const timer = setInterval(() => {
getHealth().catch((e) => {
setFetchError(true);
});
}, 5000);
// Clean up the timer on component unmount
return () => {
clearInterval(timer);
};
}, []);
return ( return (
//need parent component with width and height //need parent component with width and height
<div className="flex h-full flex-col"> <div className="flex h-full flex-col">

View file

@ -21,10 +21,6 @@ const useAlertStore = create<AlertStoreType>((set, get) => ({
notificationCenter: false, notificationCenter: false,
notificationList: [], notificationList: [],
loading: true, loading: true,
fetchError: false,
setFetchError: (newState: boolean) => {
set({ fetchError: newState });
},
setErrorData: (newState: { title: string; list?: Array<string> }) => { setErrorData: (newState: { title: string; list?: Array<string> }) => {
if (newState.title && newState.title !== "") { if (newState.title && newState.title !== "") {
set({ set({

View file

@ -1,9 +1,9 @@
import { create } from "zustand"; import { create } from "zustand";
import { getAll, getHealth } from "../controllers/API"; import { getAll } from "../controllers/API";
import { APIDataType, APIKindType } from "../types/api"; import { APIDataType } from "../types/api";
import { TypesStoreType } from "../types/zustand/types"; import { TypesStoreType } from "../types/zustand/types";
import useAlertStore from "./alertStore";
import { templatesGenerator, typesGenerator } from "../utils/reactflowUtils"; import { templatesGenerator, typesGenerator } from "../utils/reactflowUtils";
import useAlertStore from "./alertStore";
export const useTypesStore = create<TypesStoreType>((set, get) => ({ export const useTypesStore = create<TypesStoreType>((set, get) => ({
types: {}, types: {},
@ -24,17 +24,15 @@ export const useTypesStore = create<TypesStoreType>((set, get) => ({
resolve(); resolve();
}) })
.catch((error) => { .catch((error) => {
useAlertStore.getState().setErrorData({
title: "An error has occurred while fetching types.",
list: ["Please refresh the page."],
});
console.error("An error has occurred while fetching types."); console.error("An error has occurred while fetching types.");
console.log(error); console.log(error);
getHealth().catch((e) => {
useAlertStore.setState({
fetchError: true,
loading: false,
});
reject(); reject();
}); });
}); });
});
}, },
setTypes: (newState: {}) => { setTypes: (newState: {}) => {
set({ types: newState }); set({ types: newState });

View file

@ -20,6 +20,4 @@ export type AlertStoreType = {
removeFromNotificationList: (index: string) => void; removeFromNotificationList: (index: string) => void;
loading: boolean; loading: boolean;
setLoading: (newState: boolean) => void; setLoading: (newState: boolean) => void;
fetchError: boolean;
setFetchError: (newState: boolean) => void;
}; };