108 lines
2.7 KiB
JavaScript
Raw Normal View History

/**
*
* TableRow
*
*/
import React from 'react';
import PropTypes from 'prop-types';
2017-10-10 15:58:35 +02:00
import moment from 'moment';
import { isEmpty, isObject } 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':
2017-09-27 15:36:27 +02:00
case 'text':
return value && !isEmpty(value.toString()) ? value.toString() : '-';
2017-09-27 15:36:27 +02:00
case 'float':
2017-04-11 18:02:55 +02:00
case 'integer':
2017-09-27 15:36:27 +02:00
case 'biginteger':
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-10-10 15:58:35 +02:00
case 'date':
case 'time':
case 'datetime':
case 'timestamp':
return value && isObject(value) && value._isAMomentObject === true ?
this.props.value.format('YYYY-MM-DD HH:mm:ss') :
moment(this.props.value).format('YYYY-MM-DD HH:mm:ss');
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}>
2017-10-12 15:24:35 +02:00
<div className={styles.truncate}>
<div className={styles.truncated}>
{this.getDisplayedValue(
header.type,
this.props.record[header.name]
)}
</div>
</div>
2017-08-30 17:56:52 +02:00
</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,
2017-10-10 15:58:35 +02:00
value: PropTypes.shape({
format: PropTypes.func,
}),
};
TableRow.defaultProps = {
handleDelete: () => {},
};
export default TableRow;