sipp11 6 years ago
parent
commit
d7d34127a1
  1. 3
      src/components/FareAttributesDetail.js
  2. 70
      src/components/FareRulesForm.js

3
src/components/FareAttributesDetail.js

@ -115,6 +115,9 @@ class FareAttributesDetail extends Component {
return ( return (
<div> <div>
{this.renderItem(ones[0])} {this.renderItem(ones[0])}
<Link className="link is-info" to={`/fare/rules/new?fareattr=${ones[0].id}`}>
<i className="fas fa-plus" /> New fare rule
</Link>
{farerule.results.map(ele => <FareRulesOne item={ele} />)} {farerule.results.map(ele => <FareRulesOne item={ele} />)}
</div> </div>
) )

70
src/components/FareRulesForm.js

@ -72,12 +72,23 @@ class FareRulesForm extends Component {
const { props } = this const { props } = this
const { ruleID } = props.match.params const { ruleID } = props.match.params
const { results } = props.farerule const { results } = props.farerule
const { search } = props.location
if (ruleID === undefined) {
// this is for create form
if (search.indexOf("?fareattr=") > -1) {
const fa = search.split("=")
const fat = props.fareattrItems.filter(ele => ele.id === +fa[1])
if (fat.length === 1)
this.setState({ fare: fat[0] })
}
} else {
// get state ready for edit form
const ones = results.filter(ele => ele.id === +ruleID) const ones = results.filter(ele => ele.id === +ruleID)
if (ones.length > 0) { if (ones.length > 0) {
this.setState(ones[0]) this.setState(ones[0])
} }
} }
}
renderForm() { renderForm() {
const one = this.state const one = this.state
@ -101,7 +112,8 @@ class FareRulesForm extends Component {
target: { target: {
name: 'fare', name: 'fare',
value: resp.value, value: resp.value,
}} }
}
this.handleChange(evt) this.handleChange(evt)
} }
}} }}
@ -122,7 +134,8 @@ class FareRulesForm extends Component {
target: { target: {
name: 'route', name: 'route',
value: resp.value, value: resp.value,
}} }
}
this.handleChange(evt) this.handleChange(evt)
} }
}} }}
@ -138,14 +151,20 @@ class FareRulesForm extends Component {
loadOptions={getStopsAsyncSelect} loadOptions={getStopsAsyncSelect}
components={{ Option: StopOption }} components={{ Option: StopOption }}
onChange={(resp, evt) => { onChange={(resp, evt) => {
let result
const fieldName = 'origin_id'
if (evt.action === 'select-option') { if (evt.action === 'select-option') {
let evt = { result = {
target: { target: {
name: 'origin_id', name: fieldName,
value: resp.value.stop_id, value: resp.value.stop_id,
}}
this.handleChange(evt)
} }
}
} else if (evt.action === 'clear') {
result = { target: { name: fieldName, value: '' } }
}
if (result !== undefined)
this.handleChange(result)
}} }}
/> />
</div> </div>
@ -154,20 +173,27 @@ class FareRulesForm extends Component {
<label className="label">Destination ID (or IDs)</label> <label className="label">Destination ID (or IDs)</label>
<AsyncSelect <AsyncSelect
isMulti isMulti
isClearable
cacheOptions cacheOptions
defaultOptions defaultOptions
defaultValue={one.destination_id && { value: one.destination_id, label: one.destination_id }} defaultValue={one.destination_id && { value: one.destination_id, label: one.destination_id }}
loadOptions={getStopsAsyncSelect} loadOptions={getStopsAsyncSelect}
components={{ Option: StopOption }} components={{ Option: StopOption }}
onChange={(resp, evt) => { onChange={(resp, evt) => {
let result
const fieldName = 'destination_id'
if (evt.action === 'select-option') { if (evt.action === 'select-option') {
let evt = { result = {
target: { target: {
name: 'destination_id', name: fieldName,
value: resp.map(el => el.value.stop_id), value: resp.map(el => el.value.stop_id),
}}
this.handleChange(evt)
} }
}
} else if (evt.action === 'clear') {
result = { target: { name: fieldName, value: '' } }
}
if (result !== undefined)
this.handleChange(result)
}} }}
/> />
</div> </div>
@ -176,20 +202,27 @@ class FareRulesForm extends Component {
<label className="label">Contains ID (or IDs)</label> <label className="label">Contains ID (or IDs)</label>
<AsyncSelect <AsyncSelect
isMulti isMulti
isClearable
cacheOptions={true} cacheOptions={true}
defaultOptions defaultOptions
defaultValue={one.contains_id && { value: one.contains_id, label: one.contains_id }} defaultValue={one.contains_id && { value: one.contains_id, label: one.contains_id }}
loadOptions={getStopsAsyncSelect} loadOptions={getStopsAsyncSelect}
components={{ Option: StopOption }} components={{ Option: StopOption }}
onChange={(resp, evt) => { onChange={(resp, evt) => {
let result
const fieldName = 'contains_id'
if (evt.action === 'select-option') { if (evt.action === 'select-option') {
let evt = { result = {
target: { target: {
name: 'contains_id', name: fieldName,
value: resp.map(el => el.value.stop_id), value: resp.map(el => el.value.stop_id),
}}
this.handleChange(evt)
} }
}
} else if (evt.action === 'clear') {
result = { target: { name: fieldName, value: '' } }
}
if (result !== undefined)
this.handleChange(result)
}} }}
/> />
</div> </div>
@ -222,16 +255,16 @@ class FareRulesForm extends Component {
const one = this.state const one = this.state
const { fetching } = this.props.farerule const { fetching } = this.props.farerule
// redirect to view page if no data // redirect to view page if no data
const { attribID } = this.props.match.params const { ruleID } = this.props.match.params
// this is a create form // this is a create form
if (attribID === undefined) { if (ruleID === undefined) {
if (one.justSubmit === true && !fetching) { if (one.justSubmit === true && !fetching) {
return <Redirect to={`/fare`} /> return <Redirect to={`/fare`} />
} }
return this.renderForm() return this.renderForm()
} }
if (one.id === null && attribID.length > 0) if (one.id === null && ruleID.length > 0)
return <Redirect to={`/fare`} /> return <Redirect to={`/fare`} />
// redirect to fare list if submitted // redirect to fare list if submitted
@ -246,6 +279,7 @@ class FareRulesForm extends Component {
const mapStateToProps = state => ({ const mapStateToProps = state => ({
farerule: state.farerule, farerule: state.farerule,
fareattrItems: state.fareattr.results,
}) })
const connectFareRulesForm = connect( const connectFareRulesForm = connect(

Loading…
Cancel
Save