add error to InstallationContext

This commit is contained in:
Sina Blattmann 2023-03-23 13:28:42 +01:00
parent b0aebf6e7d
commit 04ef034b07
2 changed files with 14 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import { AxiosError } from "axios";
import { createContext, ReactNode, useCallback, useState } from "react"; import { createContext, ReactNode, useCallback, useState } from "react";
import axiosConfig from "../../config/axiosConfig"; import axiosConfig from "../../config/axiosConfig";
import { I_Installation } from "../../util/types"; import { I_Installation } from "../../util/types";
@ -8,6 +9,8 @@ interface InstallationContextProviderProps {
fetchData: () => Promise<void>; fetchData: () => Promise<void>;
loading: boolean; loading: boolean;
setLoading: (value: boolean) => void; setLoading: (value: boolean) => void;
error?: AxiosError;
setError: (value: AxiosError) => void;
} }
export const InstallationContext = export const InstallationContext =
@ -17,23 +20,28 @@ export const InstallationContext =
fetchData: () => Promise.resolve(), fetchData: () => Promise.resolve(),
loading: false, loading: false,
setLoading: () => {}, setLoading: () => {},
setError: (value) => {},
}); });
const InstallationContextProvider = ({ children }: { children: ReactNode }) => { const InstallationContextProvider = ({ children }: { children: ReactNode }) => {
const [data, setData] = useState<I_Installation[]>([]); const [data, setData] = useState<I_Installation[]>([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [error, setError] = useState<AxiosError>();
const fetchData = useCallback(async () => { const fetchData = useCallback(async () => {
setLoading(true); setLoading(true);
axiosConfig.get("/GetAllInstallations", {}).then((res) => { axiosConfig
setData(res.data); .get("/GetAllInstallations", {})
setLoading(false); .then((res) => {
}); setData(res.data);
setLoading(false);
})
.catch((err: AxiosError) => setError(err));
}, []); }, []);
return ( return (
<InstallationContext.Provider <InstallationContext.Provider
value={{ data, setData, fetchData, loading, setLoading }} value={{ data, setData, fetchData, loading, setLoading, error, setError }}
> >
{children} {children}
</InstallationContext.Provider> </InstallationContext.Provider>

View File

@ -36,7 +36,7 @@ const filterData = (
}; };
const InstallationList = (props: InstallationListProps) => { const InstallationList = (props: InstallationListProps) => {
const { fetchData, data, loading } = useContext(InstallationContext); const { fetchData, data, loading, error } = useContext(InstallationContext);
const filteredData = filterData(props.searchQuery, data); const filteredData = filterData(props.searchQuery, data);