diff --git a/client/modules/User/pages/DashboardView.jsx b/client/modules/User/pages/DashboardView.jsx index 3de9e7f956..f0b6d249b0 100644 --- a/client/modules/User/pages/DashboardView.jsx +++ b/client/modules/User/pages/DashboardView.jsx @@ -1,10 +1,9 @@ -import PropTypes from 'prop-types'; -import React from 'react'; -import { connect } from 'react-redux'; +import React, { useState, useCallback } from 'react'; +import { useDispatch, useSelector } from 'react-redux'; import MediaQuery from 'react-responsive'; -import { withTranslation } from 'react-i18next'; +import { useLocation, useParams } from 'react-router-dom'; +import { useTranslation } from 'react-i18next'; -import browserHistory from '../../../browserHistory'; import Button from '../../../common/Button'; import Nav from '../../IDE/components/Header/Nav'; import Overlay from '../../App/components/Overlay'; @@ -13,7 +12,7 @@ import AssetSize from '../../IDE/components/AssetSize'; import CollectionList from '../../IDE/components/CollectionList'; import SketchList from '../../IDE/components/SketchList'; import RootPage from '../../../components/RootPage'; -import * as ProjectActions from '../../IDE/actions/project'; +import { newProject } from '../../IDE/actions/project'; import { CollectionSearchbar, SketchSearchbar @@ -24,36 +23,24 @@ import DashboardTabSwitcherPublic, { TabKey } from '../components/DashboardTabSwitcher'; -class DashboardView extends React.Component { - static defaultProps = { - user: null - }; +const DashboardView = () => { + const { t } = useTranslation(); + + const params = useParams(); + const location = useLocation(); - constructor(props) { - super(props); - this.closeAccountPage = this.closeAccountPage.bind(this); - this.createNewSketch = this.createNewSketch.bind(this); - this.gotoHomePage = this.gotoHomePage.bind(this); - this.toggleCollectionCreate = this.toggleCollectionCreate.bind(this); - this.state = { - collectionCreateVisible: false - }; - } + const dispatch = useDispatch(); - closeAccountPage() { - browserHistory.push(this.props.previousPath); - } + const user = useSelector((state) => state.user); - createNewSketch() { - this.props.newProject(); - } + const [collectionCreateVisible, setCollectionCreateVisible] = useState(false); - gotoHomePage() { - browserHistory.push('/'); - } + const createNewSketch = () => { + dispatch(newProject()); + }; - selectedTabKey() { - const path = this.props.location.pathname; + const selectedTabKey = useCallback(() => { + const path = location.pathname; if (/assets/.test(path)) { return TabKey.assets; @@ -62,57 +49,53 @@ class DashboardView extends React.Component { } return TabKey.sketches; - } + }, [location.pathname]); - ownerName() { - if (this.props.params.username) { - return this.props.params.username; + const ownerName = () => { + if (params.username) { + return params.username; } - return this.props.user.username; - } + return user.username; + }; - isOwner() { - return this.props.user.username === this.props.params.username; - } + const isOwner = () => params.username === user.username; - toggleCollectionCreate() { - this.setState((prevState) => ({ - collectionCreateVisible: !prevState.collectionCreateVisible - })); - } + const toggleCollectionCreate = () => { + setCollectionCreateVisible((prevState) => !prevState); + }; - renderActionButton(tabKey, username, t) { + const renderActionButton = (tabKey) => { switch (tabKey) { case TabKey.assets: - return this.isOwner() && ; + return isOwner() && ; case TabKey.collections: return ( - this.isOwner() && ( - - + isOwner() && ( + <> + {t('DashboardView.CreateCollection')} - + > ) ); case TabKey.sketches: default: return ( - - {this.isOwner() && ( - + <> + {isOwner() && ( + {t('DashboardView.NewSketch')} )} - + > ); } - } + }; - renderContent(tabKey, username, mobile) { + const renderContent = (tabKey, username, mobile) => { switch (tabKey) { case TabKey.assets: return ; @@ -126,80 +109,46 @@ class DashboardView extends React.Component { ); } - } - - render() { - const currentTab = this.selectedTabKey(); - const isOwner = this.isOwner(); - const { username } = this.props.params; - const actions = this.renderActionButton(currentTab, username, this.props.t); - - return ( - - - - - - - {this.ownerName()} - - - - {actions && ( - {actions} - )} - - - - - - {(mobile) => this.renderContent(currentTab, username, mobile)} - - - - {this.state.collectionCreateVisible && ( - - - - )} - - ); - } -} - -function mapStateToProps(state) { - return { - previousPath: state.ide.previousPath, - user: state.user }; -} - -const mapDispatchToProps = { - ...ProjectActions -}; -DashboardView.propTypes = { - newProject: PropTypes.func.isRequired, - location: PropTypes.shape({ - pathname: PropTypes.string.isRequired - }).isRequired, - params: PropTypes.shape({ - username: PropTypes.string.isRequired - }).isRequired, - previousPath: PropTypes.string.isRequired, - user: PropTypes.shape({ - username: PropTypes.string - }), - t: PropTypes.func.isRequired + const currentTab = selectedTabKey(); + const actions = renderActionButton(currentTab); + + return ( + + + + + + {ownerName()} + + + {actions && ( + {actions} + )} + + + + + + {(mobile) => renderContent(currentTab, params.username, mobile)} + + + + {collectionCreateVisible && ( + + + + )} + + ); }; -export default withTranslation()( - connect(mapStateToProps, mapDispatchToProps)(DashboardView) -); +export default DashboardView;