-
Notifications
You must be signed in to change notification settings - Fork 0
/
TodoList.js
183 lines (170 loc) · 5.06 KB
/
TodoList.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
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
179
180
181
182
183
// TodoApp.js
import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, Button, ScrollView, TouchableOpacity, StyleSheet, Platform } from 'react-native';
import DateTimePicker from '@react-native-community/datetimepicker';
const TodoApp = () => {
const [tasks, setTasks] = useState([]);
const [newTask, setNewTask] = useState("");
const [newTaskDate, setNewTaskDate] = useState(null); // Initialize with null
const [showDatePicker, setShowDatePicker] = useState(false);
const [editIndex, setEditIndex] = useState(null);
useEffect(() => {
// Use AsyncStorage or another storage solution for persistence in React Native
// Example: AsyncStorage.setItem('tasks', JSON.stringify(tasks));
}, [tasks]);
const addTask = () => {
if (editIndex !== null) {
// If editing, update the existing task
const updatedTasks = [...tasks];
updatedTasks[editIndex] = { text: newTask, date: newTaskDate ? newTaskDate.toISOString().split('T')[0] : null };
setTasks(updatedTasks);
setEditIndex(null);
} else {
// If not editing, add a new task
setTasks([...tasks, { text: newTask, date: newTaskDate ? newTaskDate.toISOString().split('T')[0] : null }]);
}
// Clear the input fields
setNewTask("");
setNewTaskDate(null); // Set to null after adding a task
};
const editTask = (index) => {
// Set the input fields with the task to be edited
setNewTask(tasks[index].text);
setNewTaskDate(tasks[index].date ? new Date(tasks[index].date) : null);
setEditIndex(index);
};
const deleteTask = (index) => {
// Remove task at the specified index
const updatedTasks = [...tasks];
updatedTasks.splice(index, 1);
setTasks(updatedTasks);
// Reset editIndex if the task being edited is deleted
if (index === editIndex) {
setEditIndex(null);
setNewTask("");
setNewTaskDate(null); // Set to null after deleting a task
}
};
const showDatepicker = () => {
setShowDatePicker(true);
};
const onDateChange = (event, selectedDate) => {
setShowDatePicker(Platform.OS === 'ios'); // Close the date picker on iOS
if (selectedDate) {
setNewTaskDate(selectedDate);
}
};
return (
<View style={styles.container}>
<Text style={styles.header}>Todo List</Text>
<View style={styles.form}>
<TextInput
style={styles.input}
placeholder="What do you have planned?"
value={newTask}
onChangeText={(text) => setNewTask(text)}
/>
<TouchableOpacity style={styles.datePickerButton} onPress={showDatepicker}>
<Text>Date: {newTaskDate ? newTaskDate.toISOString().split('T')[0] : 'Select date'}</Text>
</TouchableOpacity>
{showDatePicker && (
<DateTimePicker
value={newTaskDate || new Date()} // Set to current date if null
mode="date"
display="default"
onChange={onDateChange}
/>
)}
<Button title={editIndex !== null ? "Save task" : "Add task"} onPress={addTask} />
</View>
<ScrollView style={styles.taskList}>
<Text style={styles.taskListHeader}>Tasks</Text>
{tasks.map((task, index) => (
<View key={index} style={styles.task}>
<View style={styles.content}>
<Text style={styles.text}>{task.text}</Text>
{task.date && <Text style={styles.date}>Date: {task.date}</Text>}
</View>
<View style={styles.actions}>
<TouchableOpacity style={styles.editButton} onPress={() => editTask(index)}>
<Text>{editIndex === index ? "Cancel" : "Edit"}</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.deleteButton} onPress={() => deleteTask(index)}>
<Text>Delete</Text>
</TouchableOpacity>
</View>
</View>
))}
</ScrollView>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
backgroundColor: '#f0f0f0',
},
header: {
fontSize: 24,
marginBottom: 16,
},
form: {
marginBottom: 16,
},
input: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
marginBottom: 16,
paddingHorizontal: 10,
},
datePickerButton: {
height: 40,
justifyContent: 'center',
marginBottom: 16,
paddingHorizontal: 10,
borderColor: 'gray',
borderWidth: 1,
},
taskList: {
flex: 1,
},
taskListHeader: {
fontSize: 20,
marginBottom: 8,
},
task: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
padding: 8,
backgroundColor: '#ffffff',
marginBottom: 8,
},
content: {
flex: 1,
},
text: {
fontSize: 18,
},
date: {
fontSize: 16,
color: 'gray',
},
actions: {
flexDirection: 'row',
},
editButton: {
padding: 8,
backgroundColor: '#7c8fc2',
borderRadius: 4,
marginRight: 8,
},
deleteButton: {
padding: 8,
backgroundColor: '#ed4572',
borderRadius: 4,
},
});
export default TodoApp;