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 { FlowsContext } from "./contexts/flowsContext";
import { locationContext } from "./contexts/locationContext";
import { getVersion } from "./controllers/API";
import { getHealth, getVersion } from "./controllers/API";
import Router from "./routes";
import useAlertStore from "./stores/alertStore";
import { useTypesStore } from "./stores/typesStore";
@ -43,7 +43,7 @@ export default function App() {
const successOpen = useAlertStore((state) => state.successOpen);
const setSuccessOpen = useAlertStore((state) => state.setSuccessOpen);
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
const [alertsList, setAlertsList] = useState<
@ -147,6 +147,20 @@ export default function App() {
});
}, [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 (
//need parent component with width and height
<div className="flex h-full flex-col">

View file

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

View file

@ -1,9 +1,9 @@
import { create } from "zustand";
import { getAll, getHealth } from "../controllers/API";
import { APIDataType, APIKindType } from "../types/api";
import { getAll } from "../controllers/API";
import { APIDataType } from "../types/api";
import { TypesStoreType } from "../types/zustand/types";
import useAlertStore from "./alertStore";
import { templatesGenerator, typesGenerator } from "../utils/reactflowUtils";
import useAlertStore from "./alertStore";
export const useTypesStore = create<TypesStoreType>((set, get) => ({
types: {},
@ -24,15 +24,13 @@ export const useTypesStore = create<TypesStoreType>((set, get) => ({
resolve();
})
.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.log(error);
getHealth().catch((e) => {
useAlertStore.setState({
fetchError: true,
loading: false,
});
reject();
});
reject();
});
});
},

View file

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