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