Allow events to span on multiple days

This commit is contained in:
Arnaud Vergnet 2020-03-21 20:32:28 +01:00
parent 7a3d5f16b1
commit 40d7985bbd
4 changed files with 100 additions and 44 deletions

View file

@ -59,7 +59,7 @@ export default class PlanningScreen extends React.Component<Props, State> {
onAgendaRef: Function; onAgendaRef: Function;
onCalendarToggled: Function; onCalendarToggled: Function;
onBackButtonPressAndroid: Function; onBackButtonPressAndroid: Function;
currentDate = PlanningEventManager.getCurrentDateString(); currentDate = PlanningEventManager.getDateOnlyString(PlanningEventManager.getCurrentDateString());
constructor(props: any) { constructor(props: any) {
super(props); super(props);
@ -108,12 +108,14 @@ export default class PlanningScreen extends React.Component<Props, State> {
}; };
generateEmptyCalendar() { generateEmptyCalendar() {
let end = new Date(new Date().setMonth(new Date().getMonth() + AGENDA_MONTH_SPAN + 1)); let end = new Date(new Date().setMonth(new Date().getMonth() + AGENDA_MONTH_SPAN + 1));
let daysOfYear = {}; let daysOfYear = {};
for (let d = new Date(); d <= end; d.setDate(d.getDate() + 1)) { for (let d = new Date(); d <= end; d.setDate(d.getDate() + 1)) {
daysOfYear[PlanningEventManager.dateToString(new Date(d))] = [] daysOfYear[
PlanningEventManager.getDateOnlyString(
PlanningEventManager.dateToString(new Date(d))
)] = []
} }
return daysOfYear; return daysOfYear;
} }
@ -192,16 +194,53 @@ export default class PlanningScreen extends React.Component<Props, State> {
} }
}; };
getClonedEventArray(event: Object, times: number) {
let cloneArray = [];
if (times > 1) {
for (let i = 0; i < times; i++) {
let clone = JSON.parse(JSON.stringify(event));
let startDate = PlanningEventManager.stringToDate(clone["date_begin"]);
let endDate = new Date();
if (i !== 0) {
startDate.setHours(0, 0, 0);
startDate.setDate(startDate.getDate() + i);
clone["date_begin"] = PlanningEventManager.dateToString(startDate);
}
if (i !== (times - 1)) {
endDate = PlanningEventManager.stringToDate(clone["date_end"]);
endDate.setHours(23, 59, 0);
endDate.setFullYear(startDate.getFullYear(),
startDate.getMonth(),
startDate.getDate() + i);
clone["date_end"] = PlanningEventManager.dateToString(endDate);
}
cloneArray.push(clone)
}
} else
cloneArray = [event];
return cloneArray;
}
generateEventAgenda(eventList: Array<Object>) { generateEventAgenda(eventList: Array<Object>) {
let agendaItems = this.generateEmptyCalendar(); let agendaItems = this.generateEmptyCalendar();
for (let i = 0; i < eventList.length; i++) { for (let i = 0; i < eventList.length; i++) {
if (PlanningEventManager.getDateOnlyString(eventList[i]["date_begin"]) !== undefined) { if (PlanningEventManager.getDateOnlyString(eventList[i]["date_begin"]) !== undefined) {
this.pushEventInOrder(agendaItems, eventList[i], PlanningEventManager.getDateOnlyString(eventList[i]["date_begin"])); const clonedEventArray = this.getClonedEventArray(
eventList[i],
PlanningEventManager.getEventDaysNumber(eventList[i]["date_begin"], eventList[i]["date_end"])
);
this.pushEvents(agendaItems, clonedEventArray);
} }
} }
this.setState({agendaItems: agendaItems}) this.setState({agendaItems: agendaItems})
} }
pushEvents(agendaItems: Object, eventList: Array<Object>) {
for (let i = 0; i < eventList.length; i++) {
this.pushEventInOrder(agendaItems, eventList[i], PlanningEventManager.getDateOnlyString(eventList[i]["date_begin"]));
}
}
pushEventInOrder(agendaItems: Object, event: Object, startDate: string) { pushEventInOrder(agendaItems: Object, event: Object, startDate: string) {
if (agendaItems[startDate].length === 0) if (agendaItems[startDate].length === 0)
agendaItems[startDate].push(event); agendaItems[startDate].push(event);

View file

@ -8,13 +8,13 @@ export default class DateManager {
monthsOfYear = []; monthsOfYear = [];
constructor() { constructor() {
this.daysOfWeek.push(i18n.t("date.daysOfWeek.sunday")); // 0 represents sunday
this.daysOfWeek.push(i18n.t("date.daysOfWeek.monday")); this.daysOfWeek.push(i18n.t("date.daysOfWeek.monday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.tuesday")); this.daysOfWeek.push(i18n.t("date.daysOfWeek.tuesday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.wednesday")); this.daysOfWeek.push(i18n.t("date.daysOfWeek.wednesday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.thursday")); this.daysOfWeek.push(i18n.t("date.daysOfWeek.thursday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.friday")); this.daysOfWeek.push(i18n.t("date.daysOfWeek.friday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.saturday")); this.daysOfWeek.push(i18n.t("date.daysOfWeek.saturday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.sunday"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.january")); this.monthsOfYear.push(i18n.t("date.monthsOfYear.january"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.february")); this.monthsOfYear.push(i18n.t("date.monthsOfYear.february"));
@ -44,7 +44,7 @@ export default class DateManager {
let dateArray = dateString.split('-'); let dateArray = dateString.split('-');
let date = new Date(); let date = new Date();
date.setFullYear(parseInt(dateArray[0]), parseInt(dateArray[1]) - 1, parseInt(dateArray[2])); date.setFullYear(parseInt(dateArray[0]), parseInt(dateArray[1]) - 1, parseInt(dateArray[2]));
return this.daysOfWeek[date.getDay() - 1] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear(); return this.daysOfWeek[date.getDay()] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear();
} }
} }

View file

@ -9,10 +9,33 @@ export default class PlanningEventManager {
* *
* @return {string} The string representation * @return {string} The string representation
*/ */
static getCurrentDateString() { static getCurrentDateString(): string {
return PlanningEventManager.dateToString(new Date()); return PlanningEventManager.dateToString(new Date());
} }
/**
* Gets how many days the event lasts. If no end date is specified, defaults to 1.
*
*
* @param start The start date string in format YYYY-MM-DD HH:MM:SS
* @param end The end date string in format YYYY-MM-DD HH:MM:SS
* @return {number} The number of days, 0 on error
*/
static getEventDaysNumber(start: string, end: string): number {
let startDate = PlanningEventManager.stringToDate(start);
let endDate = PlanningEventManager.stringToDate(end);
if (startDate !== undefined && endDate !== undefined) {
if (startDate.getTime() !== endDate.getTime()) {
const diffTime = endDate - startDate;
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
} else
return 1;
} else if (startDate !== undefined)
return 1;
else
return 0;
}
/** /**
* Checks if the given date is before the other. * Checks if the given date is before the other.
@ -89,26 +112,19 @@ export default class PlanningEventManager {
/** /**
* Converts a date object to a string in the format * Converts a date object to a string in the format
* YYYY-MM-DD * YYYY-MM-DD HH-MM-SS
* *
* @param date The date object to convert * @param date The date object to convert
* @return {string} The converted string * @return {string} The converted string
*/ */
static dateToString(date: Date) { static dateToString(date: Date) {
let dd = String(date.getDate()).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0');
let mm = String(date.getMonth() + 1).padStart(2, '0'); //January is 0! const month = String(date.getMonth() + 1).padStart(2, '0'); //January is 0!
let yyyy = date.getFullYear(); const year = date.getFullYear();
return yyyy + '-' + mm + '-' + dd; const hours = String(date.getHours()).padStart(2, '0');
} const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
/** return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
* Returns a padded string for the given number if it is lower than 10
*
* @param i The string to be converted
* @return {string}
*/
static toPaddedString(i: number): string {
return (i < 10 && i >= 0) ? "0" + i.toString(10) : i.toString(10);
} }
/** /**
@ -118,7 +134,7 @@ export default class PlanningEventManager {
* *
* If the end date is not specified or is equal to start time, only start time will be shown. * If the end date is not specified or is equal to start time, only start time will be shown.
* *
* If the end date is not on the same day, 00:00 will be shown as end time * If the end date is not on the same day, 23:59 will be shown as end time
* *
* @param start Start time in YYYY-MM-DD HH:MM:SS format * @param start Start time in YYYY-MM-DD HH:MM:SS format
* @param end End time in YYYY-MM-DD HH:MM:SS format * @param end End time in YYYY-MM-DD HH:MM:SS format
@ -130,19 +146,19 @@ export default class PlanningEventManager {
let endDate = PlanningEventManager.stringToDate(end); let endDate = PlanningEventManager.stringToDate(end);
if (startDate !== undefined && endDate !== undefined && startDate.getTime() !== endDate.getTime()) { if (startDate !== undefined && endDate !== undefined && startDate.getTime() !== endDate.getTime()) {
formattedStr = PlanningEventManager.toPaddedString(startDate.getHours()) + ':' formattedStr = String(startDate.getHours()).padStart(2, '0') + ':'
+ PlanningEventManager.toPaddedString(startDate.getMinutes()) + ' - '; + String(startDate.getMinutes()).padStart(2, '0') + ' - ';
if (endDate.getFullYear() > startDate.getFullYear() if (endDate.getFullYear() > startDate.getFullYear()
|| endDate.getMonth() > startDate.getMonth() || endDate.getMonth() > startDate.getMonth()
|| endDate.getDate() > startDate.getDate()) || endDate.getDate() > startDate.getDate())
formattedStr += '00:00'; formattedStr += '23:59';
else else
formattedStr += PlanningEventManager.toPaddedString(endDate.getHours()) + ':' formattedStr += String(endDate.getHours()).padStart(2, '0') + ':'
+ PlanningEventManager.toPaddedString(endDate.getMinutes()); + String(endDate.getMinutes()).padStart(2, '0');
} else if (startDate !== undefined) } else if (startDate !== undefined)
formattedStr = formattedStr =
PlanningEventManager.toPaddedString(startDate.getHours()) + ':' String(startDate.getHours()).padStart(2, '0') + ':'
+ PlanningEventManager.toPaddedString(startDate.getMinutes()); + String(startDate.getMinutes()).padStart(2, '0');
return formattedStr return formattedStr
} }

View file

@ -17,16 +17,6 @@ test('isDescriptionEmpty', () => {
expect(PlanningEventManager.isDescriptionEmpty("<p>coucou</p>")).toBeFalse(); expect(PlanningEventManager.isDescriptionEmpty("<p>coucou</p>")).toBeFalse();
}); });
test('toPaddedString', () => {
expect(PlanningEventManager.toPaddedString(-1)).toBe("-1");
expect(PlanningEventManager.toPaddedString(0)).toBe("00");
expect(PlanningEventManager.toPaddedString(1)).toBe("01");
expect(PlanningEventManager.toPaddedString(2)).toBe("02");
expect(PlanningEventManager.toPaddedString(10)).toBe("10");
expect(PlanningEventManager.toPaddedString(53)).toBe("53");
expect(PlanningEventManager.toPaddedString(100)).toBe("100");
});
test('isEventDateStringFormatValid', () => { test('isEventDateStringFormatValid', () => {
expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21 09:00:00")).toBeTrue(); expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21 09:00:00")).toBeTrue();
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 01:16:65")).toBeTrue(); expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 01:16:65")).toBeTrue();
@ -77,7 +67,7 @@ test('getFormattedEventTime', () => {
expect(PlanningEventManager.getFormattedEventTime("2020-03-21 09:00:00", "2020-03-21 09:00:00")) expect(PlanningEventManager.getFormattedEventTime("2020-03-21 09:00:00", "2020-03-21 09:00:00"))
.toBe('09:00'); .toBe('09:00');
expect(PlanningEventManager.getFormattedEventTime("2020-03-21 09:00:00", "2020-03-22 17:00:00")) expect(PlanningEventManager.getFormattedEventTime("2020-03-21 09:00:00", "2020-03-22 17:00:00"))
.toBe('09:00 - 00:00'); .toBe('09:00 - 23:59');
expect(PlanningEventManager.getFormattedEventTime("2020-03-30 20:30:00", "2020-03-30 23:00:00")) expect(PlanningEventManager.getFormattedEventTime("2020-03-30 20:30:00", "2020-03-30 23:00:00"))
.toBe('20:30 - 23:00'); .toBe('20:30 - 23:00');
}); });
@ -127,10 +117,21 @@ test('isEventBefore', () => {
test('dateToString', () => { test('dateToString', () => {
let testDate = new Date(); let testDate = new Date();
testDate.setFullYear(2020, 2, 21); testDate.setFullYear(2020, 2, 21);
expect(PlanningEventManager.dateToString(testDate)).toBe("2020-03-21"); testDate.setHours(9, 0, 0, 0);
expect(PlanningEventManager.dateToString(testDate)).toBe("2020-03-21 09:00:00");
testDate.setFullYear(2021, 0, 12); testDate.setFullYear(2021, 0, 12);
expect(PlanningEventManager.dateToString(testDate)).toBe("2021-01-12"); testDate.setHours(9, 10, 0, 0);
expect(PlanningEventManager.dateToString(testDate)).toBe("2021-01-12 09:10:00");
testDate.setFullYear(2022, 11, 31); testDate.setFullYear(2022, 11, 31);
expect(PlanningEventManager.dateToString(testDate)).toBe("2022-12-31"); testDate.setHours(9, 10, 15, 0);
expect(PlanningEventManager.dateToString(testDate)).toBe("2022-12-31 09:10:15");
}); });
test('getEventDaysNumber', () => {
expect(PlanningEventManager.getEventDaysNumber('2020-03-21 09:00:00', '2020-03-22 17:00:00')).toBe(2);
expect(PlanningEventManager.getEventDaysNumber('2020-03-21 09:00:00', '2020-03-21 17:00:00')).toBe(1);
expect(PlanningEventManager.getEventDaysNumber('2020-03-21 09:00:00', '2020-03-21 09:00:00')).toBe(1);
expect(PlanningEventManager.getEventDaysNumber('2020-03-21 09:gg:00', '2020-03-21 17:00:00')).toBe(0);
expect(PlanningEventManager.getEventDaysNumber('2020-03-21 09:00:00', undefined)).toBe(1);
expect(PlanningEventManager.getEventDaysNumber('2020-03-21 09:00:00', '2020-04-05 20:00:00')).toBe(16);
});