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
|
|
@ -6,9 +6,12 @@ import { ExpenseItem, LoadingSymbol, TextInputBar } from "../../../components";
|
||||||
import useFetch from "../../../hooks/useFetch";
|
import useFetch from "../../../hooks/useFetch";
|
||||||
import { useTheme } from "../../contexts/ThemeContext";
|
import { useTheme } from "../../contexts/ThemeContext";
|
||||||
import { SafeAreaView } from "react-native-safe-area-context";
|
import { SafeAreaView } from "react-native-safe-area-context";
|
||||||
|
import { useNavigation } from "expo-router/src/useNavigation";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const navigation = useNavigation();
|
||||||
const {colors} = useTheme();
|
const {colors} = useTheme();
|
||||||
const {category_guid} = useLocalSearchParams();
|
const {category_guid} = useLocalSearchParams();
|
||||||
|
|
||||||
|
|
@ -17,13 +20,21 @@ export default function Page() {
|
||||||
const {data, isLoading, reFetch} = useFetch({sql: "SELECT e.guid AS expense_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 FROM expense e JOIN category c ON e.category_guid = c.guid WHERE c.guid = ? ORDER BY expense_datetime desc;", args: [category_guid]});
|
const {data, isLoading, reFetch} = useFetch({sql: "SELECT e.guid AS expense_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 FROM expense e JOIN category c ON e.category_guid = c.guid WHERE c.guid = ? ORDER BY expense_datetime desc;", args: [category_guid]});
|
||||||
|
|
||||||
const handleEditCategory = () => {
|
const handleEditCategory = () => {
|
||||||
router.push({pathname: "/budget/editCategory", params: {category_guid: category_guid, category_color: category_color, category_amount: category_amount, category_name: category_name, category_type: category_type}});
|
router.push({pathname: "/editCategory", params: {category_guid: category_guid, category_color: category_color, category_amount: category_amount, category_name: category_name, category_type: category_type}});
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleBackButton = () => {
|
const handleBackButton = () => {
|
||||||
router.back();
|
router.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
console.log("useEffect called")
|
||||||
|
const unsubscribe = navigation.addListener("focus", () => {
|
||||||
|
reFetch();
|
||||||
|
})
|
||||||
|
return unsubscribe;
|
||||||
|
}, [navigation])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SafeAreaView style={[styles.safeAreaView, {backgroundColor: colors.containerColor}]}>
|
<SafeAreaView style={[styles.safeAreaView, {backgroundColor: colors.containerColor}]}>
|
||||||
<TouchableOpacity style={styles.backContainer} onPress={handleBackButton}>
|
<TouchableOpacity style={styles.backContainer} onPress={handleBackButton}>
|
||||||
|
|
@ -40,7 +51,7 @@ export default function Page() {
|
||||||
<TextInputBar style={styles.searchBar} placeholder="Search..."/>
|
<TextInputBar style={styles.searchBar} placeholder="Search..."/>
|
||||||
|
|
||||||
{isLoading ? (<LoadingSymbol/>) : (
|
{isLoading ? (<LoadingSymbol/>) : (
|
||||||
<FlatList
|
<FlatList style={{marginHorizontal: 10}}
|
||||||
data={data}
|
data={data}
|
||||||
renderItem = {({item}) => <ExpenseItem
|
renderItem = {({item}) => <ExpenseItem
|
||||||
color={item.category_color}
|
color={item.category_color}
|
||||||
|
|
@ -48,9 +59,9 @@ export default function Page() {
|
||||||
title={item.expense_name}
|
title={item.expense_name}
|
||||||
date={item.expense_datetime}
|
date={item.expense_datetime}
|
||||||
value={item.expense_amount}
|
value={item.expense_amount}
|
||||||
onPress={()=>router.push(`/home/expense/${item.guid}`)}
|
onPress={()=>router.push(`/expense/${item.expense_guid}`)}
|
||||||
/>}
|
/>}
|
||||||
keyExtractor={item => item.guid}
|
keyExtractor={item => item.expense_guid}
|
||||||
ItemSeparatorComponent={() => {
|
ItemSeparatorComponent={() => {
|
||||||
return (<View style={styles.itemSeperator}/>);
|
return (<View style={styles.itemSeperator}/>);
|
||||||
}}
|
}}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||||
import { router } from 'expo-router';
|
import { router, useNavigation } from 'expo-router';
|
||||||
import { useEffect, useState } from 'react';
|
import { 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';
|
||||||
|
|
@ -11,7 +11,7 @@ import { useTheme } from '../../contexts/ThemeContext';
|
||||||
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 [selectedPage, setSelectedPage] = useState("noPageLoaded");
|
const [selectedPage, setSelectedPage] = useState("noPageLoaded");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -27,9 +27,10 @@ export default function Page() {
|
||||||
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]);
|
||||||
|
|
||||||
|
|
||||||
const handlePageSelection = (page: string) => {
|
const handlePageSelection = (page: string) => {
|
||||||
if(page !== selectedPage) {
|
if(page !== selectedPage) {
|
||||||
setSelectedPage(page);
|
setSelectedPage(page);
|
||||||
|
|
@ -38,15 +39,16 @@ export default function Page() {
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleCategoryPress = (item: {[column: string]: any;}) => {
|
const handleCategoryPress = (item: {[column: string]: any;}) => {
|
||||||
router.push({pathname: "/(tabs)/budget/category", params: {category_guid: item.category_guid, category_name: item.category_name}})
|
router.push({pathname: "/category", params: {category_guid: item.category_guid, category_name: item.category_name}})
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SafeAreaView style={[styles.safeAreaViewStyle, {backgroundColor: containerColor}]}>
|
<SafeAreaView style={[styles.safeAreaViewStyle, {backgroundColor: containerColor}]}>
|
||||||
|
<View style={{flex: 1, marginHorizontal: 10}}>
|
||||||
<BudgetHeader selectedPage={selectedPage} handlePageSelection={handlePageSelection}/>
|
<BudgetHeader selectedPage={selectedPage} handlePageSelection={handlePageSelection}/>
|
||||||
|
|
||||||
<Plus onPress={() => {
|
<Plus onPress={() => {
|
||||||
router.push({pathname: '/(tabs)/budget/addCategory'});
|
router.push({pathname: '/addCategory'});
|
||||||
}}/>
|
}}/>
|
||||||
|
|
||||||
{isLoading ? (<LoadingSymbol/>) : (
|
{isLoading ? (<LoadingSymbol/>) : (
|
||||||
|
|
@ -67,6 +69,7 @@ export default function Page() {
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
</View>
|
||||||
</SafeAreaView>
|
</SafeAreaView>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -24,9 +24,6 @@ export default function _Layout() {
|
||||||
title: "test",
|
title: "test",
|
||||||
headerShown: false,
|
headerShown: false,
|
||||||
}}/>
|
}}/>
|
||||||
<Stack.Screen name="expense" options={{
|
|
||||||
headerShown: false,
|
|
||||||
}}/>
|
|
||||||
<Stack.Screen name="userSettings" options={{
|
<Stack.Screen name="userSettings" options={{
|
||||||
animation: "slide_from_left",
|
animation: "slide_from_left",
|
||||||
title: "User Settings",
|
title: "User Settings",
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { useMemo, useRef, useState } from 'react';
|
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||||
import { NativeScrollEvent, NativeSyntheticEvent, StyleSheet, View } from 'react-native';
|
import { NativeScrollEvent, NativeSyntheticEvent, StyleSheet, View } from 'react-native';
|
||||||
import { Calendar } from 'react-native-calendars';
|
import { Calendar } from 'react-native-calendars';
|
||||||
import { FlatList, RefreshControl } from 'react-native-gesture-handler';
|
import { FlatList, RefreshControl } from 'react-native-gesture-handler';
|
||||||
|
|
@ -10,6 +10,7 @@ import { useRouter } from "expo-router";
|
||||||
import { addExpense } from "../../../services/database";
|
import { addExpense } from "../../../services/database";
|
||||||
import { SimpleDate } from '../../../util/SimpleDate';
|
import { SimpleDate } from '../../../util/SimpleDate';
|
||||||
import { useTheme } from '../../contexts/ThemeContext';
|
import { useTheme } from '../../contexts/ThemeContext';
|
||||||
|
import { useNavigation } from 'expo-router';
|
||||||
|
|
||||||
|
|
||||||
interface MarkingProps {
|
interface MarkingProps {
|
||||||
|
|
@ -36,8 +37,7 @@ const constructMarkedDates = (data : {[column: string]: any}) => {
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
const { colors, theme } = useTheme()
|
const { colors, theme } = useTheme()
|
||||||
|
|
||||||
|
const navigation = useNavigation();
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [plusShow, setPlusShow] = useState(true);
|
const [plusShow, setPlusShow] = useState(true);
|
||||||
const prevOffset = useRef(0);
|
const prevOffset = useRef(0);
|
||||||
|
|
@ -67,12 +67,19 @@ export default function Page() {
|
||||||
constructMarkedDates(data)
|
constructMarkedDates(data)
|
||||||
, [data])
|
, [data])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const unsubscribe = navigation.addListener('focus', () => {
|
||||||
|
console.log("focus event triggered")
|
||||||
|
reFetch();
|
||||||
|
});
|
||||||
|
return unsubscribe;
|
||||||
|
}, [navigation]);
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SafeAreaView edges={["left", "right", "top"]} style={[styles.safeAreaViewStyle, {backgroundColor: colors.containerColor}]}>
|
<SafeAreaView edges={["left", "right", "top"]} style={[styles.safeAreaViewStyle, {backgroundColor: colors.containerColor}]}>
|
||||||
{plusShow && <Plus onPress={()=>{
|
{plusShow && <Plus onPress={()=>{
|
||||||
router.push("/(tabs)/home/expense/new");
|
router.push("/expense/new");
|
||||||
|
|
||||||
// executeQuery({sql: "SELECT guid FROM category", args: []}).then((result) => {
|
// executeQuery({sql: "SELECT guid FROM category", args: []}).then((result) => {
|
||||||
// if("rows" in result[0]) {
|
// if("rows" in result[0]) {
|
||||||
|
|
@ -89,7 +96,7 @@ export default function Page() {
|
||||||
data={data}
|
data={data}
|
||||||
ListHeaderComponent={
|
ListHeaderComponent={
|
||||||
<>
|
<>
|
||||||
<Welcome name="My Dude" image={profile} onPress={() => {router.push("/home/userSettings")}}/>
|
<Welcome name="My Dude" image={profile} onPress={() => {router.push("/userSettings")}}/>
|
||||||
<Calendar key={theme} maxDate={SimpleDate.now().format("YYYY-MM-DD")} style={{borderRadius: 20, padding:10}} theme={{
|
<Calendar key={theme} maxDate={SimpleDate.now().format("YYYY-MM-DD")} style={{borderRadius: 20, padding:10}} theme={{
|
||||||
dayTextColor: colors.primaryText,
|
dayTextColor: colors.primaryText,
|
||||||
textDisabledColor: colors.secondaryText,
|
textDisabledColor: colors.secondaryText,
|
||||||
|
|
@ -107,8 +114,17 @@ export default function Page() {
|
||||||
<TextInputBar placeholder='Type to Search...' style={{marginBottom: 20}}></TextInputBar>
|
<TextInputBar placeholder='Type to Search...' style={{marginBottom: 20}}></TextInputBar>
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
renderItem = {({item}) => <ExpenseItem category={item.category_name} color={item.category_color} date={item.expense_datetime} title={item.expense_name} value={item.expense_amount} guid={item.expense_guid} onPress={(guid) => {router.push(`/(tabs)/home/expense/${guid}`)}}/>}
|
renderItem = {({item}) =>
|
||||||
keyExtractor={item => item.expense_guid}
|
<ExpenseItem
|
||||||
|
category={item.category_name}
|
||||||
|
color={item.category_color}
|
||||||
|
date={item.expense_datetime}
|
||||||
|
title={item.expense_name}
|
||||||
|
value={item.expense_amount}
|
||||||
|
guid={item.expense_guid}
|
||||||
|
onPress={(guid) => {router.push(`/expense/${guid}`)}}
|
||||||
|
/>}
|
||||||
|
keyExtractor={item => item.expense_guid }
|
||||||
ItemSeparatorComponent={() => {
|
ItemSeparatorComponent={() => {
|
||||||
return (<View style={styles.itemSeperatorStyle}/>);
|
return (<View style={styles.itemSeperatorStyle}/>);
|
||||||
}}
|
}}
|
||||||
|
|
@ -35,30 +35,25 @@ export default function Layout() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tabs sceneContainerStyle={styles.sceneContainer} screenOptions={screenOptions}>
|
<Tabs sceneContainerStyle={styles.sceneContainer} screenOptions={screenOptions}>
|
||||||
<Tabs.Screen name="budget" options={
|
<Tabs.Screen name="(budget)" options={
|
||||||
{
|
{
|
||||||
tabBarLabel: "Budget",
|
tabBarLabel: "Budget",
|
||||||
tabBarIcon: ({size, color}) => (
|
tabBarIcon: ({size, color}) => (
|
||||||
<FontAwesome name="money" size={size} color={color}/>),
|
<FontAwesome name="money" size={size} color={color}/>),
|
||||||
unmountOnBlur: true,
|
|
||||||
href: "(tabs)/budget"
|
|
||||||
}
|
}
|
||||||
}/>
|
}/>
|
||||||
<Tabs.Screen name="home" options={
|
<Tabs.Screen name="(home)" options={
|
||||||
{
|
{
|
||||||
tabBarLabel: "Home",
|
tabBarLabel: "Home",
|
||||||
tabBarIcon: ({size, color}) => (
|
tabBarIcon: ({size, color}) => (
|
||||||
<FontAwesome name="home" size={size} color={color}/>),
|
<FontAwesome name="home" size={size} color={color}/>),
|
||||||
unmountOnBlur: true,
|
|
||||||
href: "(tabs)/home/"
|
|
||||||
}
|
}
|
||||||
}/>
|
}/>
|
||||||
<Tabs.Screen name="stats/index" options={
|
<Tabs.Screen name="(stats)/index" options={
|
||||||
{
|
{
|
||||||
tabBarLabel: "Stats",
|
tabBarLabel: "Stats",
|
||||||
tabBarIcon: ({size, color}) => (
|
tabBarIcon: ({size, color}) => (
|
||||||
<FontAwesome name="bar-chart" size={size} color={color}/>),
|
<FontAwesome name="bar-chart" size={size} color={color}/>),
|
||||||
unmountOnBlur: true,
|
|
||||||
}
|
}
|
||||||
}/>
|
}/>
|
||||||
</Tabs>
|
</Tabs>
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,37 @@
|
||||||
import { Slot } from 'expo-router';
|
import { SplashScreen, Stack } from 'expo-router';
|
||||||
import React, { useEffect } from 'react';
|
import React, { useEffect } from 'react';
|
||||||
import { addCategory, initDatabase } from '../services/database';
|
import { addCategory, initDatabase } from '../services/database';
|
||||||
import { AuthProvider } from './contexts/AuthContext';
|
import { AuthProvider } from './contexts/AuthContext';
|
||||||
import { ThemeProvider } from './contexts/ThemeContext';
|
import { ThemeProvider } from './contexts/ThemeContext';
|
||||||
|
import { useTheme } from './contexts/ThemeContext';
|
||||||
|
|
||||||
|
|
||||||
export default function _layout() {
|
export default function _layout() {
|
||||||
|
const {colors} = useTheme();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
initDatabase();
|
initDatabase();
|
||||||
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
console.log(colors.backgroundColor)
|
||||||
return (
|
return (
|
||||||
<AuthProvider>
|
<ThemeProvider>
|
||||||
<ThemeProvider>
|
<AuthProvider>
|
||||||
<Slot />
|
<Stack
|
||||||
</ThemeProvider>
|
screenOptions={{
|
||||||
</AuthProvider>
|
headerShown: false,
|
||||||
|
navigationBarHidden: true,
|
||||||
|
animation: 'none',
|
||||||
|
contentStyle: {backgroundColor: colors.backgroundColor}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Stack.Screen name="index"
|
||||||
|
options={{
|
||||||
|
contentStyle: {backgroundColor: colors.backgroundColor}
|
||||||
|
}}/>
|
||||||
|
|
||||||
|
</Stack>
|
||||||
|
</AuthProvider>
|
||||||
|
</ThemeProvider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -1,15 +1,15 @@
|
||||||
import { View, Text, Alert, StyleSheet } from 'react-native'
|
import { View, Text, Alert, StyleSheet } from 'react-native'
|
||||||
import React, { useEffect, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { router, useLocalSearchParams, useRouter } from 'expo-router'
|
import { router, useLocalSearchParams, useRouter } from 'expo-router'
|
||||||
import useFetch from '../../../../hooks/useFetch';
|
import useFetch from '../../hooks/useFetch';
|
||||||
import { Category, Expense } from '../../../../types/dbItems';
|
import { Category, Expense } from '../../types/dbItems';
|
||||||
import { CategorySelectorModal, AutoDecimalInput, CategorySelector, TextInputBar, DateSelectorButton, RoundedButton } from '../../../../components';
|
import { CategorySelectorModal, AutoDecimalInput, CategorySelector, TextInputBar, DateSelectorButton, RoundedButton } from '../../components';
|
||||||
import colors from '../../../../constants/colors';
|
import colors from '../../constants/colors';
|
||||||
import { addExpense, deleteExpense, executeQuery, updateExpense } from '../../../../services/database';
|
import { addExpense, deleteExpense, executeQuery, updateExpense } from '../../services/database';
|
||||||
import { SimpleDate } from '../../../../util/SimpleDate';
|
import { SimpleDate } from '../../util/SimpleDate';
|
||||||
import DateTimePicker from '@react-native-community/datetimepicker';
|
import DateTimePicker from '@react-native-community/datetimepicker';
|
||||||
import { SIZES } from '../../../../constants/theme';
|
import { SIZES } from '../../constants/theme';
|
||||||
import { useTheme } from '../../../contexts/ThemeContext';
|
import { useTheme } from '../contexts/ThemeContext';
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
@ -25,8 +25,6 @@ export default function Page() {
|
||||||
const [datePickerShown, setDatePickerShown] = useState<boolean>(false);
|
const [datePickerShown, setDatePickerShown] = useState<boolean>(false);
|
||||||
const [selectedDate, setSelectedDate] = useState<Date>(new Date());
|
const [selectedDate, setSelectedDate] = useState<Date>(new Date());
|
||||||
|
|
||||||
|
|
||||||
console.log(router.canGoBack())
|
|
||||||
useEffect(()=>{
|
useEffect(()=>{
|
||||||
const entry = data[0];
|
const entry = data[0];
|
||||||
console.log(entry)
|
console.log(entry)
|
||||||
|
|
@ -69,8 +67,7 @@ export default function Page() {
|
||||||
await updateExpense(selectedExpense!.guid!, expenseName, selectedCategory!.guid!, new SimpleDate(selectedDate).format("YYYY-MM-DD"), Number(formatedValue))
|
await updateExpense(selectedExpense!.guid!, expenseName, selectedCategory!.guid!, new SimpleDate(selectedDate).format("YYYY-MM-DD"), Number(formatedValue))
|
||||||
}
|
}
|
||||||
if(validateInput()){
|
if(validateInput()){
|
||||||
insert();
|
insert().then( () => router.back())
|
||||||
router.back();
|
|
||||||
}else {
|
}else {
|
||||||
Alert.alert("Invalid input", "One of the Props is not properly defined")
|
Alert.alert("Invalid input", "One of the Props is not properly defined")
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { StyleSheet, Text, View } from 'react-native'
|
import { StyleSheet, Text, View } from 'react-native'
|
||||||
import { Stack } from 'expo-router'
|
import { Stack } from 'expo-router'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { useTheme } from '../../../contexts/ThemeContext'
|
import { useTheme } from '../contexts/ThemeContext'
|
||||||
|
|
||||||
const _layout = () => {
|
const _layout = () => {
|
||||||
const {colors} = useTheme();
|
const {colors} = useTheme();
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
import { View, Text, StyleSheet, Alert } from 'react-native'
|
import { View, Text, StyleSheet, Alert } from 'react-native'
|
||||||
import React, { useRef, useState } from 'react'
|
import React, { useRef, useState } from 'react'
|
||||||
import { SIZES } from '../../../../constants/theme'
|
import { SIZES } from '../../constants/theme'
|
||||||
import { useTheme } from '../../../contexts/ThemeContext'
|
import { useTheme } from '../contexts/ThemeContext'
|
||||||
import { AutoDecimalInput, CategorySelector, CategorySelectorModal, DateSelectorButton, RoundedButton, TextInputBar } from '../../../../components'
|
import { AutoDecimalInput, CategorySelector, CategorySelectorModal, DateSelectorButton, RoundedButton, TextInputBar } from '../../components'
|
||||||
import { Category } from '../../../../types/dbItems'
|
import { Category } from '../../types/dbItems'
|
||||||
import DateTimePicker from '@react-native-community/datetimepicker';
|
import DateTimePicker from '@react-native-community/datetimepicker';
|
||||||
import { addExpense } from '../../../../services/database'
|
import { addExpense } from '../../services/database'
|
||||||
import { SimpleDate } from '../../../../util/SimpleDate'
|
import { SimpleDate } from '../../util/SimpleDate'
|
||||||
import { useRouter } from 'expo-router'
|
import { useRouter } from 'expo-router'
|
||||||
|
|
||||||
export default function AddItem() {
|
export default function AddItem() {
|
||||||
|
|
@ -41,8 +41,9 @@ export default function AddItem() {
|
||||||
await addExpense(expenseName, selectedCategory?.guid!, new SimpleDate(selectedDate).format("YYYY-MM-DD"), Number(formatedValue))
|
await addExpense(expenseName, selectedCategory?.guid!, new SimpleDate(selectedDate).format("YYYY-MM-DD"), Number(formatedValue))
|
||||||
}
|
}
|
||||||
if(validateInput()){
|
if(validateInput()){
|
||||||
insert();
|
insert().then(() => {
|
||||||
router.back();
|
router.back();
|
||||||
|
})
|
||||||
}else {
|
}else {
|
||||||
Alert.alert("Invalid input", "One of the Props is not properly defined")
|
Alert.alert("Invalid input", "One of the Props is not properly defined")
|
||||||
}
|
}
|
||||||
|
|
@ -7,7 +7,7 @@ export default function index() {
|
||||||
const {authState} = useAuth()
|
const {authState} = useAuth()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Redirect href={authState?.authenticated ? "/home/" : "/login"}></Redirect>
|
<Redirect href={authState?.authenticated ? "/(tabs)/(home)" : "/login"}></Redirect>
|
||||||
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -77,12 +77,11 @@ const styles = StyleSheet.create({
|
||||||
containerStyle: {
|
containerStyle: {
|
||||||
flexDirection: "row",
|
flexDirection: "row",
|
||||||
justifyContent: "space-evenly",
|
justifyContent: "space-evenly",
|
||||||
marginHorizontal: 20,
|
|
||||||
marginBottom: 20,
|
marginBottom: 20,
|
||||||
|
marginHorizontal: 10,
|
||||||
marginTop: 10,
|
marginTop: 10,
|
||||||
},
|
},
|
||||||
searchBarStyle: {
|
searchBarStyle: {
|
||||||
marginBottom: 20,
|
marginBottom: 20,
|
||||||
marginHorizontal: 10,
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -40,7 +40,6 @@ export default function CustomCard(props : ViewProps) {
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
container:{
|
container:{
|
||||||
borderRadius: 20,
|
borderRadius: 20,
|
||||||
marginHorizontal: 10,
|
|
||||||
},
|
},
|
||||||
boxShadow: {},
|
boxShadow: {},
|
||||||
})
|
})
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'
|
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'
|
||||||
import React, { useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
import { useTheme } from '../../../app/contexts/ThemeContext'
|
import { useTheme } from '../../app/contexts/ThemeContext'
|
||||||
import { SIZES } from '../../../constants/theme';
|
import { SIZES } from '../../constants/theme';
|
||||||
import { Category } from '../../../types/dbItems';
|
import { Category } from '../../types/dbItems';
|
||||||
import CategorySelectorModal from './CategorySelectorModal';
|
import CategorySelectorModal from './CategorySelectorModal';
|
||||||
|
|
||||||
interface CategorySelectorProps {
|
interface CategorySelectorProps {
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
import { Modal, NativeSyntheticEvent, StyleSheet, Text, View, FlatList } from 'react-native'
|
import { Modal, NativeSyntheticEvent, StyleSheet, Text, View, FlatList } from 'react-native'
|
||||||
import React, { useEffect, useMemo, useState } from 'react'
|
import React, { useEffect, useMemo, useState } from 'react'
|
||||||
import { Category } from '../../../types/dbItems';
|
import { Category } from '../../types/dbItems';
|
||||||
import { useTheme } from '../../../app/contexts/ThemeContext';
|
import { useTheme } from '../../app/contexts/ThemeContext';
|
||||||
import CategoryListItem from '../../common/CategoryListItem';
|
import CategoryListItem from '../common/CategoryListItem';
|
||||||
import { SIZES } from '../../../constants/theme';
|
import { SIZES } from '../../constants/theme';
|
||||||
import useFetch from '../../../hooks/useFetch';
|
import useFetch from '../../hooks/useFetch';
|
||||||
import TextInputBar from '../../common/TextInputBar';
|
import TextInputBar from '../common/TextInputBar';
|
||||||
|
|
||||||
|
|
||||||
interface CategorySelectorModalProps{
|
interface CategorySelectorModalProps{
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { View, Text, StyleSheet, TouchableOpacity, TouchableOpacityProps } from 'react-native'
|
import { View, Text, StyleSheet, TouchableOpacity, TouchableOpacityProps } from 'react-native'
|
||||||
import React, { useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
import { useTheme } from '../../../app/contexts/ThemeContext';
|
import { useTheme } from '../../app/contexts/ThemeContext';
|
||||||
import { SIZES } from '../../../constants/theme';
|
import { SIZES } from '../../constants/theme';
|
||||||
import { SimpleDate } from '../../../util/SimpleDate';
|
import { SimpleDate } from '../../util/SimpleDate';
|
||||||
import { ViewProps } from 'react-native/Libraries/Components/View/ViewPropTypes';
|
import { ViewProps } from 'react-native/Libraries/Components/View/ViewPropTypes';
|
||||||
|
|
||||||
interface DateSelectorProps extends ViewProps {
|
interface DateSelectorProps extends ViewProps {
|
||||||
|
|
@ -4,9 +4,9 @@ import Welcome from "./home/Welcome"
|
||||||
import ExpenseItem from "./home/expenseItem"
|
import ExpenseItem from "./home/expenseItem"
|
||||||
|
|
||||||
//home/addItem
|
//home/addItem
|
||||||
import CategorySelector from "./home/addItem/CategorySelector"
|
import CategorySelector from "./expense/CategorySelector"
|
||||||
import CategorySelectorModal from "./home/addItem/CategorySelectorModal"
|
import CategorySelectorModal from "./expense/CategorySelectorModal"
|
||||||
import DateSelectorButton from "./home/addItem/DateSelectorButton"
|
import DateSelectorButton from "./expense/DateSelectorButton"
|
||||||
|
|
||||||
//common
|
//common
|
||||||
import CustomCard from "./common/CustomCard"
|
import CustomCard from "./common/CustomCard"
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,31 @@ import { executeQuery } from "../services/database";
|
||||||
|
|
||||||
const useFetch = (query: Query) => {
|
const useFetch = (query: Query) => {
|
||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [fetchState, setFetchState] = useState<{
|
||||||
const [data, setData] = useState<{[column: string]: any;}[]>([]);
|
data: {[column: string]: any;}[];
|
||||||
const [isEmptyResult, setIsEmptyResult] = useState<boolean | undefined>(undefined);
|
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 = () => {
|
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]) {
|
||||||
|
|
@ -16,6 +36,7 @@ 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);
|
||||||
|
|
@ -28,7 +49,7 @@ const useFetch = (query: Query) => {
|
||||||
reFetch();
|
reFetch();
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
return {data, isLoading, reFetch, isEmptyResult};
|
return {...fetchState, reFetch};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useFetch;
|
export default useFetch;
|
||||||
|
|
@ -57,7 +57,7 @@ export const addCategory = async (name: string, color: string, type: string, all
|
||||||
export const updateExpense = async (guid: string, name: string, category_guid: string, datetime: string, amount: number) => {
|
export const updateExpense = async (guid: string, name: string, category_guid: string, datetime: string, amount: number) => {
|
||||||
|
|
||||||
//needs user input validation for type and color (should be validated by this function)
|
//needs user input validation for type and color (should be validated by this function)
|
||||||
|
console.log("update expense called")
|
||||||
try {
|
try {
|
||||||
await db.transactionAsync(async (tx) => {
|
await db.transactionAsync(async (tx) => {
|
||||||
await tx.executeSqlAsync("UPDATE expense SET name = ?, category_guid = ?, datetime = ?, amount = ? WHERE guid = ?", [name, category_guid, datetime, amount, guid]
|
await tx.executeSqlAsync("UPDATE expense SET name = ?, category_guid = ?, datetime = ?, amount = ? WHERE guid = ?", [name, category_guid, datetime, amount, guid]
|
||||||
|
|
@ -67,6 +67,7 @@ export const updateExpense = async (guid: string, name: string, category_guid: s
|
||||||
console.log("Error updating expense: ", error);
|
console.log("Error updating expense: ", error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
console.log("update expense finished")
|
||||||
};
|
};
|
||||||
|
|
||||||
export const addExpense = async (name: string, category_guid: string, datetime: string, amount: number) => {
|
export const addExpense = async (name: string, category_guid: string, datetime: string, amount: number) => {
|
||||||
|
|
|
||||||
Reference in a new issue