2018-02-07 18:06:07 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2016-08-15 21:06:12 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import moment from 'moment';
|
2016-11-10 21:13:00 +00:00
|
|
|
import { Link, browserHistory } from 'react-router';
|
2017-02-22 19:29:35 +00:00
|
|
|
import InlineSVG from 'react-inlinesvg';
|
2016-08-15 21:06:12 +00:00
|
|
|
import * as SketchActions from '../actions/projects';
|
|
|
|
import * as ProjectActions from '../actions/project';
|
2017-01-06 18:08:03 +00:00
|
|
|
import * as ToastActions from '../actions/toast';
|
2017-02-22 19:29:35 +00:00
|
|
|
|
2016-10-12 18:24:53 +00:00
|
|
|
const trashCan = require('../../../images/trash-can.svg');
|
2016-08-15 21:06:12 +00:00
|
|
|
|
|
|
|
class SketchList extends React.Component {
|
2016-11-10 21:13:00 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-02-17 20:47:54 +00:00
|
|
|
this.props.getProjects(this.props.username);
|
2016-11-10 21:13:00 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 21:06:12 +00:00
|
|
|
render() {
|
2016-12-01 22:12:34 +00:00
|
|
|
const username = this.props.username !== undefined ? this.props.username : this.props.user.username;
|
2016-08-15 21:06:12 +00:00
|
|
|
return (
|
2017-07-11 15:37:43 +00:00
|
|
|
<div className="sketches-table-container">
|
2017-07-24 15:12:11 +00:00
|
|
|
{ this.props.sketches.length === 0 &&
|
|
|
|
<p className="sketches-table__empty">No sketches.</p>
|
|
|
|
}
|
|
|
|
{ this.props.sketches.length > 0 &&
|
|
|
|
<table className="sketches-table" summary="table containing all saved projects">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th className="sketch-list__trash-column" scope="col"></th>
|
|
|
|
<th scope="col">Sketch</th>
|
|
|
|
<th scope="col">Date created</th>
|
|
|
|
<th scope="col">Date updated</th>
|
2016-08-15 21:06:12 +00:00
|
|
|
</tr>
|
2017-07-24 15:12:11 +00:00
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{this.props.sketches.map(sketch =>
|
|
|
|
// eslint-disable-next-line
|
|
|
|
<tr
|
|
|
|
className="sketches-table__row visibility-toggle"
|
|
|
|
key={sketch.id}
|
|
|
|
onClick={() => browserHistory.push(`/${username}/sketches/${sketch.id}`)}
|
|
|
|
>
|
|
|
|
<td className="sketch-list__trash-column">
|
|
|
|
{(() => { // eslint-disable-line
|
|
|
|
if (this.props.username === this.props.user.username || this.props.username === undefined) {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className="sketch-list__trash-button"
|
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
if (window.confirm(`Are you sure you want to delete "${sketch.name}"?`)) {
|
|
|
|
this.props.deleteProject(sketch.id);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<InlineSVG src={trashCan} alt="Delete Project" />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})()}
|
|
|
|
</td>
|
|
|
|
<th scope="row"><Link to={`/${username}/sketches/${sketch.id}`}>{sketch.name}</Link></th>
|
|
|
|
<td>{moment(sketch.createdAt).format('MMM D, YYYY h:mm A')}</td>
|
|
|
|
<td>{moment(sketch.updatedAt).format('MMM D, YYYY h:mm A')}</td>
|
|
|
|
</tr>
|
|
|
|
)}
|
|
|
|
</tbody>
|
|
|
|
</table>}
|
2017-07-11 15:37:43 +00:00
|
|
|
</div>
|
2016-08-15 21:06:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SketchList.propTypes = {
|
2017-02-22 19:29:35 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
username: PropTypes.string
|
|
|
|
}).isRequired,
|
2016-08-15 21:06:12 +00:00
|
|
|
getProjects: PropTypes.func.isRequired,
|
2017-02-22 19:29:35 +00:00
|
|
|
sketches: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
createdAt: PropTypes.string.isRequired,
|
|
|
|
updatedAt: PropTypes.string.isRequired
|
|
|
|
})).isRequired,
|
2016-10-12 18:24:53 +00:00
|
|
|
username: PropTypes.string,
|
2017-07-11 15:37:43 +00:00
|
|
|
deleteProject: PropTypes.func.isRequired
|
2017-02-22 19:29:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SketchList.defaultProps = {
|
|
|
|
username: undefined
|
2016-08-15 21:06:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
|
|
|
return {
|
|
|
|
user: state.user,
|
|
|
|
sketches: state.sketches
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
2017-01-06 18:08:03 +00:00
|
|
|
return bindActionCreators(Object.assign({}, SketchActions, ProjectActions, ToastActions), dispatch);
|
2016-08-15 21:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(SketchList);
|