Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
203 lines (182 loc) · 4.66 KB
/
Copy pathApp.js
File metadata and controls
203 lines (182 loc) · 4.66 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { useState, useEffect } from "react";
import { StatusBar } from 'expo-status-bar';
import Checkbox from 'expo-checkbox';
import { StyleSheet, Text, View,Platform,FlatList,SafeAreaView,Modal,Button,TextInput } from 'react-native';
import Constants from "expo-constants";
import * as SQLite from "expo-sqlite";
import { FloatingAction } from "react-native-floating-action";
const openDatabase = () => {
if (Platform.OS === "web") {
return {
transaction: () => {
return {
executeSql: () => {},
};
},
};
}
const db = SQLite.openDatabase("Tasks.db");
db.transaction((tx) => {
tx.executeSql(
"create table if not exists Tasks (TaskId integer primary key not null, Title text, DueDate text, Completed int);"
);
});
return db;
}
const db = openDatabase();
const actions = [
{
text: "New Task",
icon: require("./assets/add.png"),
name: "newTask",
position: 1
}
]
const App = () => {
const [activeTasks, setActiveTasks] = useState(null);
const [modalVisible, setModalVisible] = useState(false);
const [title, setTitle] = useState("");
const [dueDate, setDueDate] = useState("");
useEffect(() => {
GetActiveTasks();
}, []);
const Item = ({ taskId,title,dueDate,completed}) => (
<View style={styles.item}>
<Checkbox style={styles.checkbox} value={completed==0 ? false : true} onValueChange={()=>handleComplete(taskId)} />
<Text style={styles.title}>{title}</Text>
<Text style={styles.dueDate}>{dueDate}</Text>
</View>
);
const GetActiveTasks = () => {
db.transaction((tx) => {
tx.executeSql("select * from Tasks where Completed=0", [], (_, { rows: { _array } }) =>
setActiveTasks(_array)
);
});
}
const AddTask = (title,dueDate) => {
db.transaction((tx) => {
tx.executeSql(
"insert into Tasks (Title, DueDate, Completed) values (?, ?, 0)", [title,dueDate]
);
});
}
const CompleteTask = (taskId) => {
db.transaction((tx) => {
tx.executeSql(
"update Tasks set Completed=1 where TaskId=?", [taskId]
);
});
}
const handleComplete = (taskId) => {
CompleteTask(taskId);
GetActiveTasks();
}
const handleAction = (name) => {
if (name=="newTask") {
setModalVisible(true);
}
}
const handleSubmitTask = () => {
AddTask(title,dueDate);
setModalVisible(false);
GetActiveTasks();
}
const renderItem = ({ item }) => <Item title={item.Title} dueDate={item.DueDate} taskId={item.TaskId} completed={item.Completed} />;
return (
<SafeAreaView style={styles.container}>
<Text>TASKMASTER</Text>
<FlatList data={activeTasks} renderItem={renderItem} keyExtractor={item => item.TaskId} />
<FloatingAction
actions={actions}
overrideWithAction="true"
onPressItem={name => handleAction(name)}
/>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
setModalVisible(!modalVisible);
}}>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<TextInput
style={styles.input}
onChangeText={(text)=>setTitle(text)}
value={title}
placeholder="Title"
keyboardType="default"
/>
<TextInput
style={styles.input}
onChangeText={(text)=>setDueDate(text)}
value={dueDate}
placeholder="Due Date"
keyboardType="default"
/>
<Button title="Add New Task" onPress={() => handleSubmitTask()} />
</View>
</View>
</Modal>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 50,
marginLeft: 10
},
item: {
backgroundColor: '#4d423d',
padding: 10,
marginVertical: 4,
marginHorizontal: 16,
},
title: {
fontSize: 16,
color:'#fff'
},
dueDate: {
fontSize: 12,
color: '#fff',
marginTop:5
},
centeredView: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
marginTop: 22,
},
modalView: {
margin: 20,
backgroundColor: 'white',
borderRadius: 20,
padding: 35,
alignItems: 'center',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5,
},
modalText: {
marginBottom: 15,
textAlign: 'center',
},
input: {
width:200,
height: 50,
margin: 12,
borderWidth: 1,
padding: 10,
},
checkbox: {
margin: 8,
},
});
export default App;