add error to InstallationContext
This commit is contained in:
parent
b0aebf6e7d
commit
04ef034b07
|
@ -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
|
||||||
|
.get("/GetAllInstallations", {})
|
||||||
|
.then((res) => {
|
||||||
setData(res.data);
|
setData(res.data);
|
||||||
setLoading(false);
|
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>
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue