Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/clear input button #151

Merged
merged 17 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/pages/GapsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ const GapsPage = () => {
}, [operatorId, routeKey, timestamp])

useEffect(() => {
if (!operatorId || !lineNumber) {
if (!operatorId || operatorId === '0' || !lineNumber) {
setSearch((current) => ({
...current,
routes: undefined,
routeKey: undefined,
}))
return
}
getRoutesAsync(moment(timestamp), moment(timestamp), operatorId, lineNumber)
Expand Down Expand Up @@ -156,7 +161,7 @@ const GapsPage = () => {
)}
</Grid>
</Grid>
{!gapsIsLoading && routeKey && (
{!gapsIsLoading && routeKey && routeKey !== '0' && (
<>
<FormControlLabel
control={
Expand Down
31 changes: 17 additions & 14 deletions src/pages/GapsPatternsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ const GapsPatternsPage = () => {
}

useEffect(() => {
if (!operatorId || !lineNumber) {
if (!operatorId || operatorId === '0' || !lineNumber) {
setSearch((current) => ({ ...current, routeKey: undefined, routes: undefined }))
return
}
loadSearchData()
Expand Down Expand Up @@ -217,21 +218,23 @@ const GapsPatternsPage = () => {
(routes.length === 0 ? (
<NotFound>{TEXTS.line_not_found}</NotFound>
) : (
<RouteSelector
routes={routes}
routeKey={routeKey}
setRouteKey={(key) => setSearch((current) => ({ ...current, routeKey: key }))}
/>
<>
<RouteSelector
routes={routes}
routeKey={routeKey}
setRouteKey={(key) => setSearch((current) => ({ ...current, routeKey: key }))}
/>
<Grid xs={12}>
<GapsByHour
lineRef={routes?.find((route) => route.key === routeKey)?.lineRef || 0}
operatorRef={operatorId || ''}
fromDate={startDate}
toDate={endDate}
/>
</Grid>
</>
))}
</Grid>
<Grid xs={12}>
<GapsByHour
lineRef={routes?.find((route) => route.key === routeKey)?.lineRef || 0}
operatorRef={operatorId || ''}
fromDate={startDate}
toDate={endDate}
/>
</Grid>
</Grid>
</PageContainer>
)
Expand Down
3 changes: 2 additions & 1 deletion src/pages/SingleLineMapPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const SingleLineMapPage = () => {
}, [])

useEffect(() => {
if (!operatorId || !lineNumber) {
if (!operatorId || operatorId === '0' || !lineNumber) {
setSearch((current) => ({ ...current, routes: undefined, routeKey: undefined }))
return
}
getRoutesAsync(moment(timestamp), moment(timestamp), operatorId, lineNumber).then((routes) =>
Expand Down
19 changes: 16 additions & 3 deletions src/pages/TimelinePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,21 @@ const TimelinePage = () => {
const [hitsIsLoading, setHitsIsLoading] = useState(false)

const clearRoutes = useCallback(() => {
setState((current) => ({ ...current, routes: undefined, routeKey: undefined }))
}, [setState])
setSearch((current) => ({ ...current, routes: undefined, routeKey: undefined }))
setRoutesIsLoading(false)
}, [setSearch])

const clearStops = useCallback(() => {
setState((current) => ({
...current,
stops: undefined,
stopName: undefined,
stopKey: undefined,
gtfsHitTimes: undefined,
siriHitTimes: undefined,
}))
setStopsIsLoading(false)
setHitsIsLoading(false)
}, [setState])

useEffect(() => {
Expand All @@ -58,7 +62,7 @@ const TimelinePage = () => {

useEffect(() => {
clearStops()
if (!operatorId || !lineNumber) {
if (!operatorId || operatorId === '0' || !lineNumber) {
return
}
setRoutesIsLoading(true)
Expand All @@ -79,6 +83,9 @@ const TimelinePage = () => {

useEffect(() => {
clearStops()
if (!operatorId || operatorId === '0' || !lineNumber) {
return
}
if (!routeKey || !selectedRouteIds) {
return
}
Expand All @@ -89,6 +96,9 @@ const TimelinePage = () => {
}, [selectedRouteIds, routeKey, clearStops])

useEffect(() => {
if (!operatorId || operatorId === '0' || !lineNumber) {
return
}
if (!stopKey || !stops || !selectedRoute) {
return
}
Expand All @@ -107,6 +117,9 @@ const TimelinePage = () => {
}, [stopKey, stops, timestamp, selectedRoute])

useEffect(() => {
if (!operatorId || operatorId === '0' || !lineNumber) {
return
}
if (!stopName || !stops || stopKey) {
return
}
Expand Down
13 changes: 13 additions & 0 deletions src/pages/components/ClearButton.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.clear-indicator {
position: absolute;
left: 33px;
visibility: hidden;
height: 28px;
width: 28px;
color: #6a6b6c;
padding: 7px;
&:hover {
background-color: #e7e9eb;
border-radius: 50%;
}
}
9 changes: 9 additions & 0 deletions src/pages/components/ClearButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import { CloseOutlined as ClearIcon } from '@ant-design/icons'
import './ClearButton.scss'

const ClearButton = ({ onClearInput }: { onClearInput: () => void }) => {
return <ClearIcon onClick={onClearInput} className="clear-indicator" />
}

export default ClearButton
7 changes: 6 additions & 1 deletion src/pages/components/DateSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { DatePicker } from '@mui/x-date-pickers/DatePicker'
import { TEXTS } from 'src/resources/texts'
import { DataAndTimeSelectorProps } from './utils/dateAndTime'
import { DateValidationError } from '@mui/x-date-pickers'
import styled from 'styled-components'

const Error = styled.div`
color: 'red';
`

export function DateSelector({ time, onChange, customLabel }: DataAndTimeSelectorProps) {
const [error, setError] = React.useState<DateValidationError | null>(null)
Expand All @@ -17,7 +22,7 @@ export function DateSelector({ time, onChange, customLabel }: DataAndTimeSelecto
disableFuture
onError={(err) => setError(err)}
/>
{error && <div style={{ color: 'red' }}>{error}</div>}
{error && <Error>{error}</Error>}
</>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function FilterPositionsByStartTimeSelector({
disablePortal
value={value}
onChange={(e, value) => setStartTime(value ? value.value : '0')}
id="operator-select"
id="start-time-select"
options={options}
renderInput={(params) => <TextField {...params} label={TEXTS.choose_start_time} />}
getOptionLabel={(option) => option.label}
Expand Down
92 changes: 53 additions & 39 deletions src/pages/components/LineSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
import React, { useCallback, useLayoutEffect, useState } from 'react'
import { TEXTS } from 'src/resources/texts'
import debounce from 'lodash.debounce'
import { TextField } from '@mui/material'

type LineSelectorProps = {
lineNumber: string | undefined
setLineNumber: (lineNumber: string) => void
}

const LineSelector = ({ lineNumber, setLineNumber }: LineSelectorProps) => {
const [value, setValue] = useState<LineSelectorProps['lineNumber']>(lineNumber)
const debouncedSetLineNumber = useCallback(debounce(setLineNumber, 200), [setLineNumber])

useLayoutEffect(() => {
setValue(lineNumber)
}, [])

return (
<TextField
sx={{ width: '100%' }}
label={TEXTS.choose_line}
type="number"
value={value}
onChange={(e) => {
setValue(e.target.value)
debouncedSetLineNumber(e.target.value)
}}
InputLabelProps={{
shrink: true,
}}
InputProps={{
placeholder: TEXTS.line_placeholder,
}}
/>
)
}

export default LineSelector
import React, { useCallback, useLayoutEffect, useState } from 'react'
import { TEXTS } from 'src/resources/texts'
import debounce from 'lodash.debounce'
import { TextField } from '@mui/material'
import ClearButton from './ClearButton'
import './Selector.scss'
import classNames from 'classnames'

type LineSelectorProps = {
lineNumber: string | undefined
setLineNumber: (lineNumber: string) => void
}

const LineSelector = ({ lineNumber, setLineNumber }: LineSelectorProps) => {
const [value, setValue] = useState<LineSelectorProps['lineNumber']>(lineNumber)
const debouncedSetLineNumber = useCallback(debounce(setLineNumber, 200), [setLineNumber])

useLayoutEffect(() => {
setValue(lineNumber)
}, [])

const handleClearInput = () => {
setValue('')
setLineNumber('')
}

const textFieldClass = classNames({
'selector-line-text-field': true,
'selector-line-text-field_visible': value,
'selector-line-text-field_hidden': !value,
})
return (
<TextField
className={textFieldClass}
label={TEXTS.choose_line}
type="number"
value={value && +value < 0 ? 0 : value}
onChange={(e) => {
setValue(e.target.value)
debouncedSetLineNumber(e.target.value)
}}
InputLabelProps={{
shrink: true,
}}
InputProps={{
placeholder: TEXTS.line_placeholder,
endAdornment: <ClearButton onClearInput={handleClearInput} />,
}}
/>
)
}

export default LineSelector
64 changes: 39 additions & 25 deletions src/pages/components/MinuteSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
import React from 'react'
import { TEXTS } from 'src/resources/texts'
import { TextField } from '@mui/material'

type MinuteSelectorProps = {
num: number
setNum: (num: string) => void
}

const MinuteSelector = ({ num, setNum }: MinuteSelectorProps) => {
return (
<TextField
sx={{ width: '100%' }}
label={TEXTS.minutes}
type="number"
value={num}
onChange={(e) => setNum(e.target.value)}
InputLabelProps={{
shrink: true,
}}
/>
)
}

export default MinuteSelector
import React from 'react'
import { TEXTS } from 'src/resources/texts'
import { TextField } from '@mui/material'
import ClearButton from './ClearButton'
import './Selector.scss'
import classNames from 'classnames'

type MinuteSelectorProps = {
num: number
setNum: (num: number) => void
}

const MinuteSelector = ({ num, setNum }: MinuteSelectorProps) => {
const handleClearInput = () => {
setNum(1) // 1 minute this is the wanted default value
}
const textFieldClass = classNames({
'selector-minute-text-field': true,
'selector-minute-text-field_visible': num,
'selector-minute-text-field_hidden': !num,
})
return (
<TextField
className={textFieldClass}
label={TEXTS.minutes}
type="number"
value={num}
onChange={(e) => setNum(+e.target.value)}
InputLabelProps={{
shrink: true,
}}
InputProps={{
endAdornment: <ClearButton onClearInput={handleClearInput} />,
}}
/>
)
}

export default MinuteSelector
2 changes: 1 addition & 1 deletion src/pages/components/RouteSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const RouteSelector = ({ routes, routeKey, setRouteKey }: RouteSelectorProps) =>
disablePortal
value={value}
onChange={(e, value) => setRouteKey(value ? value.key : '0')}
id="operator-select"
id="route-select"
options={routes}
renderInput={(params) => (
<TextField {...params} label={formatted(TEXTS.choose_route, routes.length.toString())} />
Expand Down
22 changes: 22 additions & 0 deletions src/pages/components/Selector.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.selector {
&-minute,
&-line {
&-text-field {
width: 100%;
&_visible {
&:hover .clear-indicator,
& .Mui-focused .clear-indicator {
position: absolute;
visibility: visible;
}
}
&_hidden {
&:hover .clear-indicator,
& .Mui-focused .clear-indicator {
position: absolute;
visibility: hidden;
}
}
}
}
}
Loading
Loading