@@ -5,35 +5,41 @@ import React, { useState } from 'react'
55import Icon from '@conveyal/woonerf/components/icon'
66import { Button } from 'react-bootstrap'
77
8- import type { GtfsStop } from '../../types'
8+ import type { GtfsArea , GtfsStop } from '../../types'
99
1010import VirtualizedEntitySelect from './VirtualizedEntitySelect'
1111
1212type Props = {
13+ areas : Array < GtfsArea > ,
1314 currentValue : Array < string > | string ,
1415 processFieldChange : ( val : any ) => void ,
1516 stops : Array < GtfsStop >
1617}
1718
18- const StopAreasSelector = ( { currentValue, processFieldChange, stops } : Props ) => {
19+ const StopAreasSelector = ( { currentValue, areas , processFieldChange, stops } : Props ) => {
1920 const [ dropdownShowing , setDropdownShowing ] = useState ( false )
20- const getStationOrLocationName =
21- ( haltId ) => {
21+ const getStopOrAreaName =
22+ ( entityId ) => {
2223 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 } )`
24+ stops . find ( ( stop ) => stop . stop_id === entityId ) ||
25+ areas . find ( ( area ) => area . area_id === entityId )
26+ // const name = entity && (entity.stop_name || entity.area_name) ? (entity.stop_name || entity.area_name) : ''
27+ const codeOrId = entity && entity . stop_code ? entity . stop_code : entityId
28+ return `todo?! (${ codeOrId } )`
2729 }
2830
29- const deleteHalt = ( haltId ) => {
31+ const deleteEntity = ( entityId ) => {
32+ console . log ( 'deleting entity' , entityId )
3033 if ( typeof currentValue === 'string' ) return
3134
32- processFieldChange (
33- currentValue . filter ( id => id !== haltId )
34- )
35+ // Filter out the entityId and join remaining items with ';'
36+ const filtered = currentValue . filter ( id => id !== entityId )
37+ const newValue = filtered . length > 1 ? filtered . join ( ';' ) : ( filtered [ 0 ] || '' )
38+ processFieldChange ( newValue )
3539 }
3640
41+ console . log ( 'currentValue:::::::' , currentValue )
42+
3743 return (
3844 < >
3945 < div style = { {
@@ -45,37 +51,37 @@ const StopAreasSelector = ({ currentValue, processFieldChange, stops }: Props) =
4551 { currentValue && typeof currentValue !== 'string' &&
4652 currentValue . map ( ( l ) => (
4753 < 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 ) } >
54+ < span > { getStopOrAreaName ( l ) } </ span >
55+ < Button bsSize = 'small' bsStyle = 'danger' style = { { padding : '0 2px' , margin : '0 1ch' } } onClick = { ( ) => deleteEntity ( l ) } >
5056 < Icon type = 'trash' />
5157 </ Button >
5258 </ React . Fragment >
5359 ) ) }
5460 </ div >
55- { ! dropdownShowing && ! ( stops . length === 0 ) && < Button
61+ { ! dropdownShowing && ! ( areas . length === 0 ) && < Button
5662 block
5763 bsSize = 'small'
5864 onClick = { ( ) => setDropdownShowing ( true ) }
5965 >
60- < Icon type = 'plus' /> Add stop or location by name
66+ < Icon type = 'plus' /> Add stop or area by name
6167 </ Button > }
6268 { dropdownShowing && < VirtualizedEntitySelect
63- component = { 'stop or location ' }
64- entityKey = { 'stop_or_location_id ' }
69+ component = { 'stop or area ' }
70+ entityKey = { 'stop_or_area_id ' }
6571 entities = { [
66- ...stops
72+ ...areas
6773 // $FlowFixMe Flow struggles with union types
68- ] . filter ( ( stopOrLocation : GtfsStop ) => {
74+ ] . filter ( ( stopOrArea : GtfsStop | GtfsArea ) => {
6975 return ! currentValue || ! currentValue . includes (
7076 // $FlowFixMe making this flow compatible would introduce a lot of unneeded code
71- stopOrLocation . stop_id || stopOrLocation . location_id
77+ stopOrArea . stop_id || stopOrArea . area_id
7278 )
7379 } ) }
7480 onChange = { ( change ) => {
75- processFieldChange ( [
76- ... ( currentValue || [ ] ) ,
77- change . entity . stop_id || change . entity . location_id
78- ] )
81+ const selectedId = change . entity . stop_id || change . entity . area_id
82+ const valuesArray = Array . isArray ( currentValue ) ? currentValue : ( currentValue ? [ currentValue ] : [ ] )
83+ const newValue = [ ... valuesArray , selectedId ] . filter ( Boolean ) . join ( '§' )
84+ processFieldChange ( newValue )
7985 setDropdownShowing ( false )
8086 } }
8187 /> }
0 commit comments