Skip to content

Commit f1e5b47

Browse files
kagiurazwliew
authored andcommitted
refactor: 🚨 fixed dependency loop + fixed SVCs with gaps during the day to show "resume" status properly
1 parent 2b0bf12 commit f1e5b47

File tree

3 files changed

+67
-60
lines changed

3 files changed

+67
-60
lines changed

website/src/utils/mobility.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import isbServicesJSON from '../data/isb-services.json';
2+
3+
const isbServices = isbServicesJSON;
4+
5+
export const timeIsBefore = (a: Date, b: string) => {
6+
const parse = (x: string) => parseInt(x, 10);
7+
const ta = new Date(0, 0, 0, a.getHours(), a.getMinutes());
8+
const tb = new Date(0, 0, 0, ...b.split(':').map(parse));
9+
return ta < tb;
10+
};
11+
12+
export const timeIsAfter = (a: Date, b: string) => !timeIsBefore(a, b);
13+
14+
export const isWithinBlock = (time: Date, block: ScheduleBlock) => {
15+
const { from, to } = block;
16+
return timeIsBefore(time, to) && timeIsAfter(time, from);
17+
};
18+
19+
export const getServiceStatus = (period: 'term' | 'vacation' = 'term') => {
20+
const time = new Date();
21+
// used to debug:
22+
// time.setHours(time.getHours() - 2);
23+
24+
const serviceStatuses: ServiceStatus[] = isbServices.map((service) => {
25+
const todaySchedule = service.schedule[period][time.getDay()];
26+
const currentBlock = todaySchedule.find((t) => isWithinBlock(time, t));
27+
if (currentBlock) {
28+
return {
29+
id: service.id,
30+
running: true,
31+
currentBlock,
32+
};
33+
}
34+
35+
let nextBlock;
36+
let i = -1;
37+
while (!nextBlock && i < 7) {
38+
i += 1;
39+
// if this day has schedule
40+
if (service.schedule[period][(time.getDay() + i) % 7].length > 0) {
41+
if (i === 0) {
42+
// today
43+
nextBlock = todaySchedule.find((t) => timeIsBefore(time, t.from));
44+
} else [nextBlock] = service.schedule[period][(time.getDay() + i) % 7];
45+
}
46+
}
47+
48+
console.log(nextBlock, i, 'service:', service.id);
49+
if (nextBlock) {
50+
return {
51+
id: service.id,
52+
running: false,
53+
runningThisPeriod: true,
54+
nextDay: (time.getDay() + i) % 7,
55+
nextTime: nextBlock.from,
56+
};
57+
}
58+
return {
59+
id: service.id,
60+
running: false,
61+
runningThisPeriod: false,
62+
};
63+
});
64+
return serviceStatuses;
65+
};

website/src/views/mobility/MobilityContainer/MobilityContainer.tsx

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import useScrollToTop from 'views/hooks/useScrollToTop';
88

99
import LocationMap from 'views/components/bus-map/LocationMap';
1010
import NoFooter from 'views/layout/NoFooter';
11-
import { set } from 'lodash';
11+
import { getServiceStatus } from 'utils/mobility';
1212
import styles from './MobilityContainer.scss';
1313
import ServiceDetails from '../ServiceDetails';
1414
import ServiceList from '../ServiceList';
@@ -44,64 +44,6 @@ const getRouteSegments = (stops: string[], color: string) => {
4444
return routes;
4545
};
4646

47-
const timeIsBefore = (a: Date, b: string) => {
48-
const parse = (x: string) => parseInt(x, 10);
49-
const ta = new Date(0, 0, 0, a.getHours(), a.getMinutes());
50-
const tb = new Date(0, 0, 0, ...b.split(':').map(parse));
51-
return ta < tb;
52-
};
53-
54-
const isWithinBlock = (time: Date, block: ScheduleBlock) => {
55-
const { from, to } = block;
56-
return timeIsBefore(time, to) && !timeIsBefore(time, from);
57-
};
58-
59-
export { isWithinBlock };
60-
61-
const getServiceStatus = (period: 'term' | 'vacation' = 'vacation') => {
62-
const time = new Date();
63-
// +12 hrs
64-
// time.setHours(time.getHours() + 8 + 24);
65-
const serviceStatuses: ServiceStatus[] = isbServices.map((service) => {
66-
const todaySchedule = service.schedule[period][time.getDay()];
67-
const currentBlock = todaySchedule.find((t) => isWithinBlock(time, t));
68-
if (currentBlock) {
69-
return {
70-
id: service.id,
71-
running: true,
72-
currentBlock,
73-
};
74-
}
75-
76-
let nextBlock;
77-
let i = -1;
78-
79-
while (!nextBlock && i < 7) {
80-
i += 1;
81-
// if this day has schedule
82-
if (service.schedule[period][(time.getDay() + i) % 7].length > 0) {
83-
[nextBlock] = service.schedule[period][(time.getDay() + i) % 7];
84-
}
85-
}
86-
87-
if (nextBlock) {
88-
return {
89-
id: service.id,
90-
running: false,
91-
runningThisPeriod: true,
92-
nextDay: (time.getDay() + i) % 7,
93-
nextTime: nextBlock.from,
94-
};
95-
}
96-
return {
97-
id: service.id,
98-
running: false,
99-
runningThisPeriod: false,
100-
};
101-
});
102-
return serviceStatuses;
103-
};
104-
10547
const MobilityContainer = () => {
10648
useScrollToTop();
10749

website/src/views/mobility/ServiceDetails.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import classNames from 'classnames';
44
import ButtonGroupSelector from 'views/components/ButtonGroupSelector';
55
import { useState } from 'react';
66
import { ChevronDown, ChevronUp } from 'react-feather';
7+
import { isWithinBlock } from 'utils/mobility';
78
import isbServicesJSON from '../../data/isb-services.json';
89
import isbStopsJSON from '../../data/isb-stops.json';
910

1011
import styles from './ServiceDetails.scss';
11-
import { isWithinBlock } from './MobilityContainer/MobilityContainer';
1212

1313
const isbServices = isbServicesJSON;
1414
const isbStops = isbStopsJSON;

0 commit comments

Comments
 (0)