Browse Source

Fix planning management to match new api

Arnaud Vergnet 1 year ago
parent
commit
ea7321eb9d

+ 5
- 11
src/components/Home/PreviewEventDashboardItem.js View File

@@ -4,12 +4,12 @@ import * as React from 'react';
4 4
 import {StyleSheet, View} from 'react-native';
5 5
 import i18n from 'i18n-js';
6 6
 import {Avatar, Button, Card, TouchableRipple} from 'react-native-paper';
7
-import {getFormattedEventTime, isDescriptionEmpty} from '../../utils/Planning';
7
+import {getTimeOnlyString, isDescriptionEmpty} from '../../utils/Planning';
8 8
 import CustomHTML from '../Overrides/CustomHTML';
9
-import type {EventType} from '../../screens/Home/HomeScreen';
9
+import type {PlanningEventType} from '../../utils/Planning';
10 10
 
11 11
 type PropsType = {
12
-  event?: EventType | null,
12
+  event?: PlanningEventType | null,
13 13
   clickAction: () => void,
14 14
 };
15 15
 
@@ -62,19 +62,13 @@ class PreviewEventDashboardItem extends React.Component<PropsType> {
62 62
               {hasImage ? (
63 63
                 <Card.Title
64 64
                   title={event.title}
65
-                  subtitle={getFormattedEventTime(
66
-                    event.date_begin,
67
-                    event.date_end,
68
-                  )}
65
+                  subtitle={getTimeOnlyString(event.date_begin)}
69 66
                   left={getImage}
70 67
                 />
71 68
               ) : (
72 69
                 <Card.Title
73 70
                   title={event.title}
74
-                  subtitle={getFormattedEventTime(
75
-                    event.date_begin,
76
-                    event.date_end,
77
-                  )}
71
+                  subtitle={getTimeOnlyString(event.date_begin)}
78 72
                 />
79 73
               )}
80 74
               {!isEmpty ? (

+ 3
- 14
src/screens/Home/HomeScreen.js View File

@@ -28,6 +28,7 @@ import MascotPopup from '../../components/Mascot/MascotPopup';
28 28
 import DashboardManager from '../../managers/DashboardManager';
29 29
 import type {ServiceItemType} from '../../managers/ServicesManager';
30 30
 import {getDisplayEvent, getFutureEvents} from '../../utils/Home';
31
+import type {PlanningEventType} from '../../utils/Planning';
31 32
 // import DATA from "../dashboard_data.json";
32 33
 
33 34
 const DATA_URL =
@@ -49,24 +50,12 @@ export type FeedItemType = {
49 50
   page_id: string,
50 51
 };
51 52
 
52
-export type EventType = {
53
-  id: number,
54
-  title: string,
55
-  logo: string | null,
56
-  date_begin: string,
57
-  date_end: string,
58
-  description: string,
59
-  club: string,
60
-  category_id: number,
61
-  url: string,
62
-};
63
-
64 53
 export type FullDashboardType = {
65 54
   today_menu: Array<{[key: string]: {...}}>,
66 55
   proximo_articles: number,
67 56
   available_dryers: number,
68 57
   available_washers: number,
69
-  today_events: Array<EventType>,
58
+  today_events: Array<PlanningEventType>,
70 59
   available_tutorials: number,
71 60
 };
72 61
 
@@ -193,7 +182,7 @@ class HomeScreen extends React.Component<PropsType, StateType> {
193 182
    * @param content
194 183
    * @return {*}
195 184
    */
196
-  getDashboardEvent(content: Array<EventType>): React.Node {
185
+  getDashboardEvent(content: Array<PlanningEventType>): React.Node {
197 186
     const futureEvents = getFutureEvents(content);
198 187
     const displayEvent = getDisplayEvent(futureEvents);
199 188
     // const clickPreviewAction = () =>

+ 3
- 6
src/screens/Planning/PlanningDisplayScreen.js View File

@@ -5,7 +5,7 @@ import {View} from 'react-native';
5 5
 import {Card} from 'react-native-paper';
6 6
 import i18n from 'i18n-js';
7 7
 import {StackNavigationProp} from '@react-navigation/stack';
8
-import {getDateOnlyString, getFormattedEventTime} from '../../utils/Planning';
8
+import {getDateOnlyString, getTimeOnlyString} from '../../utils/Planning';
9 9
 import DateManager from '../../managers/DateManager';
10 10
 import BasicLoadingScreen from '../../components/Screens/BasicLoadingScreen';
11 11
 import {apiRequest, ERROR_TYPE} from '../../utils/WebData';
@@ -96,12 +96,9 @@ class PlanningDisplayScreen extends React.Component<PropsType, StateType> {
96 96
     const {navigation} = this.props;
97 97
     const {displayData} = this;
98 98
     if (displayData == null) return null;
99
-    let subtitle = getFormattedEventTime(
100
-      displayData.date_begin,
101
-      displayData.date_end,
102
-    );
99
+    let subtitle = getTimeOnlyString(displayData.date_begin);
103 100
     const dateString = getDateOnlyString(displayData.date_begin);
104
-    if (dateString !== null)
101
+    if (dateString !== null && subtitle != null)
105 102
       subtitle += ` | ${DateManager.getInstance().getTranslatedDate(
106 103
         dateString,
107 104
       )}`;

+ 3
- 3
src/screens/Planning/PlanningScreen.js View File

@@ -12,7 +12,7 @@ import {
12 12
   generateEventAgenda,
13 13
   getCurrentDateString,
14 14
   getDateOnlyString,
15
-  getFormattedEventTime,
15
+  getTimeOnlyString,
16 16
 } from '../../utils/Planning';
17 17
 import CustomAgenda from '../../components/Overrides/CustomAgenda';
18 18
 import {MASCOT_STYLE} from '../../components/Mascot/Mascot';
@@ -198,7 +198,7 @@ class PlanningScreen extends React.Component<PropsType, StateType> {
198 198
           <Divider />
199 199
           <List.Item
200 200
             title={item.title}
201
-            description={getFormattedEventTime(item.date_begin, item.date_end)}
201
+            description={getTimeOnlyString(item.date_begin)}
202 202
             left={(): React.Node => (
203 203
               <Avatar.Image
204 204
                 source={{uri: item.logo}}
@@ -215,7 +215,7 @@ class PlanningScreen extends React.Component<PropsType, StateType> {
215 215
         <Divider />
216 216
         <List.Item
217 217
           title={item.title}
218
-          description={getFormattedEventTime(item.date_begin, item.date_end)}
218
+          description={getTimeOnlyString(item.date_begin)}
219 219
           onPress={onPress}
220 220
         />
221 221
       </View>

+ 16
- 59
src/utils/Home.js View File

@@ -1,7 +1,7 @@
1 1
 // @flow
2 2
 
3 3
 import {stringToDate} from './Planning';
4
-import type {EventType} from '../screens/Home/HomeScreen';
4
+import type {PlanningEventType} from './Planning';
5 5
 
6 6
 /**
7 7
  * Gets the time limit depending on the current day:
@@ -21,20 +21,6 @@ export function getTodayEventTimeLimit(): Date {
21 21
 }
22 22
 
23 23
 /**
24
- * Gets the duration (in milliseconds) of an event
25
- *
26
- * @param event {EventType}
27
- * @return {number} The number of milliseconds
28
- */
29
-export function getEventDuration(event: EventType): number {
30
-  const start = stringToDate(event.date_begin);
31
-  const end = stringToDate(event.date_end);
32
-  let duration = 0;
33
-  if (start != null && end != null) duration = end - start;
34
-  return duration;
35
-}
36
-
37
-/**
38 24
  * Gets events starting after the limit
39 25
  *
40 26
  * @param events
@@ -42,11 +28,11 @@ export function getEventDuration(event: EventType): number {
42 28
  * @return {Array<Object>}
43 29
  */
44 30
 export function getEventsAfterLimit(
45
-  events: Array<EventType>,
31
+  events: Array<PlanningEventType>,
46 32
   limit: Date,
47
-): Array<EventType> {
33
+): Array<PlanningEventType> {
48 34
   const validEvents = [];
49
-  events.forEach((event: EventType) => {
35
+  events.forEach((event: PlanningEventType) => {
50 36
     const startDate = stringToDate(event.date_begin);
51 37
     if (startDate != null && startDate >= limit) {
52 38
       validEvents.push(event);
@@ -56,43 +42,18 @@ export function getEventsAfterLimit(
56 42
 }
57 43
 
58 44
 /**
59
- * Gets the event with the longest duration in the given array.
60
- * If all events have the same duration, return the first in the array.
61
- *
62
- * @param events
63
- */
64
-export function getLongestEvent(events: Array<EventType>): EventType {
65
-  let longestEvent = events[0];
66
-  let longestTime = 0;
67
-  events.forEach((event: EventType) => {
68
-    const time = getEventDuration(event);
69
-    if (time > longestTime) {
70
-      longestTime = time;
71
-      longestEvent = event;
72
-    }
73
-  });
74
-  return longestEvent;
75
-}
76
-
77
-/**
78 45
  * Gets events that have not yet ended/started
79 46
  *
80 47
  * @param events
81 48
  */
82
-export function getFutureEvents(events: Array<EventType>): Array<EventType> {
49
+export function getFutureEvents(
50
+  events: Array<PlanningEventType>,
51
+): Array<PlanningEventType> {
83 52
   const validEvents = [];
84 53
   const now = new Date();
85
-  events.forEach((event: EventType) => {
54
+  events.forEach((event: PlanningEventType) => {
86 55
     const startDate = stringToDate(event.date_begin);
87
-    const endDate = stringToDate(event.date_end);
88
-    if (startDate != null) {
89
-      if (startDate > now) validEvents.push(event);
90
-      else if (endDate != null) {
91
-        if (endDate > now || endDate < startDate)
92
-          // Display event if it ends the following day
93
-          validEvents.push(event);
94
-      }
95
-    }
56
+    if (startDate != null && startDate > now) validEvents.push(event);
96 57
   });
97 58
   return validEvents;
98 59
 }
@@ -101,23 +62,19 @@ export function getFutureEvents(events: Array<EventType>): Array<EventType> {
101 62
  * Gets the event to display in the preview
102 63
  *
103 64
  * @param events
104
- * @return {EventType | null}
65
+ * @return {PlanningEventType | null}
105 66
  */
106
-export function getDisplayEvent(events: Array<EventType>): EventType | null {
67
+export function getDisplayEvent(
68
+  events: Array<PlanningEventType>,
69
+): PlanningEventType | null {
107 70
   let displayEvent = null;
108 71
   if (events.length > 1) {
109 72
     const eventsAfterLimit = getEventsAfterLimit(
110 73
       events,
111 74
       getTodayEventTimeLimit(),
112 75
     );
113
-    if (eventsAfterLimit.length > 0) {
114
-      if (eventsAfterLimit.length === 1) [displayEvent] = eventsAfterLimit;
115
-      else displayEvent = getLongestEvent(events);
116
-    } else {
117
-      displayEvent = getLongestEvent(events);
118
-    }
119
-  } else if (events.length === 1) {
120
-    [displayEvent] = events;
121
-  }
76
+    if (eventsAfterLimit.length > 0) [displayEvent] = eventsAfterLimit;
77
+    else [displayEvent] = events;
78
+  } else if (events.length === 1) [displayEvent] = events;
122 79
   return displayEvent;
123 80
 }

+ 3
- 47
src/utils/Planning.js View File

@@ -3,13 +3,13 @@
3 3
 export type PlanningEventType = {
4 4
   id: number,
5 5
   title: string,
6
-  logo: string,
7 6
   date_begin: string,
8
-  date_end: string,
9
-  description: string,
10 7
   club: string,
11 8
   category_id: number,
9
+  description: string,
10
+  place: string,
12 11
   url: string,
12
+  logo: string | null,
13 13
 };
14 14
 
15 15
 // Regex used to check date string validity
@@ -121,50 +121,6 @@ export function getTimeOnlyString(dateString: string): string | null {
121 121
 }
122 122
 
123 123
 /**
124
- * Returns a string corresponding to the event start and end times in the following format:
125
- *
126
- * HH:MM - HH:MM
127
- *
128
- * If the end date is not specified or is equal to start time, only start time will be shown.
129
- *
130
- * If the end date is not on the same day, 23:59 will be shown as end time
131
- *
132
- * @param start Start time in YYYY-MM-DD HH:MM:SS format
133
- * @param end End time in YYYY-MM-DD HH:MM:SS format
134
- * @return {string} Formatted string or "/ - /" on error
135
- */
136
-export function getFormattedEventTime(start: string, end: string): string {
137
-  let formattedStr = '/ - /';
138
-  const startDate = stringToDate(start);
139
-  const endDate = stringToDate(end);
140
-
141
-  if (
142
-    startDate !== null &&
143
-    endDate !== null &&
144
-    startDate.getTime() !== endDate.getTime()
145
-  ) {
146
-    formattedStr = `${String(startDate.getHours()).padStart(2, '0')}:${String(
147
-      startDate.getMinutes(),
148
-    ).padStart(2, '0')} - `;
149
-    if (
150
-      endDate.getFullYear() > startDate.getFullYear() ||
151
-      endDate.getMonth() > startDate.getMonth() ||
152
-      endDate.getDate() > startDate.getDate()
153
-    )
154
-      formattedStr += '23:59';
155
-    else
156
-      formattedStr += `${String(endDate.getHours()).padStart(2, '0')}:${String(
157
-        endDate.getMinutes(),
158
-      ).padStart(2, '0')}`;
159
-  } else if (startDate !== null)
160
-    formattedStr = `${String(startDate.getHours()).padStart(2, '0')}:${String(
161
-      startDate.getMinutes(),
162
-    ).padStart(2, '0')}`;
163
-
164
-  return formattedStr;
165
-}
166
-
167
-/**
168 124
  * Checks if the given description can be considered empty.
169 125
  * <br>
170 126
  * An empty description is composed only of whitespace, <b>br</b> or <b>p</b> tags

Loading…
Cancel
Save