@@ -5,6 +5,8 @@ import { Fragment, useEffect, useMemo, useState } from 'react';
5
5
import classNames from 'classnames' ;
6
6
import { ChevronDown , ChevronRight , ChevronUp , ExternalLink , MoreVertical } from 'react-feather' ;
7
7
import { current } from 'immer' ;
8
+ import { getDepartAndArriveTiming , getShownArrivalTime } from 'utils/mobility' ;
9
+ import { getStopTimings } from 'apis/nextbus-new' ;
8
10
import isbServicesJSON from '../../data/isb-services.json' ;
9
11
import isbStopsJSON from '../../data/isb-stops.json' ;
10
12
import publicBusJSON from '../../data/public-bus.json' ;
@@ -15,61 +17,11 @@ const isbServices = isbServicesJSON as ISBService[];
15
17
const isbStops = isbStopsJSON as ISBStop [ ] ;
16
18
const publicBus = publicBusJSON as Record < number , string > ;
17
19
18
- const baseURL = 'https://nusmods.com' ; // TODO: wait until we have an api proxy
19
-
20
20
type Props = {
21
21
stop : string ;
22
22
setSelectedService : ( service : ISBService ) => void ;
23
23
} ;
24
24
25
- const getArrivalTime = ( eta : number ) => {
26
- const date = new Date ( ) ;
27
- date . setSeconds ( date . getSeconds ( ) + eta ) ;
28
- return date ;
29
- } ;
30
-
31
- const getShownArrivalTime = ( eta : number , forceTime = false ) => {
32
- const date = getArrivalTime ( eta ) ;
33
- if ( ! forceTime && eta < 60 * 60 ) {
34
- if ( eta < 60 ) return 'Arriving' ;
35
- return `${ Math . floor ( eta / 60 ) } mins` ;
36
- }
37
- return date . toLocaleTimeString ( [ ] , {
38
- hour : '2-digit' ,
39
- minute : '2-digit' ,
40
- hour12 : false ,
41
- // time in SGT
42
- timeZone : 'Asia/Singapore' ,
43
- } ) ;
44
- } ;
45
-
46
- const getStopTimings = async ( stop : string , setState : ( state : ShuttleServiceResult ) => void ) => {
47
- if ( ! stop ) return ;
48
- const API_AUTH = '' ; // TODO: wait until we have an api proxy
49
- try {
50
- const response = await fetch ( `${ baseURL } /ShuttleService?busstopname=${ stop } ` , {
51
- headers : {
52
- authorization : API_AUTH ,
53
- accept : 'application/json' ,
54
- } ,
55
- } ) ;
56
- const data = await response . json ( ) ;
57
- // console.log(data);
58
- setState ( data . ShuttleServiceResult ) ;
59
- } catch ( e ) {
60
- console . error ( e ) ;
61
- }
62
- } ;
63
-
64
- const getDepartAndArriveTiming = ( timings = [ ] as NUSShuttle [ ] , isEnd : boolean ) => {
65
- if ( isEnd ) {
66
- const departTiming = timings . find ( ( t ) => t . busstopcode . endsWith ( '-S' ) ) ;
67
- const arriveTiming = timings . find ( ( t ) => t . busstopcode . endsWith ( '-E' ) ) || timings [ 0 ] ;
68
- return { departTiming, arriveTiming } ;
69
- }
70
- return { departTiming : timings [ 0 ] , arriveTiming : undefined } ;
71
- } ;
72
-
73
25
function ServiceStop ( props : { stop : ISBStop ; className ?: string } ) {
74
26
const { stop } = props ;
75
27
return (
@@ -418,7 +370,9 @@ function StopDetails(props: Props) {
418
370
const serviceDetail = isbServices . find ( ( s ) => s . id === shuttle . name . toLocaleLowerCase ( ) ) ;
419
371
if ( ! serviceDetail ) return ;
420
372
const isEnd = stopDetails . name === serviceDetail . stops [ serviceDetail . stops . length - 1 ] ;
421
- const serviceShuttles = selectedStopTiming ?. shuttles . filter ( ( s ) => s . name === shuttle . name ) ;
373
+ const serviceShuttles = selectedStopTiming ?. shuttles . filter (
374
+ ( s ) => s . name === shuttle . name ,
375
+ ) as NUSShuttle [ ] ;
422
376
const timings = getDepartAndArriveTiming ( serviceShuttles , isEnd ) ;
423
377
const timing = timings . departTiming ;
424
378
@@ -433,15 +387,12 @@ function StopDetails(props: Props) {
433
387
incomingBuses . sort ( ( a , b ) => a . arrivingInSeconds - b . arrivingInSeconds ) ;
434
388
incomingBuses . splice ( 4 ) ;
435
389
const incomingBusGroups = incomingBuses . reduce ( ( acc , bus ) => {
436
- // above but avoid param-reassign error
437
390
const shownTime = getShownArrivalTime ( bus . arrivingInSeconds ) ;
438
- // console.log('bus', bus, shownTime, bus.service.name);
439
391
const newAcc = { ...acc } ;
440
392
if ( ! newAcc [ shownTime ] ) newAcc [ shownTime ] = [ ] ;
441
393
newAcc [ shownTime ] . push ( bus ) ;
442
394
return newAcc ;
443
395
} , { } as Record < string , typeof incomingBuses > ) ;
444
- // console.log('IBG', incomingBusGroups);
445
396
446
397
const publicShuttles = shuttles
447
398
. filter ( ( shuttle ) => shuttle . name . startsWith ( 'PUB:' ) )
@@ -497,7 +448,9 @@ function StopDetails(props: Props) {
497
448
498
449
{ nusShuttles . map ( ( shuttle ) => {
499
450
const service = isbServices . find ( ( s ) => s . id === shuttle . name . toLocaleLowerCase ( ) ) ;
500
- const timings = selectedStopTiming ?. shuttles . filter ( ( s ) => s . name === shuttle . name ) ;
451
+ const timings = selectedStopTiming ?. shuttles . filter (
452
+ ( s ) => s . name === shuttle . name ,
453
+ ) as NUSShuttle [ ] ;
501
454
if ( ! service ) return < Fragment key = { shuttle . name } /> ;
502
455
return (
503
456
< StopServiceDetails
0 commit comments