-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
178 lines (167 loc) · 3.8 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import React, {useState, useLayoutEffect, useEffect} from 'react';
import {
SafeAreaView,
StyleSheet,
Text,
View,
TextInput,
Button,
FlatList,
Image,
} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
function App(): JSX.Element {
const [todos, setTodos] = useState([
'Try adding some tasks for your awesome day! 💫',
]);
useLayoutEffect(() => {
AsyncStorage.getItem('todos').then(todos => {
if (todos) {
setTodos(JSON.parse(todos));
console.log(todos);
}
});
}, []);
useEffect(() => {
AsyncStorage.setItem('todos', JSON.stringify(todos));
}, [todos]);
const [task, setTask] = useState('');
const addTask = () => {
task && setTodos(prevTasks => [task, ...prevTasks]);
};
const removeTask = (index: number) => {
const newTodos = todos.filter((todo, i) => i !== index);
setTodos(newTodos);
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.header}>
<Text style={styles.text}>Todos 📝</Text>
</View>
<View style={styles.todoEntry}>
<TextInput
style={styles.input}
placeholder="Enter Task!"
onChangeText={text => setTask(text)}
/>
<Button color="black" title="Add Task" onPress={addTask} />
</View>
<View style={styles.taskHeader}>
<Text style={styles.taskText}>Tasks ✏</Text>
</View>
{!(todos.length === 0) ? (
<FlatList
style={styles.taskContainer}
data={todos}
renderItem={todoData => {
return (
<View key={todoData.index} style={styles.todoContainer}>
<Text>{todoData.item}</Text>
<Button
color="gray"
title="Delete"
onPress={() => removeTask(todoData.index)}
/>
</View>
);
}}
/>
) : (
<View style={styles.emptyDiv}>
<Image source={require('./empty.jpg')} style={{}} />
<Text style={styles.emptyText}>Add Some Tasks UwU</Text>
</View>
)}
{/* </View> */}
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
display: 'flex',
justifyContent: 'flex-start',
alignItems: 'center',
flex: 1,
backgroundColor: 'black',
},
header: {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
width: '100%',
height: 50,
borderBottomColor: 'gray',
borderBottomWidth: 1,
},
text: {
color: 'white',
fontSize: 15,
fontWeight: 'normal',
},
todoEntry: {
width: '100%',
paddingVertical: 15,
paddingHorizontal: 5,
flexDirection: 'row',
justifyContent: 'space-between',
},
input: {
backgroundColor: 'gray',
flex: 3,
height: 40,
padding: 10,
borderRadius: 5,
},
taskBtn: {
flex: 1,
margin: 10,
backgroundColor: 'white',
color: 'black',
},
taskHeader: {
display: 'flex',
alignItems: 'flex-start',
width: '100%',
padding: 20,
},
taskText: {
color: 'white',
fontSize: 30,
fontWeight: 'bold',
},
taskContainer: {
display: 'flex',
width: '100%',
paddingHorizontal: 5,
marginBottom: 20,
},
todoContainer: {
display: 'flex',
borderWidth: 1,
borderColor: 'white',
borderRadius: 5,
padding: 15,
gap: 10,
width: '100%',
marginBottom: 10,
},
emptyDiv: {
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
},
emptyText: {
color: 'white',
fontSize: 20,
textAlign: 'center',
marginTop: 10,
},
taskParent: {
width: '100%',
display: 'flex',
justifyContent: 'flex-start',
alignItems: 'center',
},
});
export default App;