/** * * TableRow * */ import React from 'react'; import PropTypes from 'prop-types'; import { isEmpty } from 'lodash'; import styles from './styles.scss'; class TableRow extends React.Component { constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); } /** * Return a formatted value according to the * data type and value stored in database * * @param type {String} Data type * @param value {*} Value stored in database * @returns {*} */ getDisplayedValue(type, value) { switch (type) { case 'string': return !isEmpty(value) ? value.toString() : '-'; case 'integer': return !isEmpty(value) ? value.toString() : '-'; case 'boolean': return !isEmpty(value) ? value.toString() : '-'; default: return '-'; } } // Redirect to the edit page handleClick() { this.context.router.history.push(this.props.destination); } render() { // Generate cells const cells = this.props.headers.map((header, i) => ( {this.getDisplayedValue( header.type, this.props.record[header.name] )} )); // Add actions cell. cells.push( ); return ( this.handleClick(this.props.destination)}> {cells} ); } } TableRow.contextTypes = { router: PropTypes.object.isRequired, }; TableRow.propTypes = { destination: PropTypes.string.isRequired, handleDelete: PropTypes.func, headers: PropTypes.array.isRequired, record: PropTypes.object.isRequired, }; TableRow.defaultProps = { handleDelete: () => {}, }; export default TableRow;