8 changed files with 202 additions and 83 deletions
@ -0,0 +1,134 @@ |
|||||||
|
import React, { Component } from 'react' |
||||||
|
import styled from 'styled-components' |
||||||
|
import { connect } from 'react-redux' |
||||||
|
import { Redirect, Link } from 'react-router-dom' |
||||||
|
|
||||||
|
import { |
||||||
|
PaymentMethodChoices, TransferChoices |
||||||
|
} from '../constants/choices' |
||||||
|
import { getItemFromList } from '../utils' |
||||||
|
import { getFareAttr, getFareRule } from '../actions/fare' |
||||||
|
import store from '../store' |
||||||
|
import { FareRulesOne } from './FareRulesDetail' |
||||||
|
|
||||||
|
const StyledFareAttributesDetail = styled.div` |
||||||
|
padding: 1rem; |
||||||
|
background: #fafafa; |
||||||
|
` |
||||||
|
|
||||||
|
const FakeRow = styled.nav` |
||||||
|
padding-top: 5px; |
||||||
|
padding-bottom: 5px; |
||||||
|
background: white; |
||||||
|
margin-bottom: 1rem; |
||||||
|
` |
||||||
|
|
||||||
|
export const FareAttributesOne = (props) => { |
||||||
|
const { item, edit } = props |
||||||
|
const payTxt = getItemFromList(item.payment_method, PaymentMethodChoices, '') |
||||||
|
const transferTxt = getItemFromList(item.transfer, TransferChoices, '') |
||||||
|
|
||||||
|
return ( |
||||||
|
<FakeRow className="level panel" key={item.fare_id}> |
||||||
|
<div className="level-item has-text-centered"> |
||||||
|
<div> |
||||||
|
<p className="heading">Fare ID</p> |
||||||
|
<Link to={`/fare/attributes/${item.id}${edit ? '/edit' : ''}`}>{item.fare_id}</Link> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
<div className="level-item has-text-centered"> |
||||||
|
<div> |
||||||
|
<p className="heading">Price</p> |
||||||
|
<p key={item.id}>{item.price} <small>{item.currency_type}</small> |
||||||
|
<br /> |
||||||
|
{payTxt && payTxt.label} |
||||||
|
</p> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
<div className="level-item has-text-centered"> |
||||||
|
<div> |
||||||
|
<p className="heading">Transfer / sec</p> |
||||||
|
<p>{transferTxt && transferTxt.label} / {item.transfer_duration}</p> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
<div className="level-item has-text-centered"> |
||||||
|
<div> |
||||||
|
<p className="heading">Agency</p> |
||||||
|
<Link to={`/agency/${item.agency.agency_id}`} >{item.agency.name}</Link> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</FakeRow> |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
class FareAttributesDetail extends Component { |
||||||
|
state = { |
||||||
|
fetching: true |
||||||
|
} |
||||||
|
|
||||||
|
constructor() { |
||||||
|
super() |
||||||
|
this.renderItem = this.renderItem.bind(this) |
||||||
|
} |
||||||
|
|
||||||
|
componentWillMount() { |
||||||
|
const { props } = this |
||||||
|
const { attribID } = props.match.params |
||||||
|
const { results } = props.fareattr |
||||||
|
const ones = results.filter(ele => ele.id === +attribID) |
||||||
|
store.dispatch(getFareRule(`fareattr=${attribID}`)) |
||||||
|
if (ones.length > 0) { |
||||||
|
this.setState({fetching: false}) |
||||||
|
} else { |
||||||
|
store.dispatch(getFareAttr()) |
||||||
|
this.setState({fetching: true}) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
componentWillReceiveProps(nextProps) { |
||||||
|
if (this.props.fareattr.fetching && !nextProps.fareattr.fetching) { |
||||||
|
this.setState({fetching: false}) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
renderItem(one) { |
||||||
|
return ( |
||||||
|
<StyledFareAttributesDetail> |
||||||
|
<FareAttributesOne item={one} edit={true} /> |
||||||
|
</StyledFareAttributesDetail> |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
render () { |
||||||
|
const { fetching, results } = this.props.fareattr |
||||||
|
const { farerule } = this.props |
||||||
|
// redirect to view page if no data
|
||||||
|
const { attribID } = this.props.match.params |
||||||
|
const ones = results.filter(ele => ele.id === +attribID) |
||||||
|
if (this.state.fetching || fetching) |
||||||
|
return <div>Waiting...</div> |
||||||
|
|
||||||
|
if (ones.length < 1) |
||||||
|
return <Redirect to='/fare' /> |
||||||
|
|
||||||
|
return ( |
||||||
|
<div> |
||||||
|
{this.renderItem(ones[0])} |
||||||
|
{farerule.results.map(ele => <FareRulesOne item={ele} />)} |
||||||
|
</div> |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
const mapStateToProps = state => ({ |
||||||
|
fareattr: state.fareattr, |
||||||
|
farerule: state.farerule, |
||||||
|
}) |
||||||
|
|
||||||
|
const connectFareAttributesDetail = connect( |
||||||
|
mapStateToProps, |
||||||
|
)(FareAttributesDetail) |
||||||
|
export default connectFareAttributesDetail |
@ -0,0 +1,48 @@ |
|||||||
|
import React from 'react' |
||||||
|
import styled from 'styled-components' |
||||||
|
import { Link } from 'react-router-dom' |
||||||
|
|
||||||
|
const FakeRow = styled.nav` |
||||||
|
padding-top: 5px; |
||||||
|
padding-bottom: 5px; |
||||||
|
background: white; |
||||||
|
margin-bottom: 1rem; |
||||||
|
` |
||||||
|
|
||||||
|
export const FareRulesOne = (props) => { |
||||||
|
const { item } = props |
||||||
|
|
||||||
|
return ( |
||||||
|
<FakeRow className="level panel" key={item.fare_id}> |
||||||
|
<div className="level-item has-text-centered"> |
||||||
|
<div> |
||||||
|
<p className="heading">Fare ID</p> |
||||||
|
<Link to={`/fare/rules/${item.id}`}>{item.fare.fare_id}</Link> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
<div className="level-item has-text-centered"> |
||||||
|
<div> |
||||||
|
<p className="heading">Origin ID</p> |
||||||
|
<p className="title">{item.origin_id}</p> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
<div className="level-item has-text-centered"> |
||||||
|
<div> |
||||||
|
<p className="heading">Destination ID (or contains)</p> |
||||||
|
<p className="title"> |
||||||
|
{item.destination_id} |
||||||
|
{item.contains_id} |
||||||
|
</p> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
<div className="level-item has-text-centered"> |
||||||
|
<div> |
||||||
|
<p className="heading">Route ID</p> |
||||||
|
<p> |
||||||
|
{item.route && (item.route.route_id || '-')} |
||||||
|
</p> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</FakeRow> |
||||||
|
) |
||||||
|
} |
Loading…
Reference in new issue