implemented delete button and fixed routing interaction with db

This commit is contained in:
thschleicher 2024-01-25 16:06:52 +01:00
parent 7a77b9ca79
commit 7fecf8c5d9

View file

@ -2,7 +2,8 @@ import { router, useLocalSearchParams } from "expo-router";
import { useState } from "react"; import { useState } from "react";
import { SafeAreaView, StyleSheet, Text, TextInput, View } from "react-native"; import { SafeAreaView, StyleSheet, Text, TextInput, View } from "react-native";
import { AutoDecimalInput, CustomColorPicker, NavigationButton, TypeSelectorSwitch } from "../../../components"; import { AutoDecimalInput, CustomColorPicker, NavigationButton, TypeSelectorSwitch } from "../../../components";
import { updateCategory } from "../../../services/database"; import useFetch from "../../../hooks/useFetch";
import { deleteCategory, deleteExpense, updateCategory } from "../../../services/database";
import { useTheme } from "../../contexts/ThemeContext"; import { useTheme } from "../../contexts/ThemeContext";
const addCategory = () => { const addCategory = () => {
@ -14,6 +15,8 @@ const addCategory = () => {
const [selectedType, setSelectedType] = useState(category_type.toString()); const [selectedType, setSelectedType] = useState(category_type.toString());
const [amount, setAmount] = useState(Number.parseFloat(category_amount.toString())); const [amount, setAmount] = useState(Number.parseFloat(category_amount.toString()));
const {data} = useFetch({sql: "SELECT * FROM expense WHERE category_guid = ?", args: [category_guid.toString()]});
return ( return (
<SafeAreaView style={[styles.safeAreaViewStyle, {backgroundColor: colors.backgroundColor}]}> <SafeAreaView style={[styles.safeAreaViewStyle, {backgroundColor: colors.backgroundColor}]}>
<Text style={[styles.headingTextStyle, {color: colors.primaryText}]}>Edit Category</Text> <Text style={[styles.headingTextStyle, {color: colors.primaryText}]}>Edit Category</Text>
@ -44,13 +47,27 @@ const addCategory = () => {
}}/> }}/>
</View> </View>
<View style={styles.deleteButtonView}>
<NavigationButton text={"Delete Cateogry"} onPress={() => {
for (let i = 0; i < data.length;) {
deleteExpense(data[i].guid).then(() => {
i++
});
}
deleteCategory(category_guid.toString()).then(() => {
router.push("/(tabs)/(budget)");
});
}}/>
</View>
<View style={styles.navigationButtonViewStyle}> <View style={styles.navigationButtonViewStyle}>
<NavigationButton text="Back" onPress={() => { <NavigationButton text="Back" onPress={() => {
router.back(); router.back();
}}/> }}/>
<NavigationButton text="Save" onPress={() => { <NavigationButton text="Save" onPress={() => {
updateCategory(category_guid.toString(), categoryName, categoryColor, selectedType, amount); updateCategory(category_guid.toString(), categoryName, categoryColor, selectedType, amount).then(() => {
router.back(); router.back();
});
}}/> }}/>
</View> </View>
</View> </View>
@ -61,6 +78,9 @@ const addCategory = () => {
export default addCategory; export default addCategory;
const styles = StyleSheet.create({ const styles = StyleSheet.create({
deleteButtonView: {
},
containerStyle: { containerStyle: {
flex: 1, flex: 1,
margin: 10, margin: 10,