import React from 'react'; import moment from 'moment'; import { Grid, Row, Col } from 'react-bootstrap'; import TreatPopup from './treat-popup'; import { durationDisplay } from '../../../commons/utils'; import './treat-timeline.less'; export default class TreatTimeline extends React.Component { constructor(props) { super(props); this.barClick = this.barClick.bind(this); this._popupHide = this._popupHide.bind(this); } componentWillMount() { this.setState({ ini: true, ppshow: false }); const self = this; setTimeout(() => self.setState({ ini: false }), 100); } SVG(props) { return {props.children}; } renderHeader(period) { const f = 'MMM-YYYY'; const SVG = this.SVG; return ( {period.ini.format(f)} {period.end.format(f)} {durationDisplay(period.ini, period.end)} ); } barClick(period) { const self = this; return evt => { const target = evt.target; const show = self.state.pptarget !== target; setTimeout(() => self.setState({ pptarget: show ? target : null, data: period, ppshow: show }) , 100); }; } _popupHide() { this.setState({ ppshow: false, target: null }); } calcRect(treat, period) { const ini = period.ini; const end = period.end; return { x: ini.diff(treat.ini, 'days') / treat.days * 100, y: 0, width: end.diff(ini, 'days') / treat.days * 100, height: 4 }; } renderBar(pos, period) { const color = period.color ? period.color : '#48AA8A'; // animation of the bars const props = Object.assign({}, pos); if (this.state.ini) { props.width = 0; } return ( ); } renderColumn(treat, periods) { const self = this; const comps = []; // render for each period periods.forEach((p, index) => { // calculate the position const pos = self.calcRect(treat, p); pos.onClick = self.barClick(p); pos.className = 'tm-column'; // add the bar comps.push(self.renderBar(pos, p)); // is there any custom text if (p.text) { comps.push( {p.text} ); } }); const SVG = this.SVG; return ( {comps} ); } renderPrescColumn(treat, periods, presc) { const lst = periods.map(p => ({ ini: moment(p.ini), end: moment(p.end), text: p.doseUnit + ' (' + p.frequency + '/7)', data: p, presc: presc })); return this.renderColumn(treat, lst); } render() { const treat = this.props.treatment; if (!treat) { return null; } // calculate the treatement period to make calculation easier const period = { ini: moment(treat.period.ini), end: moment(treat.period.end), days: null }; period.days = period.end.diff(period.ini, 'days'); // prescribed medicines const prescs = treat.prescriptions; const titleSize = { md: 3 }; const barSize = { md: 9 }; const units = treat.units.map(it => ({ ini: moment(it.ini), end: moment(it.end), text: it.unit.name, data: it, color: '#E67E22' // '#2980b9' })); const SVG = this.SVG; return ( {this.renderHeader(period)} { __('TbCase.healthUnits') } {this.renderColumn(period, units)} { prescs.map(it => ( { it.product.name } {this.renderPrescColumn(period, it.periods, it)} )) } ); } } TreatTimeline.propTypes = { treatment: React.PropTypes.object };