116 lines
2.8 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 IcoContainer from 'components/IcoContainer';
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 {*}
*/
2017-12-12 16:27:29 +01:00
getDisplayedValue(type, value, name) {
switch (type.toLowerCase()) {
2017-04-11 18:02:55 +02:00
case 'string':
2017-09-27 15:36:27 +02:00
case 'text':
case 'email':
2017-12-12 16:27:29 +01:00
return (value && !isEmpty(value.toString())) || name === 'id' ? 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':
2017-10-17 13:57:59 +02:00
case 'decimal':
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':
2017-10-13 16:54:21 +02:00
case 'timestamp': {
const date = value && isObject(value) && value._isAMomentObject === true ?
value :
moment(value);
2017-10-16 08:46:34 +02:00
return date.utc().format('YYYY-MM-DD HH:mm:ss');
2017-10-13 16:54:21 +02:00
}
case 'password':
return '••••••••';
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,
2017-12-12 16:27:29 +01:00
this.props.record[header.name],
header.name,
2017-10-12 15:24:35 +02:00
)}
</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
cells.push(
<td key='action' className={styles.actions}>
<IcoContainer icons={[{ icoType: 'pencil' }, { id: this.props.record.id, icoType: 'trash', onClick: this.props.onDelete }]} />
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,
headers: PropTypes.array.isRequired,
onDelete: PropTypes.func,
record: PropTypes.object.isRequired,
2017-09-20 15:21:58 +02:00
redirectUrl: PropTypes.string.isRequired,
};
TableRow.defaultProps = {
onDelete: () => {},
2017-10-13 09:41:48 +02:00
value: {
format: () => {},
},
};
export default TableRow;