-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
React class to functional conversion - final #3231
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
Changes from 9 commits
25d23db
5c424ed
613bb01
4cd3e0e
a466640
9ed5799
3ce1cb4
4ffc471
cf00dc4
d7fe122
c7a619c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,187 +1,63 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { connect, useDispatch } from 'react-redux'; | ||
import { bindActionCreators } from 'redux'; | ||
import { Link } from 'react-router-dom'; | ||
import React, { useEffect } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { Helmet } from 'react-helmet'; | ||
import prettyBytes from 'pretty-bytes'; | ||
import { useTranslation, withTranslation } from 'react-i18next'; | ||
import MenuItem from '../../../components/Dropdown/MenuItem'; | ||
import TableDropdown from '../../../components/Dropdown/TableDropdown'; | ||
|
||
import { useTranslation } from 'react-i18next'; | ||
import AssetListRow from './AssetListRow'; | ||
import Loader from '../../App/components/loader'; | ||
import { deleteAssetRequest } from '../actions/assets'; | ||
import * as AssetActions from '../actions/assets'; | ||
|
||
const AssetMenu = ({ item: asset }) => { | ||
const AssetList = () => { | ||
const { t } = useTranslation(); | ||
|
||
const dispatch = useDispatch(); | ||
const { username, assetList, loading } = useSelector((state) => ({ | ||
username: state.user.username, | ||
assetList: state.assets.list, | ||
loading: state.loading | ||
})); | ||
|
||
const handleAssetDelete = () => { | ||
const { key, name } = asset; | ||
if (window.confirm(t('Common.DeleteConfirmation', { name }))) { | ||
dispatch(deleteAssetRequest(key)); | ||
} | ||
}; | ||
|
||
return ( | ||
<TableDropdown aria-label={t('AssetList.ToggleOpenCloseARIA')}> | ||
<MenuItem onClick={handleAssetDelete}>{t('AssetList.Delete')}</MenuItem> | ||
<MenuItem href={asset.url} target="_blank"> | ||
{t('AssetList.OpenNewTab')} | ||
</MenuItem> | ||
</TableDropdown> | ||
); | ||
}; | ||
|
||
AssetMenu.propTypes = { | ||
item: PropTypes.shape({ | ||
key: PropTypes.string.isRequired, | ||
url: PropTypes.string.isRequired, | ||
name: PropTypes.string.isRequired | ||
}).isRequired | ||
}; | ||
|
||
const AssetListRowBase = ({ asset, username }) => ( | ||
<tr className="asset-table__row" key={asset.key}> | ||
<th scope="row"> | ||
<a href={asset.url} target="_blank" rel="noopener noreferrer"> | ||
{asset.name} | ||
</a> | ||
</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"> | ||
<AssetMenu item={asset} /> | ||
</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, | ||
username: PropTypes.string.isRequired | ||
}; | ||
|
||
function mapStateToPropsAssetListRow(state) { | ||
return { | ||
username: state.user.username | ||
}; | ||
} | ||
|
||
function mapDispatchToPropsAssetListRow(dispatch) { | ||
return bindActionCreators(AssetActions, dispatch); | ||
} | ||
|
||
const AssetListRow = connect( | ||
mapStateToPropsAssetListRow, | ||
mapDispatchToPropsAssetListRow | ||
)(AssetListRowBase); | ||
|
||
class AssetList extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.props.getAssets(); | ||
} | ||
|
||
getAssetsTitle() { | ||
return this.props.t('AssetList.Title'); | ||
} | ||
useEffect(() => { | ||
dispatch(AssetActions.getAssets()); | ||
}, [dispatch]); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i feel like since this is from the constructor we probably don't need anything in the dependency array? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
hasAssets() { | ||
return !this.props.loading && this.props.assetList.length > 0; | ||
} | ||
const hasAssets = () => !loading && assetList.length > 0; | ||
|
||
renderLoader() { | ||
if (this.props.loading) return <Loader />; | ||
return null; | ||
} | ||
const renderLoader = () => (loading ? <Loader /> : null); | ||
|
||
renderEmptyTable() { | ||
if (!this.props.loading && this.props.assetList.length === 0) { | ||
const renderEmptyTable = () => { | ||
if (!loading && assetList.length === 0) { | ||
return ( | ||
<p className="asset-table__empty"> | ||
{this.props.t('AssetList.NoUploadedAssets')} | ||
</p> | ||
<p className="asset-table__empty">{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> | ||
); | ||
} | ||
} | ||
|
||
AssetList.propTypes = { | ||
user: PropTypes.shape({ | ||
username: PropTypes.string | ||
}).isRequired, | ||
assetList: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
key: PropTypes.string.isRequired, | ||
name: PropTypes.string.isRequired, | ||
url: PropTypes.string.isRequired, | ||
sketchName: PropTypes.string, | ||
sketchId: PropTypes.string | ||
}) | ||
).isRequired, | ||
getAssets: PropTypes.func.isRequired, | ||
loading: PropTypes.bool.isRequired, | ||
t: PropTypes.func.isRequired | ||
}; | ||
|
||
function mapStateToProps(state) { | ||
return { | ||
user: state.user, | ||
assetList: state.assets.list, | ||
loading: state.loading | ||
}; | ||
} | ||
|
||
function mapDispatchToProps(dispatch) { | ||
return bindActionCreators(Object.assign({}, AssetActions), dispatch); | ||
} | ||
return ( | ||
<article className="asset-table-container"> | ||
<Helmet> | ||
<title>{t('AssetList.Title')}</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) => ( | ||
<AssetListRow asset={asset} key={asset.key} username={username} /> | ||
))} | ||
</tbody> | ||
</table> | ||
)} | ||
</article> | ||
); | ||
}; | ||
|
||
export default withTranslation()( | ||
connect(mapStateToProps, mapDispatchToProps)(AssetList) | ||
); | ||
export default AssetList; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { Link } from 'react-router-dom'; | ||
import { useDispatch } from 'react-redux'; | ||
import { useTranslation } from 'react-i18next'; | ||
import prettyBytes from 'pretty-bytes'; | ||
import MenuItem from '../../../components/Dropdown/MenuItem'; | ||
import TableDropdown from '../../../components/Dropdown/TableDropdown'; | ||
import { deleteAssetRequest } from '../actions/assets'; | ||
|
||
const AssetMenu = ({ item: asset }) => { | ||
const { t } = useTranslation(); | ||
const dispatch = useDispatch(); | ||
|
||
const handleAssetDelete = () => { | ||
const { key, name } = asset; | ||
if (window.confirm(t('Common.DeleteConfirmation', { name }))) { | ||
dispatch(deleteAssetRequest(key)); | ||
} | ||
}; | ||
|
||
return ( | ||
<TableDropdown aria-label={t('AssetList.ToggleOpenCloseARIA')}> | ||
<MenuItem onClick={handleAssetDelete}>{t('AssetList.Delete')}</MenuItem> | ||
<MenuItem href={asset.url} target="_blank"> | ||
{t('AssetList.OpenNewTab')} | ||
</MenuItem> | ||
</TableDropdown> | ||
); | ||
}; | ||
|
||
AssetMenu.propTypes = { | ||
item: PropTypes.shape({ | ||
key: PropTypes.string.isRequired, | ||
url: PropTypes.string.isRequired, | ||
name: PropTypes.string.isRequired | ||
}).isRequired | ||
}; | ||
|
||
const AssetListRow = ({ asset, username }) => ( | ||
<tr className="asset-table__row" key={asset.key}> | ||
<th scope="row"> | ||
<a href={asset.url} target="_blank" rel="noopener noreferrer"> | ||
{asset.name} | ||
</a> | ||
</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"> | ||
<AssetMenu item={asset} /> | ||
</td> | ||
</tr> | ||
); | ||
|
||
AssetListRow.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, | ||
username: PropTypes.string.isRequired | ||
}; | ||
|
||
export default AssetListRow; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal but FYI it's better to have separate
useSelector
calls for each piece of state that you read:https://redux.js.org/style-guide/#call-useselector-multiple-times-in-function-components