11 import React, {FC, ReactElement, useState} from 'react';
22 import {
33 Alert,
44 View,
55 SafeAreaView,
66 Image,
77 ScrollView,
88 StatusBar,
99 StyleSheet,
1010 TouchableOpacity,
1111 } from 'react-native';
1212 import Parse from 'parse/react-native';
1313 import {
1414 List,
1515 Title,
1616 IconButton,
1717 Text as PaperText,
1818 Button as PaperButton,
1919 TextInput as PaperTextInput,
2020 } from 'react-native-paper';
2121
2222 export const TodoList: FC<{}> = ({}): ReactElement => {
2323
2424 const [readResults, setReadResults] = useState<[Parse.Object]>();
2525 const [newTodoTitle, setNewTodoTitle] = useState('');
2626
2727
2828 const createTodo = async function (): Promise<boolean> {
2929
3030 const newTodoTitleValue: string = newTodoTitle;
3131
3232 let Todo: Parse.Object = new Parse.Object('Todo');
3333 Todo.set('title', newTodoTitleValue);
3434 Todo.set('done', false);
3535
3636 try {
3737 await Todo.save();
3838
3939 Alert.alert('Success!', 'Todo created!');
4040
4141 readTodos();
4242 return true;
4343 } catch (error) {
4444
4545 Alert.alert('Error!', error.message);
4646 return false;
4747 }
4848 };
4949
5050 const readTodos = async function (): Promise<boolean> {
5151
5252 const parseQuery: Parse.Query = new Parse.Query('Todo');
5353 try {
5454 let todos: Parse.Object[] = await parseQuery.find();
5555
5656
5757 setReadResults(todos);
5858 return true;
5959 } catch (error) {
6060
6161 Alert.alert('Error!', error.message);
6262 return false;
6363 }
6464 };
6565
6666 const updateTodo = async function (
6767 todoId: string,
6868 done: boolean,
6969 ): Promise<boolean> {
7070
7171 let Todo: Parse.Object = new Parse.Object('Todo');
7272 Todo.set('objectId', todoId);
7373
7474 Todo.set('done', done);
7575 try {
7676 await Todo.save();
7777
7878 Alert.alert('Success!', 'Todo updated!');
7979
8080 readTodos();
8181 return true;
8282 } catch (error) {
8383
8484 Alert.alert('Error!', error.message);
8585 return false;
8686 }
8787 };
8888
8989 const deleteTodo = async function (todoId: string): Promise<boolean> {
9090
9191 let Todo: Parse.Object = new Parse.Object('Todo');
9292 Todo.set('objectId', todoId);
9393
9494 try {
9595 await Todo.destroy();
9696 Alert.alert('Success!', 'Todo deleted!');
9797
9898 readTodos();
9999 return true;
100100 } catch (error) {
101101
102102 Alert.alert('Error!', error.message);
103103 return false;
104104 }
105105 };
106106
107107 return (
108108 <>
109109 <StatusBar backgroundColor="#208AEC" />
110110 <SafeAreaView style={Styles.container}>
111111 <View style={Styles.header}>
112112 <Image
113113 style={Styles.header_logo}
114114 source={ {
115115 uri:
116116 'https://blog.back4app.com/wp-content/uploads/2019/05/back4app-white-logo-500px.png',
117117 } }
118118 />
119119 <PaperText style={Styles.header_text_bold}>
120120 {'React Native on Back4App'}
121121 </PaperText>
122122 <PaperText style={Styles.header_text}>{'Product Creation'}</PaperText>
123123 </View>
124124 <View style={Styles.wrapper}>
125125 <View style={Styles.flex_between}>
126126 <Title>Todo List</Title>
127127 {}
128128 <IconButton
129129 icon="refresh"
130130 color={'#208AEC'}
131131 size={24}
132132 onPress={() => readTodos()}
133133 />
134134 </View>
135135 <View style={Styles.create_todo_container}>
136136 {}
137137 <PaperTextInput
138138 value={newTodoTitle}
139139 onChangeText={text => setNewTodoTitle(text)}
140140 label="New Todo"
141141 mode="outlined"
142142 style={Styles.create_todo_input}
143143 />
144144 {}
145145 <PaperButton
146146 onPress={() => createTodo()}
147147 mode="contained"
148148 icon="plus"
149149 color={'#208AEC'}
150150 style={Styles.create_todo_button}>
151151 {'Add'}
152152 </PaperButton>
153153 </View>
154154 <ScrollView>
155155 {}
156156 {readResults !== null &&
157157 readResults !== undefined &&
158158 readResults.map((todo: Parse.Object) => (
159159 <List.Item
160160 key={todo.id}
161161 title={todo.get('title')}
162162 titleStyle={
163163 todo.get('done') === true
164164 ? Styles.todo_text_done
165165 : Styles.todo_text
166166 }
167167 style={Styles.todo_item}
168168 right={props => (
169169 <>
170170 {}
171171 {todo.get('done') !== true && (
172172 <TouchableOpacity
173173 onPress={() => updateTodo(todo.id, true)}>
174174 <List.Icon
175175 {...props}
176176 icon="check"
177177 color={'#4CAF50'}
178178 />
179179 </TouchableOpacity>
180180 )}
181181 {}
182182 <TouchableOpacity onPress={() => deleteTodo(todo.id)}>
183183 <List.Icon {...props} icon="close" color={'#ef5350'} />
184184 </TouchableOpacity>
185185 </>
186186 )}
187187 />
188188 ))}
189189 </ScrollView>
190190 </View>
191191 </SafeAreaView>
192192 </>
193193 );
194194 };
195195
196196
197197 const Styles = StyleSheet.create({
198198 container: {
199199 flex: 1,
200200 backgroundColor: '#FFF',
201201 },
202202 wrapper: {
203203 width: '90%',
204204 alignSelf: 'center',
205205 },
206206 header: {
207207 alignItems: 'center',
208208 paddingTop: 10,
209209 paddingBottom: 20,
210210 backgroundColor: '#208AEC',
211211 },
212212 header_logo: {
213213 width: 170,
214214 height: 40,
215215 marginBottom: 10,
216216 resizeMode: 'contain',
217217 },
218218 header_text_bold: {
219219 color: '#fff',
220220 fontSize: 14,
221221 fontWeight: 'bold',
222222 },
223223 header_text: {
224224 marginTop: 3,
225225 color: '#fff',
226226 fontSize: 14,
227227 },
228228 flex_between: {
229229 flexDirection: 'row',
230230 alignItems: 'center',
231231 justifyContent: 'space-between',
232232 },
233233 create_todo_container: {
234234 flexDirection: 'row',
235235 },
236236 create_todo_input: {
237237 flex: 1,
238238 height: 38,
239239 marginBottom: 16,
240240 backgroundColor: '#FFF',
241241 fontSize: 14,
242242 },
243243 create_todo_button: {
244244 marginTop: 6,
245245 marginLeft: 15,
246246 height: 40,
247247 },
248248 todo_item: {
249249 borderBottomWidth: 1,
250250 borderBottomColor: 'rgba(0, 0, 0, 0.12)',
251251 },
252252 todo_text: {
253253 fontSize: 15,
254254 },
255255 todo_text_done: {
256256 color: 'rgba(0, 0, 0, 0.3)',
257257 fontSize: 15,
258258 textDecorationLine: 'line-through',