29 lines
No EOL
1 KiB
TypeScript
29 lines
No EOL
1 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { executeQuery } from "../services/database";
|
|
|
|
const useFetch = () => {
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [data, setData] = useState<{[column: string]: any;}[]>([]);
|
|
|
|
const reFetch = () => {
|
|
setIsLoading(true);
|
|
executeQuery("SELECT e.guid AS expense_guid, c.guid AS category_guid, e.name AS expense_name, c.name AS category_name, e.datetime AS expense_datetime, e.amount AS expense_amount, c.color AS category_color, c.type AS category_type FROM expense e JOIN category c ON e.category_guid = c.guid;").then((result) => {
|
|
if("rows" in result[0]) {
|
|
setData(result[0]["rows"]);
|
|
}
|
|
}).catch((error: any) => {
|
|
console.error("Fetching data from database has failed: ", error);
|
|
}).finally(() => {
|
|
setIsLoading(false);
|
|
});
|
|
}
|
|
|
|
useEffect(() => {
|
|
reFetch();
|
|
}, [])
|
|
|
|
return {data, isLoading, reFetch};
|
|
}
|
|
|
|
export default useFetch; |