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 axiosConfig from "../../config/axiosConfig";
import { I_Installation } from "../../util/types";
@ -8,6 +9,8 @@ interface InstallationContextProviderProps {
fetchData: () => Promise<void>;
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<I_Installation[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<AxiosError>();
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 (
<InstallationContext.Provider
value={{ data, setData, fetchData, loading, setLoading }}
value={{ data, setData, fetchData, loading, setLoading, error, setError }}
>
{children}
</InstallationContext.Provider>

View File

@ -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);