Skip to content

Moved AssetList to functional comp #2447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
313 changes: 138 additions & 175 deletions client/modules/IDE/components/AssetList.jsx
Original file line number Diff line number Diff line change
@@ -1,220 +1,182 @@
import PropTypes from 'prop-types';
import React from 'react';
import React, { useState, useEffect } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { Link } from 'react-router-dom';
import { Helmet } from 'react-helmet';
import prettyBytes from 'pretty-bytes';
import { withTranslation } from 'react-i18next';
import PropTypes from 'prop-types'; // Import PropTypes

import Loader from '../../App/components/loader';
import * as AssetActions from '../actions/assets';
import DownFilledTriangleIcon from '../../../images/down-filled-triangle.svg';

class AssetListRowBase extends React.Component {
constructor(props) {
super(props);
this.state = {
isFocused: false,
optionsOpen: false
};
}

onFocusComponent = () => {
this.setState({ isFocused: true });
function AssetListRowBase(props) {
const [isFocused, setIsFocused] = useState(false);
const [optionsOpen, setOptionsOpen] = useState(false);

const onFocusComponent = () => {
setIsFocused(true);
};

const closeOptions = () => {
setOptionsOpen(false);
};

onBlurComponent = () => {
this.setState({ isFocused: false });
const onBlurComponent = () => {
setIsFocused(false);
setTimeout(() => {
if (!this.state.isFocused) {
this.closeOptions();
if (!isFocused) {
closeOptions();
}
}, 200);
};

openOptions = () => {
this.setState({
optionsOpen: true
});
};

closeOptions = () => {
this.setState({
optionsOpen: false
});
const openOptions = () => {
setOptionsOpen(true);
};

toggleOptions = () => {
if (this.state.optionsOpen) {
this.closeOptions();
const toggleOptions = () => {
if (optionsOpen) {
closeOptions();
} else {
this.openOptions();
openOptions();
}
};

handleDropdownOpen = () => {
this.closeOptions();
this.openOptions();
};

handleAssetDelete = () => {
const { key, name } = this.props.asset;
this.closeOptions();
if (window.confirm(this.props.t('Common.DeleteConfirmation', { name }))) {
this.props.deleteAssetRequest(key);
const handleAssetDelete = () => {
const { key, name } = props.asset;
closeOptions();
if (window.confirm(props.t('Common.DeleteConfirmation', { name }))) {
props.deleteAssetRequest(key);
}
};

render() {
const { asset, username, t } = this.props;
const { optionsOpen } = this.state;
return (
<tr className="asset-table__row" key={asset.key}>
<th scope="row">
<Link to={asset.url} target="_blank">
{asset.name}
const { asset, username, t } = props;
return (
<tr className="asset-table__row" key={asset.key}>
<th scope="row">
<Link to={asset.url} target="_blank">
{asset.name}
</Link>
</th>
<td>{prettyBytes(asset.size)}</td>
<td>
{asset.sketchId && (
<Link to={`/${username}/sketches/${asset.sketchId}`}>
{asset.sketchName}
</Link>
</th>
<td>{prettyBytes(asset.size)}</td>
<td>
{asset.sketchId && (
<Link to={`/${username}/sketches/${asset.sketchId}`}>
{asset.sketchName}
</Link>
)}
</td>
<td className="asset-table__dropdown-column">
<button
className="asset-table__dropdown-button"
onClick={this.toggleOptions}
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
aria-label={t('AssetList.ToggleOpenCloseARIA')}
>
<DownFilledTriangleIcon focusable="false" aria-hidden="true" />
</button>
{optionsOpen && (
<ul className="asset-table__action-dialogue">
<li>
<button
className="asset-table__action-option"
onClick={this.handleAssetDelete}
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
>
{t('AssetList.Delete')}
</button>
</li>
<li>
<Link
to={asset.url}
target="_blank"
onBlur={this.onBlurComponent}
onFocus={this.onFocusComponent}
className="asset-table__action-option"
>
{t('AssetList.OpenNewTab')}
</Link>
</li>
</ul>
)}
</td>
</tr>
);
}
}

AssetListRowBase.propTypes = {
asset: PropTypes.shape({
key: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
sketchId: PropTypes.string,
sketchName: PropTypes.string,
name: PropTypes.string.isRequired,
size: PropTypes.number.isRequired
}).isRequired,
deleteAssetRequest: PropTypes.func.isRequired,
username: PropTypes.string.isRequired,
t: PropTypes.func.isRequired
};

function mapStateToPropsAssetListRow(state) {
return {
username: state.user.username
};
}

function mapDispatchToPropsAssetListRow(dispatch) {
return bindActionCreators(AssetActions, dispatch);
)}
</td>
<td className="asset-table__dropdown-column">
<button
className="asset-table__dropdown-button"
onClick={toggleOptions}
onBlur={onBlurComponent}
onFocus={onFocusComponent}
aria-label={t('AssetList.ToggleOpenCloseARIA')}
>
<DownFilledTriangleIcon focusable="false" aria-hidden="true" />
</button>
{optionsOpen && (
<ul className="asset-table__action-dialogue">
<li>
<button
className="asset-table__action-option"
onClick={handleAssetDelete}
onBlur={onBlurComponent}
onFocus={onFocusComponent}
>
{t('AssetList.Delete')}
</button>
</li>
<li>
<Link
to={asset.url}
target="_blank"
onBlur={onBlurComponent}
onFocus={onFocusComponent}
className="asset-table__action-option"
>
{t('AssetList.OpenNewTab')}
</Link>
</li>
</ul>
)}
</td>
</tr>
);
}

const AssetListRow = connect(
mapStateToPropsAssetListRow,
mapDispatchToPropsAssetListRow
)(AssetListRowBase);
function AssetList(props) {
useEffect(() => {
props.getAssets();
}, []);

class AssetList extends React.Component {
constructor(props) {
super(props);
this.props.getAssets();
}
const getAssetsTitle = () => props.t('AssetList.Title');

getAssetsTitle() {
return this.props.t('AssetList.Title');
}
const hasAssets = () => !props.loading && props.assetList.length > 0;

hasAssets() {
return !this.props.loading && this.props.assetList.length > 0;
}

renderLoader() {
if (this.props.loading) return <Loader />;
const renderLoader = () => {
if (props.loading) return <Loader />;
return null;
}
};

renderEmptyTable() {
if (!this.props.loading && this.props.assetList.length === 0) {
const renderEmptyTable = () => {
if (!props.loading && props.assetList.length === 0) {
return (
<p className="asset-table__empty">
{this.props.t('AssetList.NoUploadedAssets')}
{props.t('AssetList.NoUploadedAssets')}
</p>
);
}
return null;
}

render() {
const { assetList, t } = this.props;
return (
<article className="asset-table-container">
<Helmet>
<title>{this.getAssetsTitle()}</title>
</Helmet>
{this.renderLoader()}
{this.renderEmptyTable()}
{this.hasAssets() && (
<table className="asset-table">
<thead>
<tr>
<th>{t('AssetList.HeaderName')}</th>
<th>{t('AssetList.HeaderSize')}</th>
<th>{t('AssetList.HeaderSketch')}</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{assetList.map((asset) => (
<AssetListRow asset={asset} key={asset.key} t={t} />
))}
</tbody>
</table>
)}
</article>
);
}
};

const { assetList, t } = props;
return (
<article className="asset-table-container">
<Helmet>
<title>{getAssetsTitle()}</title>
</Helmet>
{renderLoader()}
{renderEmptyTable()}
{hasAssets() && (
<table className="asset-table">
<thead>
<tr>
<th>{t('AssetList.HeaderName')}</th>
<th>{t('AssetList.HeaderSize')}</th>
<th>{t('AssetList.HeaderSketch')}</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{assetList.map((asset) => (
<AssetListRowBase asset={asset} key={asset.key} t={t} />
))}
</tbody>
</table>
)}
</article>
);
}

// AssetListRowBase component PropTypes
AssetListRowBase.propTypes = {
asset: PropTypes.string.isRequired,
key: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
sketchName: PropTypes.string.isRequired,
sketchId: PropTypes.string.isRequired,
size: PropTypes.number.isRequired,
username: PropTypes.string.isRequired,
t: PropTypes.string.isRequired,
deleteAssetRequest: PropTypes.string.isRequired
};

// AssetList component PropTypes
AssetList.propTypes = {
user: PropTypes.shape({
username: PropTypes.string
Expand All @@ -225,7 +187,8 @@ AssetList.propTypes = {
name: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
sketchName: PropTypes.string,
sketchId: PropTypes.string
sketchId: PropTypes.string,
size: PropTypes.number.isRequired
})
).isRequired,
getAssets: PropTypes.func.isRequired,
Expand Down
Loading