fix(App.tsx): remove extra comma in the useContext hook

feat(App.tsx): add support for displaying fetch error message when there is an error in the backend API call
fix(typesContext.tsx): add error handling for API call and set fetchError state accordingly
feat(typesContext.tsx): add fetchError state and setFetchError function to the typesContext
This commit is contained in:
anovazzi1 2023-08-23 12:52:49 -03:00
commit f22768c2bc
3 changed files with 21 additions and 3 deletions

View file

@ -13,6 +13,7 @@ import LoadingComponent from "./components/loadingComponent";
import { alertContext } from "./contexts/alertContext"; import { alertContext } from "./contexts/alertContext";
import { locationContext } from "./contexts/locationContext"; import { locationContext } from "./contexts/locationContext";
import { TabsContext } from "./contexts/tabsContext"; import { TabsContext } from "./contexts/tabsContext";
import { typesContext } from "./contexts/typesContext";
import Router from "./routes"; import Router from "./routes";
export default function App() { export default function App() {
@ -38,6 +39,7 @@ export default function App() {
setSuccessOpen, setSuccessOpen,
loading, loading,
} = useContext(alertContext); } = useContext(alertContext);
const { fetchError } = useContext(typesContext);
// Initialize state variable for the list of alerts // Initialize state variable for the list of alerts
const [alertsList, setAlertsList] = useState< const [alertsList, setAlertsList] = useState<
@ -137,7 +139,11 @@ export default function App() {
> >
{loading ? ( {loading ? (
<div className="loading-page-panel"> <div className="loading-page-panel">
{fetchError ? (
<div>There was an error on the backend</div>
) : (
<LoadingComponent remSize={50} /> <LoadingComponent remSize={50} />
)}
</div> </div>
) : ( ) : (
<> <>

View file

@ -6,7 +6,7 @@ import {
useState, useState,
} from "react"; } from "react";
import { Node } from "reactflow"; import { Node } from "reactflow";
import { getAll } from "../controllers/API"; import { getAll, getHealth } from "../controllers/API";
import { APIKindType } from "../types/api"; import { APIKindType } from "../types/api";
import { typesContextType } from "../types/typesContext"; import { typesContextType } from "../types/typesContext";
import { alertContext } from "./alertContext"; import { alertContext } from "./alertContext";
@ -23,6 +23,8 @@ const initialValue: typesContextType = {
setTemplates: () => {}, setTemplates: () => {},
data: {}, data: {},
setData: () => {}, setData: () => {},
setFetchError: () => {},
fetchError: false,
}; };
export const typesContext = createContext<typesContextType>(initialValue); export const typesContext = createContext<typesContextType>(initialValue);
@ -32,6 +34,7 @@ export function TypesProvider({ children }: { children: ReactNode }) {
const [reactFlowInstance, setReactFlowInstance] = useState(null); const [reactFlowInstance, setReactFlowInstance] = useState(null);
const [templates, setTemplates] = useState({}); const [templates, setTemplates] = useState({});
const [data, setData] = useState({}); const [data, setData] = useState({});
const [fetchError, setFetchError] = useState(false);
const { setLoading } = useContext(alertContext); const { setLoading } = useContext(alertContext);
useEffect(() => { useEffect(() => {
@ -46,8 +49,13 @@ export function TypesProvider({ children }: { children: ReactNode }) {
async function getTypes(): Promise<void> { async function getTypes(): Promise<void> {
try { try {
const result = await getAll(); const result = await getAll();
if (result?.status !== 200) {
let health = await getHealth().catch((e) => {
setFetchError(true);
});
}
// Make sure to only update the state if the component is still mounted. // Make sure to only update the state if the component is still mounted.
if (isMounted) { if (isMounted && result?.status === 200) {
setLoading(false); setLoading(false);
setData(result.data); setData(result.data);
setTemplates( setTemplates(
@ -117,6 +125,8 @@ export function TypesProvider({ children }: { children: ReactNode }) {
templates, templates,
data, data,
setData, setData,
fetchError,
setFetchError,
}} }}
> >
{children} {children}

View file

@ -15,4 +15,6 @@ export type typesContextType = {
setTemplates: (newState: {}) => void; setTemplates: (newState: {}) => void;
data: typeof data; data: typeof data;
setData: (newState: {}) => void; setData: (newState: {}) => void;
fetchError: boolean;
setFetchError: (newState: boolean) => void;
}; };