Fix formatting and remove unnecessary code

This commit is contained in:
Simon Larsen 2023-11-30 13:59:13 +00:00
parent 97e8f2de8e
commit 3ac00ef54a
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
3 changed files with 83 additions and 47 deletions

View File

@ -32,7 +32,6 @@ export default class OneUptimeDate {
return this.getDaysBetweenTwoDates(startDate, endDate) + 1;
}
public static getHoursBetweenTwoDates(
startDate: Date,
endDate: Date
@ -129,8 +128,6 @@ export default class OneUptimeDate {
return this.getYearsBetweenTwoDates(startDate, endDate) + 1;
}
public static toString(date: Date): string {
return date.toISOString();
}

View File

@ -10,7 +10,6 @@ import EventInterval from '../Events/EventInterval';
import StartAndEndTime from '../Time/StartAndEndTime';
export default class LayerUtil {
public static getEvents(data: {
users: Array<UserModel>;
startDateTimeOfLayer: Date;
@ -20,7 +19,6 @@ export default class LayerUtil {
handOffTime: Date;
rotation: Recurring;
}): Array<CalendarEvent> {
let events: Array<CalendarEvent> = [];
let start: Date = data.calendarStartDate;
@ -70,36 +68,66 @@ export default class LayerUtil {
if (rotation.intervalType === EventInterval.Day) {
// calculate the number of days between the start time of the layer and the handoff time.
intervalBetweenStartTimeAndHandoffTime = OneUptimeDate.getDaysBetweenTwoDates(data.startDateTimeOfLayer, handOffTime);
intervalBetweenStartTimeAndHandoffTime =
OneUptimeDate.getDaysBetweenTwoDates(
data.startDateTimeOfLayer,
handOffTime
);
}
if (rotation.intervalType === EventInterval.Hour) {
// calculate the number of hours between the start time of the layer and the handoff time.
intervalBetweenStartTimeAndHandoffTime = OneUptimeDate.getHoursBetweenTwoDates(data.startDateTimeOfLayer, handOffTime);
intervalBetweenStartTimeAndHandoffTime =
OneUptimeDate.getHoursBetweenTwoDates(
data.startDateTimeOfLayer,
handOffTime
);
}
if (rotation.intervalType === EventInterval.Week) {
// calculate the number of weeks between the start time of the layer and the handoff time.
intervalBetweenStartTimeAndHandoffTime = OneUptimeDate.getWeeksBetweenTwoDates(data.startDateTimeOfLayer, handOffTime);
intervalBetweenStartTimeAndHandoffTime =
OneUptimeDate.getWeeksBetweenTwoDates(
data.startDateTimeOfLayer,
handOffTime
);
}
if (rotation.intervalType === EventInterval.Month) {
// calculate the number of months between the start time of the layer and the handoff time.
intervalBetweenStartTimeAndHandoffTime = OneUptimeDate.getMonthsBetweenTwoDates(data.startDateTimeOfLayer, handOffTime);
intervalBetweenStartTimeAndHandoffTime =
OneUptimeDate.getMonthsBetweenTwoDates(
data.startDateTimeOfLayer,
handOffTime
);
}
if (rotation.intervalType === EventInterval.Year) {
// calculate the number of years between the start time of the layer and the handoff time.
intervalBetweenStartTimeAndHandoffTime = OneUptimeDate.getYearsBetweenTwoDates(data.startDateTimeOfLayer, handOffTime);
intervalBetweenStartTimeAndHandoffTime =
OneUptimeDate.getYearsBetweenTwoDates(
data.startDateTimeOfLayer,
handOffTime
);
}
// now divide the interval between start time and handoff time by the interval count.
const numberOfIntervalsBetweenStartAndHandoffTime: number = Math.floor(intervalBetweenStartTimeAndHandoffTime / rotation.intervalCount.toNumber());
const numberOfIntervalsBetweenStartAndHandoffTime: number = Math.floor(
intervalBetweenStartTimeAndHandoffTime /
rotation.intervalCount.toNumber()
);
currentUserIndex = LayerUtil.incrementUserIndex(currentUserIndex, data.users.length, numberOfIntervalsBetweenStartAndHandoffTime);
currentUserIndex = LayerUtil.incrementUserIndex(
currentUserIndex,
data.users.length,
numberOfIntervalsBetweenStartAndHandoffTime
);
handOffTime = OneUptimeDate.keepTimeButMoveDay(handOffTime, currentEventStartTime);
handOffTime = OneUptimeDate.keepTimeButMoveDay(
handOffTime,
currentEventStartTime
);
// if the handoff time is before the current start time, we need to add one day to the handoff time
@ -109,7 +137,6 @@ export default class LayerUtil {
let currentEventEndTime: Date = OneUptimeDate.getCurrentDate(); // temporary set to current time to avoid typescript error
while (!hasReachedTheEndOfTheCalendar) {
if (rotation.intervalType === EventInterval.Day) {
const daysToAdd: number = rotation.intervalCount.toNumber();
@ -117,33 +144,24 @@ export default class LayerUtil {
currentEventStartTime,
daysToAdd
);
} else if (rotation.intervalType === EventInterval.Week) {
const weeksToAdd: number = rotation.intervalCount.toNumber();
currentEventEndTime = OneUptimeDate.addRemoveWeeks(
currentEventStartTime,
weeksToAdd
);
} else if (rotation.intervalType === EventInterval.Month) {
const monthsToAdd: number = rotation.intervalCount.toNumber();
currentEventEndTime = OneUptimeDate.addRemoveMonths(
currentEventStartTime,
monthsToAdd
);
} else if (rotation.intervalType === EventInterval.Year) {
const yearsToAdd: number = rotation.intervalCount.toNumber();
currentEventEndTime = OneUptimeDate.addRemoveYears(
currentEventStartTime,
yearsToAdd
);
} else if (rotation.intervalType === EventInterval.Hour) {
const hoursToAdd: number = rotation.intervalCount.toNumber();
currentEventEndTime = OneUptimeDate.addRemoveHours(
@ -167,13 +185,22 @@ export default class LayerUtil {
currentEventStartTime = handOffTime;
// update user index
currentUserIndex = LayerUtil.incrementUserIndex(currentUserIndex, data.users.length);
currentUserIndex = LayerUtil.incrementUserIndex(
currentUserIndex,
data.users.length
);
// add events to the array
events = [...events, ...LayerUtil.getCalendarEventsFromStartAndEndDates(trimmedStartAndEndTimes, data.users, currentUserIndex)];
events = [
...events,
...LayerUtil.getCalendarEventsFromStartAndEndDates(
trimmedStartAndEndTimes,
data.users,
currentUserIndex
),
];
}
// check calendar end time. if the end time of the event is after the end time of the calendar, we need to update the end time of the event
if (OneUptimeDate.isAfter(currentEventEndTime, end)) {
currentEventEndTime = end;
@ -189,14 +216,24 @@ export default class LayerUtil {
restrictionTimes: data.restrictionTImes,
});
events = [...events, ...LayerUtil.getCalendarEventsFromStartAndEndDates(trimmedStartAndEndTimes, data.users, currentUserIndex)];
events = [
...events,
...LayerUtil.getCalendarEventsFromStartAndEndDates(
trimmedStartAndEndTimes,
data.users,
currentUserIndex
),
];
// update the current event start time
currentEventStartTime = currentEventEndTime;
// update the current user index
currentUserIndex = LayerUtil.incrementUserIndex(currentUserIndex, data.users.length);
currentUserIndex = LayerUtil.incrementUserIndex(
currentUserIndex,
data.users.length
);
}
// increment ids of all the events and return them, to make sure they are unique
@ -402,11 +439,13 @@ export default class LayerUtil {
return [];
}
// helper functions.
private static incrementUserIndex(currentIndex: number, userArrayLength: number, incrementBy?: number): number {
private static incrementUserIndex(
currentIndex: number,
userArrayLength: number,
incrementBy?: number
): number {
// update the current user index
if (!incrementBy) {
@ -422,15 +461,16 @@ export default class LayerUtil {
}
return currentIndex;
};
private static getCalendarEventsFromStartAndEndDates(trimmedStartAndEndTimes: Array<StartAndEndTime>, users: Array<UserModel>, currentUserIndex: number): Array<CalendarEvent> {
}
private static getCalendarEventsFromStartAndEndDates(
trimmedStartAndEndTimes: Array<StartAndEndTime>,
users: Array<UserModel>,
currentUserIndex: number
): Array<CalendarEvent> {
const events: Array<CalendarEvent> = [];
const userId: string =
users[currentUserIndex]?.id?.toString() || '';
const userId: string = users[currentUserIndex]?.id?.toString() || '';
for (const trimmedStartAndEndTime of trimmedStartAndEndTimes) {
const event: CalendarEvent = {

View File

@ -39,9 +39,8 @@ const CalendarElement: FunctionComponent<ComponentProps> = (
localizer={localizer}
showMultiDayTimes
defaultView={props.defaultCalendarView || 'week'}
onRangeChange={(range: Date[] | { start: Date; end: Date; })=> {
if(Array.isArray(range)) {
onRangeChange={(range: Date[] | { start: Date; end: Date }) => {
if (Array.isArray(range)) {
return;
}