From 04ef034b0714d44337e4841f10100d031c85b331 Mon Sep 17 00:00:00 2001 From: Sina Blattmann Date: Thu, 23 Mar 2023 13:28:42 +0100 Subject: [PATCH] add error to InstallationContext --- .../Context/InstallationContextProvider.tsx | 18 +++++++++++++----- .../Installations/InstallationList.tsx | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/typescript/Frontend/src/components/Context/InstallationContextProvider.tsx b/typescript/Frontend/src/components/Context/InstallationContextProvider.tsx index cf09fd57e..a366b8051 100644 --- a/typescript/Frontend/src/components/Context/InstallationContextProvider.tsx +++ b/typescript/Frontend/src/components/Context/InstallationContextProvider.tsx @@ -1,3 +1,4 @@ +import { AxiosError } from "axios"; import { createContext, ReactNode, useCallback, useState } from "react"; import axiosConfig from "../../config/axiosConfig"; import { I_Installation } from "../../util/types"; @@ -8,6 +9,8 @@ interface InstallationContextProviderProps { fetchData: () => Promise; loading: boolean; setLoading: (value: boolean) => void; + error?: AxiosError; + setError: (value: AxiosError) => void; } export const InstallationContext = @@ -17,23 +20,28 @@ export const InstallationContext = fetchData: () => Promise.resolve(), loading: false, setLoading: () => {}, + setError: (value) => {}, }); const InstallationContextProvider = ({ children }: { children: ReactNode }) => { const [data, setData] = useState([]); const [loading, setLoading] = useState(false); + const [error, setError] = useState(); const fetchData = useCallback(async () => { setLoading(true); - axiosConfig.get("/GetAllInstallations", {}).then((res) => { - setData(res.data); - setLoading(false); - }); + axiosConfig + .get("/GetAllInstallations", {}) + .then((res) => { + setData(res.data); + setLoading(false); + }) + .catch((err: AxiosError) => setError(err)); }, []); return ( {children} diff --git a/typescript/Frontend/src/components/Installations/InstallationList.tsx b/typescript/Frontend/src/components/Installations/InstallationList.tsx index 091f359ea..b015e1ca4 100644 --- a/typescript/Frontend/src/components/Installations/InstallationList.tsx +++ b/typescript/Frontend/src/components/Installations/InstallationList.tsx @@ -36,7 +36,7 @@ const filterData = ( }; const InstallationList = (props: InstallationListProps) => { - const { fetchData, data, loading } = useContext(InstallationContext); + const { fetchData, data, loading, error } = useContext(InstallationContext); const filteredData = filterData(props.searchQuery, data);