debugging fetch on budget screen

This commit is contained in:
Thomas Schleicher 2024-01-02 08:48:50 +01:00 committed by Jakob Stornig
parent 8931386239
commit 85c737e66c
3 changed files with 35 additions and 32 deletions

View file

@ -26,12 +26,15 @@ export default function Page() {
const {data, isLoading, reFetch} = useFetch({sql: "SELECT guid as category_guid, name as category_name, color as category_color FROM category WHERE type = ?", args: selectedPage === "expenses" ? ["expense"] : selectedPage === "savings" ? ["saving"] : []}); const {data, isLoading, reFetch} = useFetch({sql: "SELECT guid as category_guid, name as category_name, color as category_color FROM category WHERE type = ?", args: selectedPage === "expenses" ? ["expense"] : selectedPage === "savings" ? ["saving"] : []});
useEffect(() => { useEffect(() => {
console.log("reFetch()");
reFetch(); reFetch();
}, [selectedPage]); }, [selectedPage]);
const handlePageSelection = (page: string) => { const handlePageSelection = (page: string) => {
if(page !== selectedPage) {
setSelectedPage(page); setSelectedPage(page);
AsyncStorage.setItem("currentBudgetPage", page); AsyncStorage.setItem("currentBudgetPage", page);
}
}; };
return ( return (
@ -40,14 +43,13 @@ export default function Page() {
{isLoading ? (<LoadingSymbol/>) : ( {isLoading ? (<LoadingSymbol/>) : (
<FlatList <FlatList
data={data} data={data}
renderItem = {({item}) => <CategoryItem category={item.category_name} allocated_account={100} color={item.category_color}/>} renderItem = {({item}) => <CategoryItem category={item.category_name} allocated_ammount={100} color={item.category_color} category_guid={item.category_guid}/>}
keyExtractor={item => item.category_guid} keyExtractor={item => item.category_guid}
ItemSeparatorComponent={() => { ItemSeparatorComponent={() => {
return (<View style={styles.itemSeperatorStyle}/>); return (<View style={styles.itemSeperatorStyle}/>);
}} }}
/> />
)} )}
{/* <LoadingSymbol/> */}
</SafeAreaView> </SafeAreaView>
); );
} }

View file

@ -1,32 +1,29 @@
import { ColorValue, StyleSheet, Text, View } from "react-native"; import { ColorValue, StyleSheet, Text, View } from "react-native";
import { useThemeColor } from "../../hooks/useThemeColor"; import { useTheme } from "../../app/contexts/ThemeContext";
import CustomCard from "../common/CustomCard"; import CustomCard from "../common/CustomCard";
export type CategoryItemProps = { export type CategoryItemProps = {
category: string, category: string,
color: ColorValue, color: ColorValue,
allocated_account: number, allocated_ammount: number,
category_guid: string,
} }
const CategoryItem = (properties: CategoryItemProps) => { const CategoryItem = (properties: CategoryItemProps) => {
const textColor = useThemeColor("primaryText"); const { colors } = useTheme();
const backgroundColor = useThemeColor("backgroundColor");
const subText = `${calculateSumOfExpenses(properties.category_guid)} / ${properties.allocated_ammount}`;
return ( return (
<CustomCard> <CustomCard>
<View style={[styles.colorTipStyle, {backgroundColor: properties.color}]}></View> <View style={[styles.colorTipStyle, {backgroundColor: properties.color}]}></View>
<View style={[styles.textViewStyle]}> <View style={[styles.textViewStyle]}>
<Text style={[styles.textViewTextStyle, {color: textColor}]}> <Text style={[styles.categoryNameStyle, {color: colors.primaryText}]}>
{properties.category} {properties.category}
</Text> </Text>
</View> <Text style={[styles.subTextStyle, {color: colors.secondaryText}]}>
{subText}
<View style={[styles.valueViewStyle]}>
<Text style={[styles.valueViewTextStyle, {color: textColor}]}>
10/100$
{/* {properties.allocated_account} */}
</Text> </Text>
</View> </View>
</CustomCard> </CustomCard>
@ -42,22 +39,27 @@ const styles = StyleSheet.create({
borderBottomLeftRadius: 10, borderBottomLeftRadius: 10,
}, },
textViewStyle: { textViewStyle: {
flex: 2,
flexDirection: "column",
paddingVertical: 5, paddingVertical: 5,
paddingLeft: 10, paddingHorizontal: 10,
alignSelf: "stretch", alignSelf: "stretch",
}, },
textViewTextStyle: { categoryNameStyle: {
fontSize: 30, fontSize: 30,
fontWeight: "bold", fontWeight: "bold",
}, },
valueViewStyle: { subTextStyle: {
alignSelf: "stretch", fontSize: 17.5,
flex: 1,
flexDirection: "row-reverse",
alignItems: "center",
paddingHorizontal: 10,
},
valueViewTextStyle: {
fontSize: 20,
} }
}) })
const calculateSumOfExpenses = (category_guid: string) => {
// const { data } = useFetch({sql: "SELECT amount FROM expense WHERE category_guid = ?", args: [category_guid]});
// let sum: number = 0;
// console.log(data);
console.log("render");
return 0;
}

View file

@ -8,7 +8,6 @@ const useFetch = (query: Query) => {
const [data, setData] = useState<{[column: string]: any;}[]>([]); const [data, setData] = useState<{[column: string]: any;}[]>([]);
const reFetch = () => { const reFetch = () => {
setIsLoading(true); setIsLoading(true);
executeQuery(query).then((result) => { executeQuery(query).then((result) => {
if("rows" in result[0]) { if("rows" in result[0]) {
@ -16,7 +15,7 @@ const useFetch = (query: Query) => {
} }
}).catch((error: any) => { }).catch((error: any) => {
console.error("Fetching data from database has failed: ", error); console.error("Fetching data from database has failed: ", error);
}).finally(() => { }).then(() => {
setIsLoading(false); setIsLoading(false);
}); });
} }