89 lines
2.1 KiB
JavaScript
Raw Normal View History

/**
*
* TableRow
*
*/
import PropTypes from 'prop-types';
import { isEmpty } from 'lodash';
2017-04-11 11:34:59 +02:00
import styles from './styles.scss';
2017-05-11 10:54:44 +02:00
class TableRow extends React.Component {
2017-04-11 15:38:15 +02:00
constructor(props) {
super(props);
2017-08-30 17:56:52 +02:00
this.handleClick = this.handleClick.bind(this);
2017-04-11 15:38:15 +02:00
}
2017-04-11 18:02:55 +02:00
/**
* 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.toLowerCase()) {
2017-04-11 18:02:55 +02:00
case 'string':
return value && !isEmpty(value.toString()) ? value.toString() : '-';
2017-04-11 18:02:55 +02:00
case 'integer':
return value && !isEmpty(value.toString()) ? value.toString() : '-';
2017-06-20 19:19:10 +02:00
case 'boolean':
return value && !isEmpty(value.toString()) ? value.toString() : '-';
2017-04-11 18:02:55 +02:00
default:
return '-';
}
}
2017-08-30 17:56:52 +02:00
// Redirect to the edit page
handleClick() {
2017-09-20 15:21:58 +02:00
this.context.router.history.push(`${this.props.destination}${this.props.redirectUrl}`);
2017-05-11 10:54:44 +02:00
}
render() {
2017-04-11 18:02:55 +02:00
// Generate cells
2017-08-30 17:56:52 +02:00
const cells = this.props.headers.map((header, i) => (
<td key={i}>
{this.getDisplayedValue(
header.type,
this.props.record[header.name]
)}
</td>
));
2017-04-11 18:02:55 +02:00
2017-08-30 17:56:52 +02:00
// Add actions cell.
cells.push(
<td key='action' className={styles.actions}>
<i className="fa fa-pencil" aria-hidden="true"></i>
<i onClick={this.props.handleDelete} id={this.props.record.id} className="fa fa-trash" aria-hidden="true"></i>
2017-08-30 17:56:52 +02:00
</td>
);
return (
2017-08-30 17:56:52 +02:00
<tr className={styles.tableRow} onClick={() => this.handleClick(this.props.destination)}>
{cells}
</tr>
);
}
}
2017-04-11 15:38:15 +02:00
TableRow.contextTypes = {
router: PropTypes.object.isRequired,
2017-04-11 15:38:15 +02:00
};
TableRow.propTypes = {
destination: PropTypes.string.isRequired,
handleDelete: PropTypes.func,
headers: PropTypes.array.isRequired,
record: PropTypes.object.isRequired,
2017-09-20 15:21:58 +02:00
redirectUrl: PropTypes.string.isRequired,
};
TableRow.defaultProps = {
handleDelete: () => {},
};
export default TableRow;