Fix: refresh

This commit is contained in:
Jakob Stornig 2024-01-25 16:11:04 +01:00
parent 4ddc76faa4
commit c46eec06a5
4 changed files with 15 additions and 10 deletions

View file

@ -51,8 +51,7 @@ export default function Page() {
router.back(); router.back();
}}/> }}/>
<NavigationButton text="Save" onPress={() => { <NavigationButton text="Save" onPress={() => {
addCategory(categoryName, categoryColor, selectedType, amount); addCategory(categoryName, categoryColor, selectedType, amount).then(() => router.back());
router.back();
}}/> }}/>
</View> </View>
</View> </View>

View file

@ -1,20 +1,19 @@
import AsyncStorage from '@react-native-async-storage/async-storage'; import AsyncStorage from '@react-native-async-storage/async-storage';
import { useIsFocused } from '@react-navigation/native';
import { router, useNavigation } from 'expo-router'; import { router, useNavigation } from 'expo-router';
import { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { StyleSheet, View } from 'react-native'; import { StyleSheet, View } from 'react-native';
import { FlatList } from 'react-native-gesture-handler'; import { FlatList } from 'react-native-gesture-handler';
import { SafeAreaView } from 'react-native-safe-area-context'; import { SafeAreaView } from 'react-native-safe-area-context';
import { BudgetHeader, CategoryItem, LoadingSymbol, Plus } from '../../../components'; import { BudgetHeader, CategoryItem, LoadingSymbol, Plus } from '../../../components';
import useFetch from '../../../hooks/useFetch'; import useFetch from '../../../hooks/useFetch';
import { useTheme } from '../../contexts/ThemeContext'; import { useTheme } from '../../contexts/ThemeContext';
import { useFocusEffect } from 'expo-router/src/useFocusEffect';
export default function Page() { export default function Page() {
const {colors} = useTheme() const {colors} = useTheme()
const containerColor = colors.containerColor; const containerColor = colors.containerColor;
const navigation = useNavigation(); const navigation = useNavigation();
const [selectedPage, setSelectedPage] = useState("noPageLoaded"); const [selectedPage, setSelectedPage] = useState("noPageLoaded");
const isFocused = useIsFocused();
useEffect(() => { useEffect(() => {
AsyncStorage.getItem("currentBudgetPage").then((page) => { AsyncStorage.getItem("currentBudgetPage").then((page) => {
@ -24,14 +23,23 @@ export default function Page() {
}).catch((error) => { }).catch((error) => {
console.log("Error fetching previous page from Async Storage:", error); console.log("Error fetching previous page from Async Storage:", error);
}) })
}, [isFocused]); }, []);
const {data, isLoading, reFetch} = useFetch({sql: "SELECT c.guid AS category_guid, c.name AS category_name, c.color AS category_color, c.type AS category_type, SUM(e.amount) as total_expenses, c.allocated_amount as allocated_amount FROM category c LEFT JOIN expense e ON e.category_guid = c.guid WHERE c.type = ? GROUP BY c.guid", args: selectedPage === "expenses" ? ["expense"] : selectedPage === "savings" ? ["saving"] : []}); const {data, isLoading, reFetch} = useFetch({sql: "SELECT c.guid AS category_guid, c.name AS category_name, c.color AS category_color, c.type AS category_type, SUM(e.amount) as total_expenses, c.allocated_amount as allocated_amount FROM category c LEFT JOIN expense e ON e.category_guid = c.guid WHERE c.type = ? GROUP BY c.guid", args: selectedPage === "expenses" ? ["expense"] : selectedPage === "savings" ? ["saving"] : []});
useEffect(() => { useEffect(() => {
reFetch(); reFetch()
}, [selectedPage]); }, [selectedPage]);
useEffect(() => {
const unsubscribe = navigation.addListener("focus", () => {
reFetch();
})
return unsubscribe;
}, [navigation])
const handlePageSelection = (page: string) => { const handlePageSelection = (page: string) => {
if(page !== selectedPage) { if(page !== selectedPage) {
setSelectedPage(page); setSelectedPage(page);

View file

@ -34,7 +34,7 @@ export default function Layout() {
} }
return ( return (
<Tabs sceneContainerStyle={styles.sceneContainer} screenOptions={screenOptions}> <Tabs sceneContainerStyle={styles.sceneContainer} backBehavior="initialRoute" initialRouteName="(home)" screenOptions={screenOptions}>
<Tabs.Screen name="(budget)" options={ <Tabs.Screen name="(budget)" options={
{ {
tabBarLabel: "Budget", tabBarLabel: "Budget",

View file

@ -28,7 +28,6 @@ const useFetch = (query: Query) => {
const reFetch = () => { const reFetch = () => {
console.log("refetch called")
setIsLoading(true); setIsLoading(true);
executeQuery(query).then((result) => { executeQuery(query).then((result) => {
if("rows" in result[0]) { if("rows" in result[0]) {
@ -36,7 +35,6 @@ const useFetch = (query: Query) => {
if(result[0]["rows"].length == 0){ if(result[0]["rows"].length == 0){
setIsEmptyResult(true); setIsEmptyResult(true);
} }
console.log("len", result[0]["rows"].length)
} }
}).catch((error: any) => { }).catch((error: any) => {
console.error("Fetching data from database has failed: ", error); console.error("Fetching data from database has failed: ", error);