-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
250 lines (237 loc) · 7.19 KB
/
App.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { useEffect, useState } from 'react';
import { StyleSheet, SafeAreaView, StatusBar } from 'react-native';
import {
ThemeProvider,
TabView,
Tab,
View,
Text,
colors,
SpeedDial,
} from 'react-native-elements';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { LOCALHOST_IP } from './config';
import TaskListView from './Components/TaskListView/TaskListView';
import AddPage from './Components/AddPage/AddPage';
// set theme colors
const theme = {
colors: {
primary: '#e64a19',
},
};
export default function App() {
// local state for the tasklist
const [taskList, setTaskList] = useState([]);
// local state for tab index
const [tabIndex, setTabIndex] = useState(2);
// keeps track of items to edit
const [editTask, setEditTask] = useState({});
// keeps track of whether the speed dial is open
const [openSpeedDial, setOpenSpeedDial] = useState(false);
// keeps track of sort position
// default is descending sort
const [sortDesc, setSortDesc] = useState(true);
const [latestSortType, setLatestSortType] = useState('name');
// on app load, retrieve the taskList
useEffect(() => {
refreshWithoutSort();
}, []);
// fetches the taskList without sort
const refreshWithoutSort = () => {
fetch(`http://${LOCALHOST_IP}:5000/api/task`)
.then((response) => response.json())
.then((data) => {
setTaskList(data);
})
.catch((err) => console.log('Error in fetch: ', err));
};
// fetches the taskList with sort
const refreshWithSort = (type, sortDirection) => {
fetch(
`http://${LOCALHOST_IP}:5000/api/task?type=${type}&direction=${sortDirection}`
)
.then((response) => response.json())
.then((data) => {
console.log('you ran a sort and your data is', data);
setTaskList(data);
// update the latestSortType
setLatestSortType(type);
})
.catch((err) => console.log('Error in fetch for sort: ', err));
};
// handles the sort
const handleSort = (type) => {
// variable to determine sort direction
let sortDirection;
if (latestSortType === type) {
// change the sort direction and toggle
sortDirection = sortDesc ? 'asc' : 'desc';
setSortDesc(!sortDesc);
} else {
// use the normal sortDirection
sortDirection = 'desc';
// and return sortDesc to true
setSortDesc(true);
}
// now use queries to refresh the taskList
refreshWithSort(type, sortDirection);
};
return (
<ThemeProvider theme={theme}>
<SafeAreaProvider>
<StatusBar
barStyle="light-content"
style={{ backgroundColor: '#e64a19' }}
/>
<SafeAreaView style={styles.container}>
{tabIndex === 0 && (
<TaskListView
setEditTask={setEditTask}
setTabIndex={setTabIndex}
taskList={taskList}
setTaskList={setTaskList}
view={'overdue'}
/>
)}
{tabIndex === 1 && (
<TaskListView
setEditTask={setEditTask}
setTabIndex={setTabIndex}
taskList={taskList}
setTaskList={setTaskList}
view={'gif'}
/>
)}
{tabIndex === 2 && (
<TaskListView
setEditTask={setEditTask}
setTabIndex={setTabIndex}
taskList={taskList}
setTaskList={setTaskList}
view={'task'}
/>
)}
{tabIndex === 3 && (
<AddPage
setTabIndex={setTabIndex}
setEditTask={setEditTask}
refreshWithSort={refreshWithSort}
refreshWithoutSort={refreshWithoutSort}
latestSortType={latestSortType}
sortDesc={sortDesc}
editTask={editTask}
/>
)}
<Tab
value={tabIndex}
style={{ backgroundColor: '#e64a19' }}
onChange={(event) => {
// clear the editTask
setEditTask({});
setTabIndex(event);
}}
indicatorStyle={{
backgroundColor: 'white',
height: 3,
}}
variant="primary"
>
<Tab.Item
title="Due"
titleStyle={{ fontSize: 12 }}
icon={{ name: 'clock', type: 'font-awesome-5', color: 'white' }}
/>
<Tab.Item
title="Gifs"
titleStyle={{ fontSize: 12 }}
icon={{
name: 'grin-squint',
type: 'font-awesome-5',
color: 'white',
}}
/>
<Tab.Item
title="Tasks"
titleStyle={{ fontSize: 12 }}
icon={{
name: 'tasks',
type: 'font-awesome-5',
color: 'white',
}}
/>
<Tab.Item
title="Add"
titleStyle={{ fontSize: 12 }}
icon={{
name: 'plus',
type: 'font-awesome-5',
color: 'white',
}}
/>
</Tab>
{tabIndex !== 3 && (
<SpeedDial
isOpen={openSpeedDial}
style={{ marginBottom: 58 }}
icon={{
name: 'sort-amount-down-alt',
type: 'font-awesome-5',
color: '#fff',
}}
openIcon={{
name: 'close',
color: '#fff',
}}
title={openSpeedDial && 'Sort by'}
transitionDuration={100}
onOpen={() => setOpenSpeedDial(!openSpeedDial)}
onClose={() => setOpenSpeedDial(!openSpeedDial)}
>
<SpeedDial.Action
icon={{
name: 'exclamation',
type: 'font-awesome-5',
color: '#fff',
}}
title="Priority"
onPress={() => handleSort('priority')}
/>
<SpeedDial.Action
icon={{
name: 'calendar-alt',
type: 'font-awesome-5',
color: '#fff',
}}
title="Date Added"
onPress={() => handleSort('created')}
/>
<SpeedDial.Action
icon={{ name: 'clock', type: 'font-awesome-5', color: '#fff' }}
title="Due Date"
onPress={() => handleSort('due_date')}
/>
<SpeedDial.Action
icon={{
name:
latestSortType === 'name' && sortDesc
? 'sort-alpha-down'
: 'sort-alpha-up',
type: 'font-awesome-5',
color: '#fff',
}}
title="Name"
onPress={() => handleSort('name')}
/>
</SpeedDial>
)}
</SafeAreaView>
</SafeAreaProvider>
</ThemeProvider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#e64a19',
},
});