-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathindex.tsx
More file actions
187 lines (172 loc) · 6.17 KB
/
Copy pathindex.tsx
File metadata and controls
187 lines (172 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { Alert, CircularProgress, Grid, Typography } from '@mui/material'
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import dayjs, { ISRAEL_TIMEZONE } from 'src/dayjs'
import { GlobalSearchContext } from 'src/model/globalState'
import { INPUT_SIZE } from 'src/resources/sizes'
import { Gap, getGapsAsync } from '../../api/gapsService'
import { getServiceDayRoutes } from '../../api/serviceDayRoutesService'
import { BusRoute } from '../../model/busRoute'
import { DateSelector } from '../components/DateSelector'
import { Label } from '../components/Label'
import LineNumberSelector from '../components/LineSelector'
import { NotFound } from '../components/NotFound'
import OperatorSelector from '../components/OperatorSelector'
import { PageContainer } from '../components/PageContainer'
import RouteSelector from '../components/RouteSelector'
import { Row } from '../components/Row'
import { StickyInputs } from '../components/StickyInputs'
import GapsTable from './GapsTable'
const GapsPage = () => {
const { t } = useTranslation()
const { search, setSearch } = useContext(GlobalSearchContext)
const { operatorId, lineNumber, date, routeKey } = search
const [routes, setRoutes] = useState<BusRoute[] | undefined>()
const [gaps, setGaps] = useState<Gap[]>()
const [gapsIsLoading, setGapsIsLoading] = useState(false)
const singleLineMapBaseHref = useMemo(() => {
const params = new URLSearchParams()
params.set('operatorId', search.operatorId || '')
params.set('lineNumber', search.lineNumber || '')
params.set('routeKey', search.routeKey || '')
return `/single-line-map?${params.toString()}`
}, [search.lineNumber, search.operatorId, search.routeKey])
useEffect(() => {
if (!(operatorId && routes && routeKey && date)) return
const selectedRoute = routes.find((route) => route.key === routeKey)
if (!selectedRoute) return
setGapsIsLoading(true)
const start = dayjs.tz(date, ISRAEL_TIMEZONE).startOf('day')
const end = start.add(1, 'day').add(4, 'h')
getGapsAsync(start.valueOf(), end.valueOf(), operatorId, selectedRoute.lineRef)
.then((res) =>
setGaps(
res.filter((g) => {
const t = g.plannedStartTime || g.actualStartTime
return t && !t.isBefore(start) && t.isBefore(end)
}),
),
)
.catch((err) => {
console.error('Failed to fetch gaps:', err.message)
setGaps(undefined)
})
.finally(() => setGapsIsLoading(false))
}, [operatorId, routes, routeKey, date])
useEffect(() => {
if (!operatorId || !lineNumber) {
return
}
const controller = new AbortController()
getServiceDayRoutes(dayjs.tz(date, ISRAEL_TIMEZONE), operatorId, lineNumber, controller.signal)
.then((fetchedRoutes) => {
if (search.lineNumber === lineNumber) {
setRoutes(fetchedRoutes)
}
})
.catch((err) => {
console.error('Failed to fetch routes:', err.message)
})
return () => controller.abort()
}, [operatorId, lineNumber, date, setSearch])
const handleDateChange = (time: dayjs.Dayjs | null) => {
if (!time) return
setSearch((current) => ({
...current,
date: time.format('YYYY-MM-DD'),
}))
}
const handleOperatorChange = (operatorId: string) => {
setSearch((current) => ({ ...current, operatorId }))
}
const handleLineNumberChange = (lineNumber: string) => {
setSearch((current) =>
lineNumber === current.lineNumber
? { ...current }
: { ...current, lineNumber, routeKey: null },
)
if (lineNumber !== search.lineNumber) {
setRoutes(undefined)
}
}
const handleRouteKeyChange = (routeKey?: string) => {
setSearch((current) => ({ ...current, routeKey: routeKey ?? null }))
}
const handleStartTimeClick = useCallback(
(rideTime: string) => {
setSearch((current) => ({ ...current, rideTime }))
},
[setSearch],
)
return (
<PageContainer>
<Typography className="page-title" variant="h4">
{t('gaps_page_title')}
</Typography>
<Alert severity="info" variant="outlined" icon={false}>
{t('gaps_page_description')}
</Alert>
<StickyInputs>
<Grid container spacing={2} sx={{ maxWidth: INPUT_SIZE }}>
{/* choose date */}
<Grid size={{ xs: 4 }}>
<Label text={t('choose_date')} />
</Grid>
<Grid size={{ xs: 8 }}>
<DateSelector time={dayjs.tz(date, ISRAEL_TIMEZONE)} onChange={handleDateChange} />
</Grid>
{/* choose operator */}
<Grid size={{ xs: 4 }}>
<Label text={t('choose_operator')} />
</Grid>
<Grid size={{ xs: 8 }}>
<OperatorSelector
operatorId={operatorId ?? undefined}
setOperatorId={handleOperatorChange}
/>
</Grid>
{/* choose line */}
<Grid size={{ xs: 4 }}>
<Label text={t('choose_line')} />
</Grid>
<Grid size={{ xs: 8 }}>
<LineNumberSelector
lineNumber={lineNumber ?? undefined}
setLineNumber={handleLineNumberChange}
/>
</Grid>
{/* choose routes */}
<Grid size={{ xs: 12 }}>
{routes?.length === 0 ? (
<NotFound>{t('line_not_found')}</NotFound>
) : (
<RouteSelector
routes={routes || []}
disabled={!routes}
routeKey={routeKey ?? undefined}
setRouteKey={handleRouteKeyChange}
/>
)}
</Grid>
<Grid size={{ xs: 12 }}>
{gapsIsLoading && (
<Row>
<Label text={t('loading_gaps')} />
<CircularProgress />
</Row>
)}
</Grid>
</Grid>
</StickyInputs>
{routeKey && routeKey !== '' && (
<GapsTable
loading={gapsIsLoading}
gaps={gaps}
singleLineMapBaseHref={singleLineMapBaseHref}
onStartTimeClick={handleStartTimeClick}
/>
)}
</PageContainer>
)
}
export default GapsPage