Major refactoring: navigation does not break anymore. the user can now navigate between the tabs without loosing context
This commit is contained in:
parent
457b098883
commit
1beee68bff
23 changed files with 137 additions and 80 deletions
|
|
@ -4,11 +4,31 @@ import { executeQuery } from "../services/database";
|
|||
|
||||
const useFetch = (query: Query) => {
|
||||
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [data, setData] = useState<{[column: string]: any;}[]>([]);
|
||||
const [isEmptyResult, setIsEmptyResult] = useState<boolean | undefined>(undefined);
|
||||
const [fetchState, setFetchState] = useState<{
|
||||
data: {[column: string]: any;}[];
|
||||
isLoading: boolean;
|
||||
isEmptyResult: boolean | undefined;
|
||||
}>({
|
||||
data: [],
|
||||
isLoading: false,
|
||||
isEmptyResult: undefined
|
||||
});
|
||||
|
||||
const setIsLoading = (isLoading: boolean) => {
|
||||
setFetchState((prevState) => ( {...prevState, isLoading} ));
|
||||
}
|
||||
|
||||
const setData = (data: {[column: string]: any;}[]) => {
|
||||
setFetchState((prevState) => ( {...prevState, data} ));
|
||||
}
|
||||
|
||||
const setIsEmptyResult = (isEmptyResult: boolean) => {
|
||||
setFetchState((prevState) => ( {...prevState, isEmptyResult} ));
|
||||
}
|
||||
|
||||
|
||||
const reFetch = () => {
|
||||
console.log("refetch called")
|
||||
setIsLoading(true);
|
||||
executeQuery(query).then((result) => {
|
||||
if("rows" in result[0]) {
|
||||
|
|
@ -16,6 +36,7 @@ const useFetch = (query: Query) => {
|
|||
if(result[0]["rows"].length == 0){
|
||||
setIsEmptyResult(true);
|
||||
}
|
||||
console.log("len", result[0]["rows"].length)
|
||||
}
|
||||
}).catch((error: any) => {
|
||||
console.error("Fetching data from database has failed: ", error);
|
||||
|
|
@ -28,7 +49,7 @@ const useFetch = (query: Query) => {
|
|||
reFetch();
|
||||
}, [])
|
||||
|
||||
return {data, isLoading, reFetch, isEmptyResult};
|
||||
return {...fetchState, reFetch};
|
||||
}
|
||||
|
||||
export default useFetch;
|
||||
Reference in a new issue