-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.tsx
73 lines (64 loc) · 2.12 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
import { NavigationContainer } from "@react-navigation/native";
import React, { useEffect } from "react";
import { StyleSheet, View } from "react-native";
import { getData, LogMessage, storeData } from "./src/lib/localStorage";
import { calculateMessagesSum } from "./src/lib/utilities";
import ExpensesScreen from "./src/screens/ExpensesScreen";
import HomeScreen from "./src/screens/HomeScreen";
import SettingsScreen from "./src/screens/SettingsScreen";
export default function App() {
const Tab = createBottomTabNavigator();
const [totalSpent, onChangeTotalSpent] = React.useState(0);
const [messages, onChangeMessages] = React.useState<LogMessage[]>([]);
useEffect(() => {
const fetchData = async () => {
const data = await getData();
if (data) {
onChangeMessages(data);
onChangeTotalSpent(calculateMessagesSum(data));
}
};
fetchData().catch(console.error);
}, [messages]);
const onSubmitMessage = async function (text: string) {
if (text == "") return;
const currentDate = new Date();
const newMessages = [...messages, { message: text, currentDate }];
onChangeMessages(newMessages);
storeData(newMessages);
};
const onDeleteMessage = async function (index: number) {
messages.splice(index, 1);
onChangeMessages(messages);
storeData(messages);
}
return (
<View style={styles.root}>
<View style={styles.tabs}>
<NavigationContainer>
<Tab.Navigator>
<Tab.Screen
name="Expenses"
children={() => <ExpensesScreen totalSpent={totalSpent} />}
/>
<Tab.Screen
name="Home"
children={() => <HomeScreen messages={messages} onSubmitMessage={onSubmitMessage}
onDeleteMessage={onDeleteMessage}/>}
/>
<Tab.Screen name="Settings" component={SettingsScreen} />
</Tab.Navigator>
</NavigationContainer>
</View>
</View>
);
}
const styles = StyleSheet.create({
root: {
flex: 1,
},
tabs: {
flex: 1,
},
});