2016-08-15 23:06:12 +02:00
|
|
|
import React, { PropTypes } from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import moment from 'moment';
|
2016-11-10 22:13:00 +01:00
|
|
|
import { Link, browserHistory } from 'react-router';
|
2016-08-15 23:06:12 +02:00
|
|
|
import * as SketchActions from '../actions/projects';
|
|
|
|
import * as ProjectActions from '../actions/project';
|
|
|
|
import InlineSVG from 'react-inlinesvg';
|
|
|
|
const exitUrl = require('../../../images/exit.svg');
|
2016-10-12 20:24:53 +02:00
|
|
|
const trashCan = require('../../../images/trash-can.svg');
|
2016-08-15 23:06:12 +02:00
|
|
|
|
|
|
|
class SketchList extends React.Component {
|
2016-11-10 22:13:00 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.closeSketchList = this.closeSketchList.bind(this);
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:06:12 +02:00
|
|
|
componentDidMount() {
|
2016-08-17 21:53:25 +02:00
|
|
|
this.props.getProjects(this.props.username);
|
2016-08-24 19:30:50 +02:00
|
|
|
document.getElementById('sketchlist').focus();
|
2016-08-15 23:06:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-10 22:13:00 +01:00
|
|
|
closeSketchList() {
|
|
|
|
browserHistory.push(this.props.previousPath);
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:06:12 +02:00
|
|
|
render() {
|
2016-12-01 23:12:34 +01:00
|
|
|
const username = this.props.username !== undefined ? this.props.username : this.props.user.username;
|
2016-08-15 23:06:12 +02:00
|
|
|
return (
|
2016-08-24 19:30:50 +02:00
|
|
|
<section className="sketch-list" aria-label="project list" tabIndex="0" role="main" id="sketchlist">
|
2016-08-15 23:06:12 +02:00
|
|
|
<header className="sketch-list__header">
|
2016-12-19 23:49:35 +01:00
|
|
|
<h2 className="sketch-list__header-title">Open a Sketch</h2>
|
2016-11-10 22:13:00 +01:00
|
|
|
<button className="sketch-list__exit-button" onClick={this.closeSketchList}>
|
2016-08-15 23:06:12 +02:00
|
|
|
<InlineSVG src={exitUrl} alt="Close Sketch List Overlay" />
|
|
|
|
</button>
|
|
|
|
</header>
|
|
|
|
<div className="sketches-table-container">
|
|
|
|
<table className="sketches-table" summary="table containing all saved projects">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
2016-10-12 20:24:53 +02:00
|
|
|
<th className="sketch-list__trash-column" scope="col"></th>
|
2016-12-14 16:58:43 +01:00
|
|
|
<th scope="col">Sketch</th>
|
|
|
|
<th scope="col">Date created</th>
|
|
|
|
<th scope="col">Date updated</th>
|
2016-08-15 23:06:12 +02:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{this.props.sketches.map(sketch =>
|
2016-10-12 20:24:53 +02:00
|
|
|
<tr className="sketches-table__row visibility-toggle" key={sketch.id}>
|
|
|
|
<td>
|
2016-11-02 22:28:06 +01:00
|
|
|
{(() => { // eslint-disable-line
|
|
|
|
if (this.props.username === this.props.user.username || this.props.username === undefined) {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className="sketch-list__trash-button"
|
|
|
|
onClick={() => {
|
|
|
|
if (window.confirm(`Are you sure you want to delete "${sketch.name}"?`)) {
|
|
|
|
this.props.deleteProject(sketch.id);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<InlineSVG src={trashCan} alt="Delete Project" />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})()}
|
2016-10-12 20:24:53 +02:00
|
|
|
</td>
|
2016-12-01 23:12:34 +01:00
|
|
|
<td scope="row"><Link to={`/${username}/sketches/${sketch._id}`}>{sketch.name}</Link></td>
|
2016-12-14 16:58:43 +01:00
|
|
|
<td>{moment(sketch.createdAt).format('MMM D, YYYY h:mm A')}</td>
|
|
|
|
<td>{moment(sketch.updatedAt).format('MMM D, YYYY h:mm A')}</td>
|
2016-08-15 23:06:12 +02:00
|
|
|
</tr>
|
|
|
|
)}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
2016-08-24 19:30:50 +02:00
|
|
|
</section>
|
2016-08-15 23:06:12 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SketchList.propTypes = {
|
|
|
|
user: PropTypes.object.isRequired,
|
|
|
|
getProjects: PropTypes.func.isRequired,
|
|
|
|
sketches: PropTypes.array.isRequired,
|
2016-10-12 20:24:53 +02:00
|
|
|
username: PropTypes.string,
|
2016-11-10 22:13:00 +01:00
|
|
|
deleteProject: PropTypes.func.isRequired,
|
|
|
|
previousPath: PropTypes.string.isRequired
|
2016-08-15 23:06:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
|
|
|
return {
|
|
|
|
user: state.user,
|
|
|
|
sketches: state.sketches
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators(Object.assign({}, SketchActions, ProjectActions), dispatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(SketchList);
|