Skip to content

Commit 2b0bf12

Browse files
kagiurazwliew
authored andcommitted
feat: ✨ basic implementation of mobility page (for ISBs)
1 parent b44f5e1 commit 2b0bf12

38 files changed

+12150
-28
lines changed

package-lock.json

Lines changed: 3120 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"@svgr/webpack": "^8.1.0"
4+
}
5+
}

website/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
"json2mq": "0.2.0",
147147
"leaflet": "1.9.4",
148148
"leaflet-gesture-handling": "1.2.2",
149+
"leaflet-polylineoffset": "^1.1.1",
149150
"lodash": "4.17.21",
150151
"mousetrap": "1.6.5",
151152
"nusmoderator": "3.0.0",

website/src/apis/nextbus-new.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import axios from 'axios';
2+
import { BusStop } from 'types/buses';
3+
4+
import { NextBus, NextBusTime, NextBusTimings } from 'types/venues';
5+
6+
const baseUrl = 'https://nnextbus.nus.edu.sg';
7+
8+
interface ShuttleServiceResult {
9+
caption: string;
10+
name: string;
11+
shuttles: Shuttle[];
12+
}
13+
14+
interface Shuttle {
15+
arrivalTime: string;
16+
name: string;
17+
nextArrivalTime: string;
18+
nextPassengers: string;
19+
passengers: string;
20+
}
21+
22+
function convertArrivalTime(arrivalTime: string): NextBusTime {
23+
const numericTime = +arrivalTime;
24+
if (!Number.isNaN(numericTime)) return numericTime;
25+
if (arrivalTime === 'Arr' || arrivalTime === '-') return arrivalTime;
26+
throw new Error(`Unknown arrival time ${arrivalTime}`);
27+
}
28+
29+
export function getStops(): Promise<BusStop[]> {
30+
const url = `${baseUrl}/BusStops`;
31+
return axios
32+
.get(url, {
33+
headers: {
34+
Authorization: process.env?.NEXTBUS_API_AUTH || '',
35+
},
36+
})
37+
.then((response) => response.data.BusStopsResult.busstops);
38+
}
39+
40+
export function nextBus(code: string): Promise<any> {
41+
const url = `${baseUrl}/arrival`;
42+
return axios
43+
.get<{
44+
ShuttleServiceResult: ShuttleServiceResult;
45+
}>(url, { params: { busstopname: code } })
46+
.then((response) => {
47+
const shuttles: NextBusTimings = {};
48+
49+
response.data.ShuttleServiceResult.shuttles.forEach((arrival: Shuttle) => {
50+
const timing: NextBus = {
51+
arrivalTime: convertArrivalTime(arrival.arrivalTime),
52+
nextArrivalTime: convertArrivalTime(arrival.nextArrivalTime),
53+
};
54+
55+
shuttles[arrival.name] = timing;
56+
});
57+
58+
return shuttles;
59+
});
60+
}

0 commit comments

Comments
 (0)