website/src/Components/GasStation.js

132 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-03-01 20:40:51 +01:00
import React from 'react';
import { Modal, Button, ListGroup } from "react-bootstrap";
import PropTypes from 'prop-types';
import { FaWaze } from "react-icons/fa";
2020-03-21 16:44:50 +01:00
import { GiPositionMarker } from "react-icons/gi";
import { gasTypes } from "../config";
2020-03-01 20:40:51 +01:00
2020-03-04 18:45:01 +01:00
import { capitalizeFirstLetter, lowerCaseString } from "../helpers";
2020-03-01 21:17:51 +01:00
2020-03-01 20:40:51 +01:00
class GasStation extends React.Component {
2020-03-03 10:10:00 +01:00
/**
* Méthode permettant de convertir un type de carburant en un nom lisible
* @param {string} gasType
* @return {mixed}
*/
renderGasType = (gasType) => {
for( let i = 0 ; i < gasTypes.length ; i +=1 ) {
if ( gasTypes[i].type === gasType) {
return gasTypes[i].name;
}
}
return (null);
}
2020-03-03 10:10:00 +01:00
/**
* Méthode permettant de générer la liste des prix
2020-03-06 15:52:33 +01:00
* @return {string}
2020-03-03 10:10:00 +01:00
*/
2020-03-01 20:40:51 +01:00
renderPrices = () => {
const {
2020-03-06 15:52:33 +01:00
gasType,
station,
2020-03-01 20:40:51 +01:00
} = this.props;
return (
<ListGroup variant="flush">
2020-03-06 15:52:33 +01:00
{station.prices ? station.prices.map(price => {
2020-03-01 20:40:51 +01:00
return (
2020-03-06 15:52:33 +01:00
<ListGroup.Item key={price._id} className={gasType === price.gasType ? "selected-gasType" : ""}>
2020-03-21 16:44:50 +01:00
{`${this.renderGasType(price.gasType)} : ${price.price.toString().replace('.', ',')}`}
</ListGroup.Item>
2020-03-01 20:40:51 +01:00
);
}) : (null)}
</ListGroup>
)
}
2020-03-06 15:52:33 +01:00
/**
* Méthode permettant de formater le titre de la modale en fonction des infos reçues
* @return {string}
*/
renderFormatTitle = () => {
const {
station,
} = this.props;
if ( station.name ) {
return station.name;
}
return `${lowerCaseString(station.address)} - ${capitalizeFirstLetter(station.city)}`;
}
2020-03-21 16:44:50 +01:00
renderAddress = () => {
const {
station,
} = this.props;
return (
<p>
<GiPositionMarker />
{lowerCaseString(station.address)}
{' - '}
{capitalizeFirstLetter(station.city)}
</p>
);
}
2020-03-01 20:40:51 +01:00
render () {
const {
showModal,
hideModal,
} = this.props;
return (
<Modal
size="xl"
aria-labelledby="contained-modal-title-vcenter"
centered
show={showModal}
onHide={hideModal}
>
<Modal.Header closeButton>
<Modal.Title>
2020-03-06 15:52:33 +01:00
{this.renderFormatTitle()}
2020-03-01 20:40:51 +01:00
</Modal.Title>
</Modal.Header>
<Modal.Body>
2020-03-21 16:44:50 +01:00
{this.renderAddress()}
2020-03-01 20:40:51 +01:00
{this.renderPrices()}
</Modal.Body>
<Modal.Footer>
<Button variant="primary" onClick={() => hideModal(true)}>
<FaWaze size="2em" />
2020-03-01 20:40:51 +01:00
</Button>
</Modal.Footer>
</Modal>
);
}
}
GasStation.defaultProps = {
2020-03-06 15:52:33 +01:00
station: {
2020-03-01 20:40:51 +01:00
address: null,
city: null,
prices: []
}
};
GasStation.propTypes = {
2020-03-06 15:52:33 +01:00
gasType: PropTypes.string.isRequired,
2020-03-01 20:40:51 +01:00
showModal: PropTypes.bool.isRequired,
hideModal: PropTypes.func.isRequired,
2020-03-06 15:52:33 +01:00
station: PropTypes.shape({
name: PropTypes.string,
2020-03-01 20:40:51 +01:00
address: PropTypes.string,
city: PropTypes.string,
prices: PropTypes.array
}),
};
export default GasStation;