Browse Source

Display events on the same day in the right order, prevent refresh spam

keplyx 2 years ago
parent
commit
74a587cac7
1 changed files with 54 additions and 15 deletions
  1. 54
    15
      screens/PlanningScreen.js

+ 54
- 15
screens/PlanningScreen.js View File

@@ -44,6 +44,9 @@ export default class PlanningScreen extends React.Component<Props, State> {
44 44
     modalRef: { current: null | Modalize };
45 45
     webDataManager: WebDataManager;
46 46
 
47
+    lastRefresh: Date;
48
+    minTimeBetweenRefresh = 60;
49
+
47 50
     constructor(props: any) {
48 51
         super(props);
49 52
         this.modalRef = React.createRef();
@@ -198,31 +201,67 @@ export default class PlanningScreen extends React.Component<Props, State> {
198 201
      * @private
199 202
      */
200 203
     _onRefresh = () => {
201
-        this.setState({refreshing: true});
202
-        this.webDataManager.readData()
203
-            .then((fetchedData) => {
204
-                this.setState({
205
-                    refreshing: false,
206
-                });
207
-                this.generateEventAgenda(fetchedData);
208
-            })
209
-            .catch((err) => {
210
-                this.setState({
211
-                    refreshing: false,
204
+        let canRefresh;
205
+        if (this.lastRefresh !== undefined)
206
+            canRefresh = (new Date().getTime() - this.lastRefresh.getTime())/1000 > this.minTimeBetweenRefresh;
207
+        else
208
+            canRefresh = true;
209
+
210
+        if (canRefresh) {
211
+            this.setState({refreshing: true});
212
+            this.webDataManager.readData()
213
+                .then((fetchedData) => {
214
+                    this.setState({
215
+                        refreshing: false,
216
+                    });
217
+                    this.generateEventAgenda(fetchedData);
218
+                    this.lastRefresh = new Date();
219
+                })
220
+                .catch((err) => {
221
+                    this.setState({
222
+                        refreshing: false,
223
+                    });
224
+                    console.log(err);
212 225
                 });
213
-                console.log(err);
214
-            });
226
+        }
215 227
     };
216 228
 
217 229
     generateEventAgenda(eventList: Array<Object>) {
218 230
         let agendaItems = this.generateEmptyCalendar();
219 231
         for (let i = 0; i < eventList.length; i++) {
220
-            if (agendaItems[this.getEventStartDate(eventList[i])] !== undefined)
221
-                agendaItems[this.getEventStartDate(eventList[i])].push(eventList[i]);
232
+            if (agendaItems[this.getEventStartDate(eventList[i])] !== undefined) {
233
+                this.pushEventInOrder(agendaItems, eventList[i], this.getEventStartDate(eventList[i]));
234
+            }
222 235
         }
223 236
         this.setState({agendaItems: agendaItems})
224 237
     }
225 238
 
239
+    pushEventInOrder(agendaItems: Object, event: Object, startDate: string) {
240
+        if (agendaItems[startDate].length === 0)
241
+            agendaItems[startDate].push(event);
242
+        else {
243
+            for (let i = 0; i < agendaItems[startDate].length; i++) {
244
+                if (this.isEventBefore(event, agendaItems[startDate][i])) {
245
+                    agendaItems[startDate].splice(i, 0, event);
246
+                    break;
247
+                } else if (i === agendaItems[startDate].length - 1) {
248
+                    agendaItems[startDate].push(event);
249
+                    break;
250
+                }
251
+            }
252
+        }
253
+    }
254
+
255
+    isEventBefore(event1: Object, event2: Object) {
256
+        let date1 = new Date();
257
+        let date2 = new Date();
258
+        let timeArray = this.getEventStartTime(event1).split(":");
259
+        date1.setHours(parseInt(timeArray[0]), parseInt(timeArray[1]));
260
+        timeArray = this.getEventStartTime(event2).split(":");
261
+        date2.setHours(parseInt(timeArray[0]), parseInt(timeArray[1]));
262
+        return date1 < date2;
263
+    }
264
+
226 265
     getEventStartDate(event: Object) {
227 266
         return event.date_begin.split(" ")[0];
228 267
     }

Loading…
Cancel
Save