diff --git a/app/(tabs)/(budget)/addCategory.tsx b/app/(tabs)/(budget)/addCategory.tsx index a6c53b6..eced44a 100644 --- a/app/(tabs)/(budget)/addCategory.tsx +++ b/app/(tabs)/(budget)/addCategory.tsx @@ -5,6 +5,7 @@ import { SafeAreaView } from 'react-native-safe-area-context'; import { AutoDecimalInput, CustomColorPicker, NavigationButton, TypeSelectorSwitch } from "../../../components"; import { addCategory } from "../../../services/database"; import { useTheme } from "../../contexts/ThemeContext"; +import { CategoryType } from "../../../services/database"; export default function Page() { const {colors} = useTheme(); @@ -13,7 +14,9 @@ export default function Page() { const [categoryName, setCategoryName] = useState(""); const [categoryColor, setCategoryColor] = useState('#' + Math.floor(Math.random()*16777215).toString(16)); - const [selectedType, setSelectedType] = useState("expense"); + + const [selectedType, setSelectedType] = useState(CategoryType.EXPENSE); + const [amount, setAmount] = useState(0); return ( @@ -35,7 +38,7 @@ export default function Page() { { + handleButtonPress={(type: CategoryType) => { setSelectedType(type); }} /> diff --git a/components/budget/typeSelectorSwitch.tsx b/components/budget/typeSelectorSwitch.tsx index dc6231b..a4edf58 100644 --- a/components/budget/typeSelectorSwitch.tsx +++ b/components/budget/typeSelectorSwitch.tsx @@ -1,10 +1,11 @@ import { StyleSheet, Text, TouchableOpacity, View } from "react-native"; import { useTheme } from "../../app/contexts/ThemeContext"; +import { CategoryType } from "../../services/database"; export type TypeSelectorSwitchProperties = { - handleButtonPress: (type: string) => void, - currentSelected: string, + handleButtonPress: (type: CategoryType) => void, + currentSelected: CategoryType, } const TypeSelectorSwitch = (properties: TypeSelectorSwitchProperties) => { @@ -14,17 +15,17 @@ const TypeSelectorSwitch = (properties: TypeSelectorSwitchProperties) => { { - properties.handleButtonPress("expense"); + properties.handleButtonPress(CategoryType.EXPENSE); }} - style={[styles.touchableOpacityStyle, properties.currentSelected == "expense" ? {backgroundColor: colors.accentColor} : {backgroundColor: colors.elementDefaultColor}] + style={[styles.touchableOpacityStyle, properties.currentSelected == CategoryType.EXPENSE ? {backgroundColor: colors.accentColor} : {backgroundColor: colors.elementDefaultColor}] }> Expenses { - properties.handleButtonPress("saving"); + onPress={() => { + properties.handleButtonPress(CategoryType.SAVING); }} - style={[styles.touchableOpacityStyle, properties.currentSelected == "saving" ? {backgroundColor: colors.accentColor} : {backgroundColor: colors.elementDefaultColor}] + style={[styles.touchableOpacityStyle, properties.currentSelected == CategoryType.SAVING ? {backgroundColor: colors.accentColor} : {backgroundColor: colors.elementDefaultColor}] }> Savings diff --git a/components/stats/BudgetOverview.tsx b/components/stats/BudgetOverview.tsx index 05e89c4..8cc5c6c 100644 --- a/components/stats/BudgetOverview.tsx +++ b/components/stats/BudgetOverview.tsx @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react'; import { View, Text, StyleSheet } from 'react-native'; import { useTheme } from '../../app/contexts/ThemeContext'; import useFetch from '../../hooks/useFetch'; +import { CategoryType } from '../../services/database'; const BudgetOverview = () => { const { colors } = useTheme(); @@ -9,12 +10,12 @@ const BudgetOverview = () => { const [budget, setBudget] = useState(0); const spentQuery = { - sql: "SELECT SUM(e.amount) as total FROM expense e LEFT JOIN category c ON e.category_guid = c.guid WHERE c.type = 'budget'", + sql: `SELECT SUM(e.amount) as total FROM expense e LEFT JOIN category c ON e.category_guid = c.guid WHERE c.type = '${CategoryType.EXPENSE.toString()}'`, args: [] }; const budgetQuery = { - sql: "SELECT SUM(allocated_amount) as total FROM category WHERE type = 'budget'", + sql: `SELECT SUM(allocated_amount) as total FROM category WHERE type = '${CategoryType.EXPENSE.toString()}'`, args: [] }; diff --git a/components/stats/BudgetRemaining.tsx b/components/stats/BudgetRemaining.tsx index 37aa555..d5209fd 100644 --- a/components/stats/BudgetRemaining.tsx +++ b/components/stats/BudgetRemaining.tsx @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react'; import { Text, StyleSheet } from 'react-native'; import { useTheme } from '../../app/contexts/ThemeContext'; import useFetch from '../../hooks/useFetch'; +import { CategoryType } from '../../services/database'; const styles = StyleSheet.create({ text: { @@ -23,7 +24,7 @@ const BudgetRemaining = () => { }; const budgetQuery = { - sql: "SELECT SUM(allocated_amount) as total FROM category WHERE type = 'budget'", + sql: `SELECT SUM(allocated_amount) as total FROM category WHERE type = '${CategoryType.EXPENSE.toString()}'`, args: [] }; diff --git a/components/stats/SavingsOverview.tsx b/components/stats/SavingsOverview.tsx index a79234a..5162e75 100644 --- a/components/stats/SavingsOverview.tsx +++ b/components/stats/SavingsOverview.tsx @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react'; import { View, Text, StyleSheet } from 'react-native'; import { useTheme } from '../../app/contexts/ThemeContext'; import useFetch from '../../hooks/useFetch'; +import { CategoryType } from '../../services/database'; const SavingsOverview = () => { const { colors } = useTheme(); @@ -9,12 +10,12 @@ const SavingsOverview = () => { const [goal, setGoal] = useState(0); const savedQuery = { - sql: "SELECT SUM(e.amount) as total FROM expense e LEFT JOIN category c ON e.category_guid = c.guid WHERE c.type = 'savings'", + sql: `SELECT SUM(e.amount) as total FROM expense e LEFT JOIN category c ON e.category_guid = c.guid WHERE c.type = '${CategoryType.SAVING.toString()}'`, args: [] }; const goalQuery = { - sql: "SELECT SUM(allocated_amount) as total FROM category WHERE type = 'budget'", + sql: `SELECT SUM(allocated_amount) as total FROM category WHERE type = '${CategoryType.SAVING.toString()}'`, args: [] }; diff --git a/services/database.ts b/services/database.ts index 370108d..029131d 100644 --- a/services/database.ts +++ b/services/database.ts @@ -17,7 +17,7 @@ export const initDatabase = async () => { try { await db.transactionAsync(async (tx: SQLite.SQLTransactionAsync) => { await tx.executeSqlAsync( - "CREATE TABLE IF NOT EXISTS category (guid VARCHAR(36) PRIMARY KEY, name TEXT, color TEXT, type TEXT, allocated_amount DOUBLE);" + "CREATE TABLE IF NOT EXISTS category (guid VARCHAR(36) PRIMARY KEY, name TEXT, color TEXT, type string, allocated_amount DOUBLE);" ); await tx.executeSqlAsync( "CREATE TABLE IF NOT EXISTS expense (guid VARCHAR(36) PRIMARY KEY, name TEXT, category_guid VARCHAR(36), datetime DATETIME, amount DOUBLE, FOREIGN KEY (category_guid) REFERENCES category(guid));"