|
| 1 | +import React, {useState, useLayoutEffect, useEffect} from 'react'; |
| 2 | +import { |
| 3 | + SafeAreaView, |
| 4 | + StyleSheet, |
| 5 | + Text, |
| 6 | + View, |
| 7 | + TextInput, |
| 8 | + Button, |
| 9 | + FlatList, |
| 10 | + Image, |
| 11 | +} from 'react-native'; |
| 12 | + |
| 13 | +import AsyncStorage from '@react-native-async-storage/async-storage'; |
| 14 | + |
| 15 | +function App(): JSX.Element { |
| 16 | + const [todos, setTodos] = useState([ |
| 17 | + 'Try adding some tasks for your awesome day! 💫', |
| 18 | + ]); |
| 19 | + |
| 20 | + useLayoutEffect(() => { |
| 21 | + AsyncStorage.getItem('todos').then(todos => { |
| 22 | + if (todos) { |
| 23 | + setTodos(JSON.parse(todos)); |
| 24 | + console.log(todos); |
| 25 | + } |
| 26 | + }); |
| 27 | + }, []); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + AsyncStorage.setItem('todos', JSON.stringify(todos)); |
| 31 | + }, [todos]); |
| 32 | + |
| 33 | + const [task, setTask] = useState(''); |
| 34 | + |
| 35 | + const addTask = () => { |
| 36 | + task && setTodos(prevTasks => [task, ...prevTasks]); |
| 37 | + }; |
| 38 | + |
| 39 | + const removeTask = (index: number) => { |
| 40 | + const newTodos = todos.filter((todo, i) => i !== index); |
| 41 | + setTodos(newTodos); |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <SafeAreaView style={styles.container}> |
| 46 | + <View style={styles.header}> |
| 47 | + <Text style={styles.text}>Todos 📝</Text> |
| 48 | + </View> |
| 49 | + <View style={styles.todoEntry}> |
| 50 | + <TextInput |
| 51 | + style={styles.input} |
| 52 | + placeholder="Enter Task!" |
| 53 | + onChangeText={text => setTask(text)} |
| 54 | + /> |
| 55 | + <Button color="black" title="Add Task" onPress={addTask} /> |
| 56 | + </View> |
| 57 | + <View style={styles.taskHeader}> |
| 58 | + <Text style={styles.taskText}>Tasks ✏</Text> |
| 59 | + </View> |
| 60 | + |
| 61 | + {!(todos.length === 0) ? ( |
| 62 | + <FlatList |
| 63 | + style={styles.taskContainer} |
| 64 | + data={todos} |
| 65 | + renderItem={todoData => { |
| 66 | + return ( |
| 67 | + <View key={todoData.index} style={styles.todoContainer}> |
| 68 | + <Text>{todoData.item}</Text> |
| 69 | + <Button |
| 70 | + color="gray" |
| 71 | + title="Delete" |
| 72 | + onPress={() => removeTask(todoData.index)} |
| 73 | + /> |
| 74 | + </View> |
| 75 | + ); |
| 76 | + }} |
| 77 | + /> |
| 78 | + ) : ( |
| 79 | + <View style={styles.emptyDiv}> |
| 80 | + <Image source={require('./empty.jpg')} style={{}} /> |
| 81 | + <Text style={styles.emptyText}>Add Some Tasks UwU</Text> |
| 82 | + </View> |
| 83 | + )} |
| 84 | + {/* </View> */} |
| 85 | + </SafeAreaView> |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +const styles = StyleSheet.create({ |
| 90 | + container: { |
| 91 | + display: 'flex', |
| 92 | + justifyContent: 'flex-start', |
| 93 | + alignItems: 'center', |
| 94 | + flex: 1, |
| 95 | + backgroundColor: 'black', |
| 96 | + }, |
| 97 | + header: { |
| 98 | + display: 'flex', |
| 99 | + justifyContent: 'center', |
| 100 | + alignItems: 'center', |
| 101 | + width: '100%', |
| 102 | + height: 50, |
| 103 | + borderBottomColor: 'gray', |
| 104 | + borderBottomWidth: 1, |
| 105 | + }, |
| 106 | + text: { |
| 107 | + color: 'white', |
| 108 | + fontSize: 15, |
| 109 | + fontWeight: 'normal', |
| 110 | + }, |
| 111 | + todoEntry: { |
| 112 | + width: '100%', |
| 113 | + paddingVertical: 15, |
| 114 | + paddingHorizontal: 5, |
| 115 | + flexDirection: 'row', |
| 116 | + justifyContent: 'space-between', |
| 117 | + }, |
| 118 | + input: { |
| 119 | + backgroundColor: 'gray', |
| 120 | + flex: 3, |
| 121 | + height: 40, |
| 122 | + padding: 10, |
| 123 | + borderRadius: 5, |
| 124 | + }, |
| 125 | + taskBtn: { |
| 126 | + flex: 1, |
| 127 | + margin: 10, |
| 128 | + backgroundColor: 'white', |
| 129 | + color: 'black', |
| 130 | + }, |
| 131 | + taskHeader: { |
| 132 | + display: 'flex', |
| 133 | + alignItems: 'flex-start', |
| 134 | + width: '100%', |
| 135 | + padding: 20, |
| 136 | + }, |
| 137 | + taskText: { |
| 138 | + color: 'white', |
| 139 | + fontSize: 30, |
| 140 | + fontWeight: 'bold', |
| 141 | + }, |
| 142 | + taskContainer: { |
| 143 | + display: 'flex', |
| 144 | + width: '100%', |
| 145 | + paddingHorizontal: 5, |
| 146 | + marginBottom: 20, |
| 147 | + }, |
| 148 | + todoContainer: { |
| 149 | + display: 'flex', |
| 150 | + borderWidth: 1, |
| 151 | + borderColor: 'white', |
| 152 | + borderRadius: 5, |
| 153 | + padding: 15, |
| 154 | + gap: 10, |
| 155 | + width: '100%', |
| 156 | + marginBottom: 10, |
| 157 | + }, |
| 158 | + emptyDiv: { |
| 159 | + width: '100%', |
| 160 | + display: 'flex', |
| 161 | + justifyContent: 'center', |
| 162 | + alignItems: 'center', |
| 163 | + }, |
| 164 | + emptyText: { |
| 165 | + color: 'white', |
| 166 | + fontSize: 20, |
| 167 | + textAlign: 'center', |
| 168 | + marginTop: 10, |
| 169 | + }, |
| 170 | + taskParent: { |
| 171 | + width: '100%', |
| 172 | + display: 'flex', |
| 173 | + justifyContent: 'flex-start', |
| 174 | + alignItems: 'center', |
| 175 | + }, |
| 176 | +}); |
| 177 | + |
| 178 | +export default App; |
0 commit comments