-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
25d23db
rest of files for the class to functional conversion
nahbee10 5c424ed
fix connect import for redux
nahbee10 613bb01
Merge branch 'develop' into react-fc-conversion-4
nahbee10 4cd3e0e
fix test error
nahbee10 a466640
console height error ifx
nahbee10 9ed5799
fix test error
nahbee10 3ce1cb4
add stoppropagation
nahbee10 4ffc471
fix test errors in SketchList
khanniie cf00dc4
remove comments and use better react functions to memoize
nahbee10 d7fe122
final edits
nahbee10 c7a619c
fix collection list error
nahbee10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}, []); | ||
|
||
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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