Skip to content

Commit

Permalink
Merge pull request #169 from Kleostro/fix/tu-02-04/display-dates
Browse files Browse the repository at this point in the history
fix(tu-02-04): display dates
  • Loading branch information
ki8vi authored Aug 29, 2024
2 parents b185758 + 71d12a4 commit 824b681
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/app/home/services/filter/filter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,21 @@ export class FilterService implements OnDestroy {
const filteredSchedule = [];
const { segments, rideId } = schedule[ride];
const targetSegment = segments[fromStationIdIndex];
if (targetSegment.time[0] >= targetDate) {
const departureDate = targetSegment.time[0].split('T')[0];
const nextDay = new Date(targetDate);
nextDay.setDate(nextDay.getDate() + 1);
nextDay.setHours(0, 0, 0, 0);
if (targetSegment.time[0] > targetDate && new Date(targetDate).getTime() < nextDay.getTime()) {
const departureDate = this.formatDate(new Date(targetSegment.time[0]));
filteredSchedule.push({
rideId,
segments,
segments: segments.map((currSegment) => {
const departureLocalDate = new Date(currSegment.time[0]).toString();
const arrivalLocalDate = new Date(currSegment.time[1]).toString();
return {
...currSegment,
time: [departureLocalDate, arrivalLocalDate],
};
}),
});
if (!groupedRoutes[departureDate]) {
groupedRoutes[departureDate] = [];
Expand All @@ -83,18 +93,12 @@ export class FilterService implements OnDestroy {
}
}
}

Object.keys(groupedRoutes).forEach((keyDate) => {
groupedRoutes[keyDate] = groupedRoutes[keyDate].filter((route) =>
route.schedule.some((ride) => ride.segments.some((segment) => segment.time[0].split('T')[0] === keyDate)),
);
});
return this.generateMissingKeyDates(groupedRoutes, targetDate);
return this.generateMissingKeyDates(this.filterRoutesByKeyDate(groupedRoutes), targetDate);
}

private generateMissingKeyDates(groupedRoutes: GroupedRoutes, targetDate: string): GroupedRoutes {
const updatedGroupedRoutes = { ...groupedRoutes };
const dateKeys = Object.keys(updatedGroupedRoutes).sort((a, b) => a.localeCompare(b));
const dateKeys = Object.keys(updatedGroupedRoutes).sort();
const startDate = new Date(targetDate);
const endDate = new Date(dateKeys[dateKeys.length - 1]);

Expand All @@ -112,6 +116,18 @@ export class FilterService implements OnDestroy {
return date.toLocaleDateString('en-CA');
}

private filterRoutesByKeyDate(groupedRoutes: GroupedRoutes): GroupedRoutes {
const filteredRoutes = { ...groupedRoutes };
Object.keys(groupedRoutes).forEach((keyDate) => {
filteredRoutes[keyDate] = groupedRoutes[keyDate].filter((route) =>
route.schedule.some((ride) =>
ride.segments.some((segment) => this.formatDate(new Date(segment.time[0])) === keyDate),
),
);
});
return filteredRoutes;
}

public ngOnDestroy(): void {
if (this.subscription) {
this.subscription.unsubscribe();
Expand Down

0 comments on commit 824b681

Please sign in to comment.