|
@@ -1,7 +1,7 @@
|
1
|
1
|
// @flow
|
2
|
2
|
|
3
|
3
|
import * as React from 'react';
|
4
|
|
-import {Button, Caption, Card, Headline, Subheading, Text, withTheme} from 'react-native-paper';
|
|
4
|
+import {Button, Caption, Card, Headline, Subheading, withTheme} from 'react-native-paper';
|
5
|
5
|
import {Collapsible} from "react-navigation-collapsible";
|
6
|
6
|
import {withCollapsible} from "../../../utils/withCollapsible";
|
7
|
7
|
import {StackNavigationProp} from "@react-navigation/stack";
|
|
@@ -11,12 +11,18 @@ import {Animated, BackHandler} from "react-native";
|
11
|
11
|
import * as Animatable from "react-native-animatable";
|
12
|
12
|
import {View} from "react-native-animatable";
|
13
|
13
|
import i18n from "i18n-js";
|
14
|
|
-import {dateToString, getTimeOnlyString, stringToDate} from "../../../utils/Planning";
|
15
|
14
|
import {CalendarList} from "react-native-calendars";
|
16
|
|
-import DateTimePicker from '@react-native-community/datetimepicker';
|
17
|
15
|
import LoadingConfirmDialog from "../../../components/Dialogs/LoadingConfirmDialog";
|
18
|
16
|
import ConnectionManager from "../../../managers/ConnectionManager";
|
19
|
17
|
import ErrorDialog from "../../../components/Dialogs/ErrorDialog";
|
|
18
|
+import {
|
|
19
|
+ generateMarkedDates,
|
|
20
|
+ getFirstEquipmentAvailability,
|
|
21
|
+ getISODate,
|
|
22
|
+ getRelativeDateString,
|
|
23
|
+ getValidRange,
|
|
24
|
+ isEquipmentAvailable
|
|
25
|
+} from "../../../utils/EquipmentBooking";
|
20
|
26
|
|
21
|
27
|
type Props = {
|
22
|
28
|
navigation: StackNavigationProp,
|
|
@@ -33,7 +39,6 @@ type State = {
|
33
|
39
|
dialogVisible: boolean,
|
34
|
40
|
errorDialogVisible: boolean,
|
35
|
41
|
markedDates: { [key: string]: { startingDay: boolean, endingDay: boolean, color: string } },
|
36
|
|
- timePickerVisible: boolean,
|
37
|
42
|
currentError: number,
|
38
|
43
|
}
|
39
|
44
|
|
|
@@ -43,32 +48,45 @@ class EquipmentRentScreen extends React.Component<Props, State> {
|
43
|
48
|
dialogVisible: false,
|
44
|
49
|
errorDialogVisible: false,
|
45
|
50
|
markedDates: {},
|
46
|
|
- timePickerVisible: false,
|
47
|
51
|
currentError: 0,
|
48
|
52
|
}
|
49
|
53
|
|
50
|
54
|
item: Device | null;
|
51
|
|
- selectedDates: {
|
52
|
|
- start: Date | null,
|
53
|
|
- end: Date | null,
|
54
|
|
- };
|
55
|
|
-
|
56
|
|
- currentlySelectedDate: Date | null;
|
|
55
|
+ bookedDates: Array<string>;
|
57
|
56
|
|
58
|
57
|
bookRef: { current: null | Animatable.View }
|
59
|
58
|
canBookEquipment: boolean;
|
60
|
59
|
|
|
60
|
+ lockedDates: { [key: string]: { startingDay: boolean, endingDay: boolean, color: string } }
|
|
61
|
+
|
61
|
62
|
constructor(props: Props) {
|
62
|
63
|
super(props);
|
63
|
64
|
this.resetSelection();
|
64
|
65
|
this.bookRef = React.createRef();
|
65
|
66
|
this.canBookEquipment = false;
|
|
67
|
+ this.bookedDates = [];
|
66
|
68
|
if (this.props.route.params != null) {
|
67
|
69
|
if (this.props.route.params.item != null)
|
68
|
70
|
this.item = this.props.route.params.item;
|
69
|
71
|
else
|
70
|
72
|
this.item = null;
|
71
|
73
|
}
|
|
74
|
+ const item = this.item;
|
|
75
|
+ if (item != null) {
|
|
76
|
+ this.lockedDates = {};
|
|
77
|
+ for (let i = 0; i < item.booked_at.length; i++) {
|
|
78
|
+ const range = getValidRange(new Date(item.booked_at[i].begin), new Date(item.booked_at[i].end), null);
|
|
79
|
+ this.lockedDates = {
|
|
80
|
+ ...this.lockedDates,
|
|
81
|
+ ...generateMarkedDates(
|
|
82
|
+ false,
|
|
83
|
+ this.props.theme,
|
|
84
|
+ range
|
|
85
|
+ )
|
|
86
|
+ };
|
|
87
|
+ }
|
|
88
|
+ }
|
|
89
|
+
|
72
|
90
|
}
|
73
|
91
|
|
74
|
92
|
/**
|
|
@@ -99,57 +117,14 @@ class EquipmentRentScreen extends React.Component<Props, State> {
|
99
|
117
|
* @return {boolean}
|
100
|
118
|
*/
|
101
|
119
|
onBackButtonPressAndroid = () => {
|
102
|
|
- if (this.currentlySelectedDate != null) {
|
|
120
|
+ if (this.bookedDates.length > 0) {
|
103
|
121
|
this.resetSelection();
|
104
|
|
- this.setState({
|
105
|
|
- markedDates: this.generateMarkedDates(),
|
106
|
|
- });
|
|
122
|
+ this.updateMarkedSelection();
|
107
|
123
|
return true;
|
108
|
124
|
} else
|
109
|
125
|
return false;
|
110
|
126
|
};
|
111
|
127
|
|
112
|
|
- isAvailable(item: Device) {
|
113
|
|
- const availableDate = stringToDate(item.available_at);
|
114
|
|
- return availableDate != null && availableDate < new Date();
|
115
|
|
- }
|
116
|
|
-
|
117
|
|
- /**
|
118
|
|
- * Gets the string representation of the given date.
|
119
|
|
- *
|
120
|
|
- * If the given date is the same day as today, only return the tile.
|
121
|
|
- * Otherwise, return the full date.
|
122
|
|
- *
|
123
|
|
- * @param dateString The string representation of the wanted date
|
124
|
|
- * @returns {string}
|
125
|
|
- */
|
126
|
|
- getDateString(dateString: string): string {
|
127
|
|
- const today = new Date();
|
128
|
|
- const date = stringToDate(dateString);
|
129
|
|
- if (date != null && today.getDate() === date.getDate()) {
|
130
|
|
- const str = getTimeOnlyString(dateString);
|
131
|
|
- return str != null ? str : "";
|
132
|
|
- } else
|
133
|
|
- return dateString;
|
134
|
|
- }
|
135
|
|
-
|
136
|
|
- /**
|
137
|
|
- * Gets the minimum date for renting equipment
|
138
|
|
- *
|
139
|
|
- * @param item The item to rent
|
140
|
|
- * @param isAvailable True is it is available right now
|
141
|
|
- * @returns {Date}
|
142
|
|
- */
|
143
|
|
- getMinDate(item: Device, isAvailable: boolean) {
|
144
|
|
- let date = new Date();
|
145
|
|
- if (isAvailable)
|
146
|
|
- return date;
|
147
|
|
- else {
|
148
|
|
- const limit = stringToDate(item.available_at)
|
149
|
|
- return limit != null ? limit : date;
|
150
|
|
- }
|
151
|
|
- }
|
152
|
|
-
|
153
|
128
|
/**
|
154
|
129
|
* Selects a new date on the calendar.
|
155
|
130
|
* If both start and end dates are already selected, unselect all.
|
|
@@ -157,149 +132,50 @@ class EquipmentRentScreen extends React.Component<Props, State> {
|
157
|
132
|
* @param day The day selected
|
158
|
133
|
*/
|
159
|
134
|
selectNewDate = (day: { dateString: string, day: number, month: number, timestamp: number, year: number }) => {
|
160
|
|
- this.currentlySelectedDate = new Date(day.dateString);
|
161
|
|
-
|
162
|
|
- if (!this.canBookEquipment) {
|
163
|
|
- const start = this.selectedDates.start;
|
164
|
|
- if (start == null)
|
165
|
|
- this.selectedDates.start = this.currentlySelectedDate;
|
166
|
|
- else if (this.currentlySelectedDate < start) {
|
167
|
|
- this.selectedDates.end = start;
|
168
|
|
- this.selectedDates.start = this.currentlySelectedDate;
|
|
135
|
+ const selected = new Date(day.dateString);
|
|
136
|
+ const start = this.getBookStartDate();
|
|
137
|
+
|
|
138
|
+ if (!(this.lockedDates.hasOwnProperty(day.dateString))) {
|
|
139
|
+ if (start === null) {
|
|
140
|
+ this.updateSelectionRange(selected, selected);
|
|
141
|
+ this.enableBooking();
|
|
142
|
+ } else if (start.getTime() === selected.getTime()) {
|
|
143
|
+ this.resetSelection();
|
|
144
|
+ } else if (this.bookedDates.length === 1) {
|
|
145
|
+ this.updateSelectionRange(start, selected);
|
|
146
|
+ this.enableBooking();
|
169
|
147
|
} else
|
170
|
|
- this.selectedDates.end = this.currentlySelectedDate;
|
171
|
|
- } else
|
172
|
|
- this.resetSelection();
|
173
|
|
-
|
174
|
|
- if (this.selectedDates.start != null) {
|
175
|
|
- this.setState({
|
176
|
|
- markedDates: this.generateMarkedDates(),
|
177
|
|
- timePickerVisible: true,
|
178
|
|
- });
|
179
|
|
- } else {
|
180
|
|
- this.setState({
|
181
|
|
- markedDates: this.generateMarkedDates(),
|
182
|
|
- });
|
183
|
|
- }
|
184
|
|
- }
|
185
|
|
-
|
186
|
|
- resetSelection() {
|
187
|
|
- if (this.canBookEquipment)
|
188
|
|
- this.hideBookButton();
|
189
|
|
- this.canBookEquipment = false;
|
190
|
|
- this.selectedDates = {start: null, end: null};
|
191
|
|
- this.currentlySelectedDate = null;
|
192
|
|
- }
|
193
|
|
-
|
194
|
|
- /**
|
195
|
|
- * Deselect the currently selected date
|
196
|
|
- */
|
197
|
|
- deselectCurrentDate() {
|
198
|
|
- let currentlySelectedDate = this.currentlySelectedDate;
|
199
|
|
- const start = this.selectedDates.start;
|
200
|
|
- const end = this.selectedDates.end;
|
201
|
|
- if (currentlySelectedDate != null && start != null) {
|
202
|
|
- if (currentlySelectedDate === start && end === null)
|
203
|
148
|
this.resetSelection();
|
204
|
|
- else if (end != null && currentlySelectedDate === end) {
|
205
|
|
- this.currentlySelectedDate = start;
|
206
|
|
- this.selectedDates.end = null;
|
207
|
|
- } else if (currentlySelectedDate === start) {
|
208
|
|
- this.currentlySelectedDate = end;
|
209
|
|
- this.selectedDates.start = this.selectedDates.end;
|
210
|
|
- this.selectedDates.end = null;
|
211
|
|
- }
|
|
149
|
+ this.updateMarkedSelection();
|
212
|
150
|
}
|
213
|
151
|
}
|
214
|
152
|
|
215
|
|
- /**
|
216
|
|
- * Saves the selected time to the currently selected date.
|
217
|
|
- * If no the time selection was canceled, cancels the current selecction
|
218
|
|
- *
|
219
|
|
- * @param event The click event
|
220
|
|
- * @param date The date selected
|
221
|
|
- */
|
222
|
|
- onTimeChange = (event: { nativeEvent: { timestamp: number }, type: string }, date: Date) => {
|
223
|
|
- let currentDate = this.currentlySelectedDate;
|
224
|
|
- const item = this.item;
|
225
|
|
- if (item != null && event.type === "set" && currentDate != null) {
|
226
|
|
- currentDate.setHours(date.getHours());
|
227
|
|
- currentDate.setMinutes(date.getMinutes());
|
228
|
|
-
|
229
|
|
- const isAvailable = this.isAvailable(item);
|
230
|
|
- let limit = this.getMinDate(item, isAvailable);
|
231
|
|
- // Prevent selecting a date before now
|
232
|
|
- if (this.getISODate(currentDate) === this.getISODate(limit) && currentDate < limit) {
|
233
|
|
- currentDate.setHours(limit.getHours());
|
234
|
|
- currentDate.setMinutes(limit.getMinutes());
|
235
|
|
- }
|
236
|
|
-
|
237
|
|
- if (this.selectedDates.start != null && this.selectedDates.end != null) {
|
238
|
|
- if (this.selectedDates.start > this.selectedDates.end) {
|
239
|
|
- const temp = this.selectedDates.start;
|
240
|
|
- this.selectedDates.start = this.selectedDates.end;
|
241
|
|
- this.selectedDates.end = temp;
|
242
|
|
- }
|
243
|
|
- this.canBookEquipment = true;
|
244
|
|
- this.showBookButton();
|
245
|
|
- }
|
246
|
|
- } else
|
247
|
|
- this.deselectCurrentDate();
|
|
153
|
+ updateSelectionRange(start: Date, end: Date) {
|
|
154
|
+ this.bookedDates = getValidRange(start, end, this.item);
|
|
155
|
+ }
|
248
|
156
|
|
|
157
|
+ updateMarkedSelection() {
|
249
|
158
|
this.setState({
|
250
|
|
- timePickerVisible: false,
|
251
|
|
- markedDates: this.generateMarkedDates(),
|
|
159
|
+ markedDates: generateMarkedDates(
|
|
160
|
+ true,
|
|
161
|
+ this.props.theme,
|
|
162
|
+ this.bookedDates
|
|
163
|
+ ),
|
252
|
164
|
});
|
253
|
165
|
}
|
254
|
166
|
|
255
|
|
- /**
|
256
|
|
- * Returns the ISO date format (without the time)
|
257
|
|
- *
|
258
|
|
- * @param date The date to recover the ISO format from
|
259
|
|
- * @returns {*}
|
260
|
|
- */
|
261
|
|
- getISODate(date: Date) {
|
262
|
|
- return date.toISOString().split("T")[0];
|
|
167
|
+ enableBooking() {
|
|
168
|
+ if (!this.canBookEquipment) {
|
|
169
|
+ this.showBookButton();
|
|
170
|
+ this.canBookEquipment = true;
|
|
171
|
+ }
|
263
|
172
|
}
|
264
|
173
|
|
265
|
|
- /**
|
266
|
|
- * Generates the object containing all marked dates between the start and end dates selected
|
267
|
|
- *
|
268
|
|
- * @returns {{}}
|
269
|
|
- */
|
270
|
|
- generateMarkedDates() {
|
271
|
|
- let markedDates = {}
|
272
|
|
- const start = this.selectedDates.start;
|
273
|
|
- const end = this.selectedDates.end;
|
274
|
|
- if (start != null) {
|
275
|
|
- const startISODate = this.getISODate(start);
|
276
|
|
- if (end != null && this.getISODate(end) !== startISODate) {
|
277
|
|
- markedDates[startISODate] = {
|
278
|
|
- startingDay: true,
|
279
|
|
- endingDay: false,
|
280
|
|
- color: this.props.theme.colors.primary
|
281
|
|
- };
|
282
|
|
- markedDates[this.getISODate(end)] = {
|
283
|
|
- startingDay: false,
|
284
|
|
- endingDay: true,
|
285
|
|
- color: this.props.theme.colors.primary
|
286
|
|
- };
|
287
|
|
- let date = new Date(start);
|
288
|
|
- date.setDate(date.getDate() + 1);
|
289
|
|
- while (date < end && this.getISODate(date) !== this.getISODate(end)) {
|
290
|
|
- markedDates[this.getISODate(date)] =
|
291
|
|
- {startingDay: false, endingDay: false, color: this.props.theme.colors.danger};
|
292
|
|
- date.setDate(date.getDate() + 1);
|
293
|
|
- }
|
294
|
|
- } else {
|
295
|
|
- markedDates[startISODate] = {
|
296
|
|
- startingDay: true,
|
297
|
|
- endingDay: true,
|
298
|
|
- color: this.props.theme.colors.primary
|
299
|
|
- };
|
300
|
|
- }
|
301
|
|
- }
|
302
|
|
- return markedDates;
|
|
174
|
+ resetSelection() {
|
|
175
|
+ if (this.canBookEquipment)
|
|
176
|
+ this.hideBookButton();
|
|
177
|
+ this.canBookEquipment = false;
|
|
178
|
+ this.bookedDates = [];
|
303
|
179
|
}
|
304
|
180
|
|
305
|
181
|
/**
|
|
@@ -349,15 +225,15 @@ class EquipmentRentScreen extends React.Component<Props, State> {
|
349
|
225
|
onDialogAccept = () => {
|
350
|
226
|
return new Promise((resolve) => {
|
351
|
227
|
const item = this.item;
|
352
|
|
- const start = this.selectedDates.start;
|
353
|
|
- const end = this.selectedDates.end;
|
|
228
|
+ const start = this.getBookStartDate();
|
|
229
|
+ const end = this.getBookEndDate();
|
354
|
230
|
if (item != null && start != null && end != null) {
|
355
|
231
|
ConnectionManager.getInstance().authenticatedRequest(
|
356
|
|
- "", // TODO set path
|
|
232
|
+ "location/booking",
|
357
|
233
|
{
|
358
|
|
- "id": item.id,
|
359
|
|
- "start": dateToString(start, false),
|
360
|
|
- "end": dateToString(end, false),
|
|
234
|
+ "device": item.id,
|
|
235
|
+ "begin": getISODate(start),
|
|
236
|
+ "end": getISODate(end),
|
361
|
237
|
})
|
362
|
238
|
.then(() => {
|
363
|
239
|
console.log("Success, replace screen");
|
|
@@ -373,20 +249,25 @@ class EquipmentRentScreen extends React.Component<Props, State> {
|
373
|
249
|
});
|
374
|
250
|
}
|
375
|
251
|
|
|
252
|
+ getBookStartDate() {
|
|
253
|
+ return this.bookedDates.length > 0 ? new Date(this.bookedDates[0]) : null;
|
|
254
|
+ }
|
|
255
|
+
|
|
256
|
+ getBookEndDate() {
|
|
257
|
+ const length = this.bookedDates.length;
|
|
258
|
+ return length > 0 ? new Date(this.bookedDates[length - 1]) : null;
|
|
259
|
+ }
|
|
260
|
+
|
376
|
261
|
render() {
|
377
|
262
|
const {containerPaddingTop, scrollIndicatorInsetTop, onScroll} = this.props.collapsibleStack;
|
378
|
|
- let startString = <Caption>{i18n.t('equipmentScreen.notSet')}</Caption>;
|
379
|
|
- let endString = <Caption>{i18n.t('equipmentScreen.notSet')}</Caption>;
|
380
|
|
- const start = this.selectedDates.start;
|
381
|
|
- const end = this.selectedDates.end;
|
382
|
|
- if (start != null)
|
383
|
|
- startString = dateToString(start, false);
|
384
|
|
- if (end != null)
|
385
|
|
- endString = dateToString(end, false);
|
386
|
263
|
|
387
|
264
|
const item = this.item;
|
|
265
|
+ const start = this.getBookStartDate();
|
|
266
|
+ const end = this.getBookEndDate();
|
|
267
|
+
|
388
|
268
|
if (item != null) {
|
389
|
|
- const isAvailable = this.isAvailable(item);
|
|
269
|
+ const isAvailable = isEquipmentAvailable(item);
|
|
270
|
+ const firstAvailability = getFirstEquipmentAvailability(item);
|
390
|
271
|
return (
|
391
|
272
|
<View style={{flex: 1}}>
|
392
|
273
|
<Animated.ScrollView
|
|
@@ -424,40 +305,32 @@ class EquipmentRentScreen extends React.Component<Props, State> {
|
424
|
305
|
color={isAvailable ? this.props.theme.colors.success : this.props.theme.colors.primary}
|
425
|
306
|
mode="text"
|
426
|
307
|
>
|
427
|
|
- {
|
428
|
|
- isAvailable
|
429
|
|
- ? i18n.t('equipmentScreen.available')
|
430
|
|
- : i18n.t('equipmentScreen.availableAt', {date: this.getDateString(item.available_at)})
|
431
|
|
- }
|
|
308
|
+ {i18n.t('equipmentScreen.available', {date: getRelativeDateString(firstAvailability)})}
|
432
|
309
|
</Button>
|
433
|
|
- <Text style={{
|
|
310
|
+ <Subheading style={{
|
434
|
311
|
textAlign: "center",
|
435
|
|
- marginBottom: 10
|
|
312
|
+ marginBottom: 10,
|
|
313
|
+ minHeight: 50
|
436
|
314
|
}}>
|
437
|
|
- {i18n.t('equipmentScreen.booking')}
|
438
|
|
- </Text>
|
439
|
|
- <Subheading style={{textAlign: "center"}}>
|
440
|
|
- {i18n.t('equipmentScreen.startDate')}
|
441
|
|
- {startString}
|
442
|
|
- </Subheading>
|
443
|
|
- <Subheading style={{textAlign: "center"}}>
|
444
|
|
- {i18n.t('equipmentScreen.endDate')}
|
445
|
|
- {endString}
|
|
315
|
+ {
|
|
316
|
+ start == null
|
|
317
|
+ ? i18n.t('equipmentScreen.booking')
|
|
318
|
+ : end != null && start.getTime() !== end.getTime()
|
|
319
|
+ ? i18n.t('equipmentScreen.bookingPeriod', {
|
|
320
|
+ begin: getRelativeDateString(start),
|
|
321
|
+ end: getRelativeDateString(end)
|
|
322
|
+ })
|
|
323
|
+ : i18n.t('equipmentScreen.bookingDay', {
|
|
324
|
+ date: getRelativeDateString(start)
|
|
325
|
+ })
|
|
326
|
+ }
|
|
327
|
+
|
446
|
328
|
</Subheading>
|
447
|
329
|
</Card.Content>
|
448
|
330
|
</Card>
|
449
|
|
- {this.state.timePickerVisible
|
450
|
|
- ? <DateTimePicker
|
451
|
|
- value={new Date()}
|
452
|
|
- mode={"time"}
|
453
|
|
- display={"clock"}
|
454
|
|
- is24Hour={true}
|
455
|
|
- onChange={this.onTimeChange}
|
456
|
|
- />
|
457
|
|
- : null}
|
458
|
331
|
<CalendarList
|
459
|
332
|
// Minimum date that can be selected, dates before minDate will be grayed out. Default = undefined
|
460
|
|
- minDate={this.getMinDate(item, isAvailable)}
|
|
333
|
+ minDate={new Date()}
|
461
|
334
|
// Max amount of months allowed to scroll to the past. Default = 50
|
462
|
335
|
pastScrollRange={0}
|
463
|
336
|
// Max amount of months allowed to scroll to the future. Default = 50
|
|
@@ -476,7 +349,7 @@ class EquipmentRentScreen extends React.Component<Props, State> {
|
476
|
349
|
hideArrows={false}
|
477
|
350
|
// Date marking style [simple/period/multi-dot/custom]. Default = 'simple'
|
478
|
351
|
markingType={'period'}
|
479
|
|
- markedDates={this.state.markedDates}
|
|
352
|
+ markedDates={{...this.lockedDates, ...this.state.markedDates}}
|
480
|
353
|
|
481
|
354
|
theme={{
|
482
|
355
|
backgroundColor: this.props.theme.colors.agendaBackgroundColor,
|