sipp11 6 years ago
parent
commit
d7d34127a1
  1. 3
      src/components/FareAttributesDetail.js
  2. 104
      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>
) )

104
src/components/FareRulesForm.js

@ -52,30 +52,41 @@ class FareRulesForm extends Component {
handleSubmit() { handleSubmit() {
const { id } = this.state const { id } = this.state
let body = {...this.state} let body = { ...this.state }
delete body.id delete body.id
if (id !== null) { if (id !== null) {
store.dispatch(updateFareRule(id, body)) store.dispatch(updateFareRule(id, body))
} else { } else {
store.dispatch(createFareRule(body)) store.dispatch(createFareRule(body))
} }
this.setState({justSubmit: true}) this.setState({ justSubmit: true })
} }
handleDelete() { handleDelete() {
const { id } = this.state const { id } = this.state
store.dispatch(deleteFareRule(id)) store.dispatch(deleteFareRule(id))
this.setState({justSubmit: true}) this.setState({ justSubmit: true })
} }
componentWillMount() { componentWillMount() {
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
const ones = results.filter(ele => ele.id === +ruleID) if (ruleID === undefined) {
if (ones.length > 0) { // this is for create form
this.setState(ones[0]) 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)
if (ones.length > 0) {
this.setState(ones[0])
}
} }
} }
@ -92,7 +103,7 @@ class FareRulesForm extends Component {
<AsyncSelect <AsyncSelect
cacheOptions={true} cacheOptions={true}
defaultOptions defaultOptions
defaultValue={one.fare && {...one.fare, label: one.fare.fare_id}} defaultValue={one.fare && { ...one.fare, label: one.fare.fare_id }}
loadOptions={getFareAttrAsyncSelect} loadOptions={getFareAttrAsyncSelect}
components={{ Option: FareAttrOption }} components={{ Option: FareAttrOption }}
onChange={(resp, evt) => { onChange={(resp, evt) => {
@ -101,11 +112,12 @@ class FareRulesForm extends Component {
target: { target: {
name: 'fare', name: 'fare',
value: resp.value, value: resp.value,
}} }
}
this.handleChange(evt) this.handleChange(evt)
} }
}} }}
/> />
</div> </div>
<div className="field"> <div className="field">
@ -113,7 +125,7 @@ class FareRulesForm extends Component {
<AsyncSelect <AsyncSelect
cacheOptions={true} cacheOptions={true}
defaultOptions defaultOptions
defaultValue={one.route && {value: one.route, label: one.route.route_id}} defaultValue={one.route && { value: one.route, label: one.route.route_id }}
loadOptions={getRouteAsyncSelect} loadOptions={getRouteAsyncSelect}
components={{ Option: RouteOption }} components={{ Option: RouteOption }}
onChange={(resp, evt) => { onChange={(resp, evt) => {
@ -122,11 +134,12 @@ class FareRulesForm extends Component {
target: { target: {
name: 'route', name: 'route',
value: resp.value, value: resp.value,
}} }
}
this.handleChange(evt) this.handleChange(evt)
} }
}} }}
/> />
</div> </div>
<div className="field"> <div className="field">
@ -134,64 +147,84 @@ class FareRulesForm extends Component {
<AsyncSelect <AsyncSelect
cacheOptions={true} cacheOptions={true}
defaultOptions defaultOptions
defaultValue={one.origin_id && {value: one.origin_id, label: one.origin_id}} defaultValue={one.origin_id && { value: one.origin_id, label: one.origin_id }}
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>
<div className="field"> <div className="field">
<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>
<div className="field"> <div className="field">
<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>
<p>When having multiple destinations or contains, multiple fare rule records will be created automatically. Nothing differs from adding multiple records manually, it only saves time. !! only works with CREATE, UPDATE will throw 500</p> <p>When having multiple destinations or contains, multiple fare rule records will be created automatically. Nothing differs from adding multiple records manually, it only saves time. !! only works with CREATE, UPDATE will throw 500</p>
@ -218,20 +251,20 @@ class FareRulesForm extends Component {
) )
} }
render () { render() {
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