-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
117 lines (103 loc) · 2.41 KB
/
App.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import AppLoading from 'expo-app-loading';
import {
useFonts,
Inter_100Thin,
Inter_200ExtraLight,
Inter_300Light,
Inter_400Regular,
Inter_500Medium,
Inter_600SemiBold,
Inter_700Bold,
Inter_800ExtraBold,
Inter_900Black,
} from '@expo-google-fonts/inter';
import { SafeAreaView, StyleSheet, View } from 'react-native';
import React, { useEffect } from 'react';
import Header from './src/Header.js';
import TodoList from './src/TodoList.js';
import api from './src/utils/api.js';
import 'react-native-get-random-values';
import uuid from 'node-uuid';
function App() {
let [fontsLoaded] = useFonts({
Inter_100Thin,
Inter_200ExtraLight,
Inter_300Light,
Inter_400Regular,
Inter_500Medium,
Inter_600SemiBold,
Inter_700Bold,
Inter_800ExtraBold,
Inter_900Black,
});
const [restTodos, setRestTodos] = React.useState([]);
const addRestTodo = async (text) => {
await api.addRestTodo({
id: uuid.v1(),
completed: false,
text: text,
key: 'rest',
});
getRestTodos();
};
const deleteRestTodo = async (id) => {
await api.deleteRestTodo(id);
getRestTodos();
};
const completeRestTodo = async (id, text, completed) => {
await api.updateRestTodo({
id,
text,
completed: !completed,
});
getRestTodos();
};
useEffect(() => {
getRestTodos();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
}, [restTodos]);
const getRestTodos = async () => {
// Reload the todo list from the database to see the latest changes
api.getRestTodos().then((restTodos) => setRestTodos(restTodos));
};
const clearRestCompleted = async () => {
let docTodos = api.getRestTodos();
docTodos.forEach((todo) => {
completeRestTodo(todo.id, todo.text, true);
});
};
const actions = {
addRestTodo: addRestTodo,
completeRestTodo: completeRestTodo,
clearRestCompleted: clearRestCompleted,
getRestTodos: getRestTodos,
deleteRestTodo: deleteRestTodo,
};
if (!fontsLoaded) {
return <AppLoading />;
} else {
return (
<SafeAreaView style={styles.container}>
<View style={styles.todos}>
<Header title="To-Do List" addTodo={actions.addRestTodo} type="rest" />
<TodoList type="rest" todos={restTodos} actions={actions} />
</View>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
statusBar: {
backgroundColor: '#e8f4f8',
},
todos: {
flex: 1,
backgroundColor: '#e8f4f8',
},
});
export default App;