|
| 1 | +// @flow |
| 2 | + |
| 3 | +// $FlowFixMe React hook bindings not picked up |
| 4 | +import React, { useState } from 'react' |
| 5 | +import Icon from '@conveyal/woonerf/components/icon' |
| 6 | +import { Button } from 'react-bootstrap' |
| 7 | + |
| 8 | +import type { GtfsStop } from '../../types' |
| 9 | + |
| 10 | +import VirtualizedEntitySelect from './VirtualizedEntitySelect' |
| 11 | + |
| 12 | +type Props = { |
| 13 | + currentValue: Array<string> | string, |
| 14 | + processFieldChange: (val: any) => void, |
| 15 | + stops: Array<GtfsStop> |
| 16 | +} |
| 17 | + |
| 18 | +const StopAreasSelector = ({ currentValue, processFieldChange, stops }: Props) => { |
| 19 | + const [dropdownShowing, setDropdownShowing] = useState(false) |
| 20 | + const getStationOrLocationName = |
| 21 | + (haltId) => { |
| 22 | + const entity = |
| 23 | + stops.find((stop) => stop.stop_id === haltId) |
| 24 | + const name = entity && entity.stop_name ? entity.stop_name : '' |
| 25 | + const codeOrId = entity && entity.stop_code ? entity.stop_code : haltId |
| 26 | + return `${name} (${codeOrId})` |
| 27 | + } |
| 28 | + |
| 29 | + const deleteHalt = (haltId) => { |
| 30 | + if (typeof currentValue === 'string') return |
| 31 | + |
| 32 | + processFieldChange( |
| 33 | + currentValue.filter(id => id !== haltId) |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + return ( |
| 38 | + <> |
| 39 | + <div style={{ |
| 40 | + display: 'grid', |
| 41 | + gap: 5, |
| 42 | + gridTemplateColumns: '6fr 1fr', |
| 43 | + marginBottom: 5 |
| 44 | + }}> |
| 45 | + {currentValue && typeof currentValue !== 'string' && |
| 46 | + currentValue.map((l) => ( |
| 47 | + <React.Fragment key={l}> |
| 48 | + <span>{getStationOrLocationName(l)}</span> |
| 49 | + <Button bsSize='small' bsStyle='danger' style={{padding: '0 2px', margin: '0 1ch'}} onClick={() => deleteHalt(l)}> |
| 50 | + <Icon type='trash' /> |
| 51 | + </Button> |
| 52 | + </React.Fragment> |
| 53 | + ))} |
| 54 | + </div> |
| 55 | + {!dropdownShowing && !(stops.length === 0) && <Button |
| 56 | + block |
| 57 | + bsSize='small' |
| 58 | + onClick={() => setDropdownShowing(true)} |
| 59 | + > |
| 60 | + <Icon type='plus' /> Add stop or location by name |
| 61 | + </Button>} |
| 62 | + {dropdownShowing && <VirtualizedEntitySelect |
| 63 | + component={'stop or location'} |
| 64 | + entityKey={'stop_or_location_id'} |
| 65 | + entities={[ |
| 66 | + ...stops |
| 67 | + // $FlowFixMe Flow struggles with union types |
| 68 | + ].filter((stopOrLocation: GtfsStop) => { |
| 69 | + return !currentValue || !currentValue.includes( |
| 70 | + // $FlowFixMe making this flow compatible would introduce a lot of unneeded code |
| 71 | + stopOrLocation.stop_id || stopOrLocation.location_id |
| 72 | + ) |
| 73 | + })} |
| 74 | + onChange={(change) => { |
| 75 | + processFieldChange([ |
| 76 | + ...(currentValue || []), |
| 77 | + change.entity.stop_id || change.entity.location_id |
| 78 | + ]) |
| 79 | + setDropdownShowing(false) |
| 80 | + }} |
| 81 | + />} |
| 82 | + </> |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +export default StopAreasSelector |
0 commit comments