2018-02-07 19:06:07 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2017-07-11 17:37:43 +02:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { Link } from 'react-router';
|
2018-02-23 17:31:41 +01:00
|
|
|
import { Helmet } from 'react-helmet';
|
2017-07-11 17:37:43 +02:00
|
|
|
import prettyBytes from 'pretty-bytes';
|
|
|
|
|
2019-07-24 18:55:58 +02:00
|
|
|
import Loader from '../../App/components/loader';
|
2017-07-11 17:37:43 +02:00
|
|
|
import * as AssetActions from '../actions/assets';
|
|
|
|
|
|
|
|
class AssetList extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-05-30 06:37:10 +02:00
|
|
|
this.props.getAssets();
|
2017-07-11 17:37:43 +02:00
|
|
|
}
|
|
|
|
|
2018-02-23 17:31:41 +01:00
|
|
|
getAssetsTitle() {
|
2019-06-19 22:21:25 +02:00
|
|
|
if (!this.props.username || this.props.username === this.props.user.username) {
|
2018-02-23 17:31:41 +01:00
|
|
|
return 'p5.js Web Editor | My assets';
|
|
|
|
}
|
|
|
|
return `p5.js Web Editor | ${this.props.username}'s assets`;
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:55:58 +02:00
|
|
|
hasAssets() {
|
|
|
|
return !this.props.loading && this.props.assetList.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderLoader() {
|
|
|
|
if (this.props.loading) return <Loader />;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderEmptyTable() {
|
|
|
|
if (!this.props.loading && this.props.assetList.length === 0) {
|
|
|
|
return (<p className="asset-table__empty">No uploaded assets.</p>);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-11 17:37:43 +02:00
|
|
|
render() {
|
2019-06-19 22:21:25 +02:00
|
|
|
const username = this.props.username !== undefined ? this.props.username : this.props.user.username;
|
2019-11-04 20:30:24 +01:00
|
|
|
const { assetList } = this.props;
|
2017-07-11 17:37:43 +02:00
|
|
|
return (
|
|
|
|
<div className="asset-table-container">
|
2018-02-23 17:31:41 +01:00
|
|
|
<Helmet>
|
|
|
|
<title>{this.getAssetsTitle()}</title>
|
|
|
|
</Helmet>
|
2019-07-24 18:55:58 +02:00
|
|
|
{this.renderLoader()}
|
|
|
|
{this.renderEmptyTable()}
|
|
|
|
{this.hasAssets() &&
|
2017-07-11 17:37:43 +02:00
|
|
|
<table className="asset-table">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
2019-10-31 19:21:43 +01:00
|
|
|
<th scope="col">Name</th>
|
|
|
|
<th scope="col">Size</th>
|
|
|
|
<th scope="col">Sketch</th>
|
2017-07-11 17:37:43 +02:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2019-07-24 18:55:58 +02:00
|
|
|
{assetList.map(asset =>
|
2018-05-09 02:57:16 +02:00
|
|
|
(
|
|
|
|
<tr className="asset-table__row" key={asset.key}>
|
2019-10-31 19:21:43 +01:00
|
|
|
<th scope="row">
|
|
|
|
<Link to={asset.url} target="_blank">
|
|
|
|
{asset.name}
|
|
|
|
</Link>
|
|
|
|
</th>
|
2018-05-09 02:57:16 +02:00
|
|
|
<td>{prettyBytes(asset.size)}</td>
|
2019-06-19 22:21:25 +02:00
|
|
|
<td><Link to={`/${username}/sketches/${asset.sketchId}`}>{asset.sketchName}</Link></td>
|
2018-05-09 02:57:16 +02:00
|
|
|
</tr>
|
|
|
|
))}
|
2017-07-11 17:37:43 +02:00
|
|
|
</tbody>
|
|
|
|
</table>}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AssetList.propTypes = {
|
2018-02-23 17:31:41 +01:00
|
|
|
user: PropTypes.shape({
|
|
|
|
username: PropTypes.string
|
|
|
|
}).isRequired,
|
2017-07-11 17:37:43 +02:00
|
|
|
username: PropTypes.string.isRequired,
|
2019-07-24 18:55:58 +02:00
|
|
|
assetList: PropTypes.arrayOf(PropTypes.shape({
|
2017-07-11 17:37:43 +02:00
|
|
|
key: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
url: PropTypes.string.isRequired,
|
2020-02-05 00:40:54 +01:00
|
|
|
sketchName: PropTypes.string,
|
|
|
|
sketchId: PropTypes.string
|
2017-07-11 17:37:43 +02:00
|
|
|
})).isRequired,
|
|
|
|
getAssets: PropTypes.func.isRequired,
|
2019-07-24 18:55:58 +02:00
|
|
|
loading: PropTypes.bool.isRequired
|
2017-07-11 17:37:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
|
|
|
return {
|
|
|
|
user: state.user,
|
2019-07-24 18:55:58 +02:00
|
|
|
assetList: state.assets.list,
|
|
|
|
totalSize: state.assets.totalSize,
|
|
|
|
loading: state.loading
|
2017-07-11 17:37:43 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators(Object.assign({}, AssetActions), dispatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(AssetList);
|