Skip to content

Commit fd4ed50

Browse files
authored
Update DashboardView.jsx
converted from class based component to functional component
1 parent d5eee45 commit fd4ed50

File tree

1 file changed

+83
-100
lines changed

1 file changed

+83
-100
lines changed

client/modules/User/pages/DashboardView.jsx

Lines changed: 83 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PropTypes from 'prop-types';
2-
import React from 'react';
2+
import React, { useState, useEffect } from 'react';
33
import { connect } from 'react-redux';
44
import MediaQuery from 'react-responsive';
55
import { withTranslation } from 'react-i18next';
@@ -24,36 +24,27 @@ import DashboardTabSwitcherPublic, {
2424
TabKey
2525
} from '../components/DashboardTabSwitcher';
2626

27-
class DashboardView extends React.Component {
28-
static defaultProps = {
29-
user: null
27+
function DashboardView(props) {
28+
const [collectionCreateVisible, setCollectionCreateVisible] = useState(false);
29+
30+
useEffect(() => {
31+
// You can add any componentDidMount logic here if needed.
32+
}, []);
33+
34+
const closeAccountPage = () => {
35+
browserHistory.push(props.previousPath);
36+
};
37+
38+
const createNewSketch = () => {
39+
props.newProject();
3040
};
3141

32-
constructor(props) {
33-
super(props);
34-
this.closeAccountPage = this.closeAccountPage.bind(this);
35-
this.createNewSketch = this.createNewSketch.bind(this);
36-
this.gotoHomePage = this.gotoHomePage.bind(this);
37-
this.toggleCollectionCreate = this.toggleCollectionCreate.bind(this);
38-
this.state = {
39-
collectionCreateVisible: false
40-
};
41-
}
42-
43-
closeAccountPage() {
44-
browserHistory.push(this.props.previousPath);
45-
}
46-
47-
createNewSketch() {
48-
this.props.newProject();
49-
}
50-
51-
gotoHomePage() {
42+
const gotoHomePage = () => {
5243
browserHistory.push('/');
53-
}
44+
};
5445

55-
selectedTabKey() {
56-
const path = this.props.location.pathname;
46+
const selectedTabKey = () => {
47+
const path = props.location.pathname;
5748

5849
if (/assets/.test(path)) {
5950
return TabKey.assets;
@@ -62,57 +53,55 @@ class DashboardView extends React.Component {
6253
}
6354

6455
return TabKey.sketches;
65-
}
56+
};
6657

67-
ownerName() {
68-
if (this.props.params.username) {
69-
return this.props.params.username;
58+
const ownerName = () => {
59+
if (props.params.username) {
60+
return props.params.username;
7061
}
7162

72-
return this.props.user.username;
73-
}
63+
return props.user.username;
64+
};
7465

75-
isOwner() {
76-
return this.props.user.username === this.props.params.username;
77-
}
66+
const isOwner = () => {
67+
return props.user.username === props.params.username;
68+
};
7869

79-
toggleCollectionCreate() {
80-
this.setState((prevState) => ({
81-
collectionCreateVisible: !prevState.collectionCreateVisible
82-
}));
83-
}
70+
const toggleCollectionCreate = () => {
71+
setCollectionCreateVisible((prevState) => !prevState);
72+
};
8473

85-
renderActionButton(tabKey, username, t) {
74+
const renderActionButton = (tabKey, username, t) => {
8675
switch (tabKey) {
8776
case TabKey.assets:
88-
return this.isOwner() && <AssetSize />;
77+
return isOwner() && <AssetSize />;
8978
case TabKey.collections:
9079
return (
91-
this.isOwner() && (
92-
<React.Fragment>
93-
<Button onClick={this.toggleCollectionCreate}>
80+
isOwner() && (
81+
<>
82+
<Button onClick={toggleCollectionCreate}>
9483
{t('DashboardView.CreateCollection')}
9584
</Button>
9685
<CollectionSearchbar />
97-
</React.Fragment>
86+
</>
9887
)
9988
);
10089
case TabKey.sketches:
10190
default:
10291
return (
103-
<React.Fragment>
104-
{this.isOwner() && (
105-
<Button onClick={this.createNewSketch}>
92+
<>
93+
{isOwner() && (
94+
<Button onClick={createNewSketch}>
10695
{t('DashboardView.NewSketch')}
10796
</Button>
10897
)}
10998
<SketchSearchbar />
110-
</React.Fragment>
99+
</>
111100
);
112101
}
113-
}
102+
};
114103

115-
renderContent(tabKey, username, mobile) {
104+
const renderContent = (tabKey, username, mobile) => {
116105
switch (tabKey) {
117106
case TabKey.assets:
118107
return <AssetList key={username} mobile={mobile} username={username} />;
@@ -122,56 +111,50 @@ class DashboardView extends React.Component {
122111
);
123112
case TabKey.sketches:
124113
default:
125-
return (
126-
<SketchList key={username} mobile={mobile} username={username} />
127-
);
114+
return <SketchList key={username} mobile={mobile} username={username} />;
128115
}
129-
}
130-
131-
render() {
132-
const currentTab = this.selectedTabKey();
133-
const isOwner = this.isOwner();
134-
const { username } = this.props.params;
135-
const actions = this.renderActionButton(currentTab, username, this.props.t);
136-
137-
return (
138-
<RootPage fixedHeight="100%">
139-
<Nav layout="dashboard" />
140-
141-
<main className="dashboard-header">
142-
<div className="dashboard-header__header">
143-
<h2 className="dashboard-header__header__title">
144-
{this.ownerName()}
145-
</h2>
146-
<div className="dashboard-header__nav">
147-
<DashboardTabSwitcherPublic
148-
currentTab={currentTab}
149-
isOwner={isOwner}
150-
username={username}
151-
/>
152-
{actions && (
153-
<div className="dashboard-header__actions">{actions}</div>
154-
)}
155-
</div>
156-
</div>
116+
};
157117

158-
<div className="dashboard-content">
159-
<MediaQuery maxWidth={770}>
160-
{(mobile) => this.renderContent(currentTab, username, mobile)}
161-
</MediaQuery>
118+
const currentTab = selectedTabKey();
119+
const isOwnerFlag = isOwner();
120+
const { username } = props.params;
121+
const actions = renderActionButton(currentTab, username, props.t);
122+
123+
return (
124+
<RootPage fixedHeight="100%">
125+
<Nav layout="dashboard" />
126+
127+
<main className="dashboard-header">
128+
<div className="dashboard-header__header">
129+
<h2 className="dashboard-header__header__title">{ownerName()}</h2>
130+
<div className="dashboard-header__nav">
131+
<DashboardTabSwitcherPublic
132+
currentTab={currentTab}
133+
isOwner={isOwnerFlag}
134+
username={username}
135+
/>
136+
{actions && (
137+
<div className="dashboard-header__actions">{actions}</div>
138+
)}
162139
</div>
163-
</main>
164-
{this.state.collectionCreateVisible && (
165-
<Overlay
166-
title={this.props.t('DashboardView.CreateCollectionOverlay')}
167-
closeOverlay={this.toggleCollectionCreate}
168-
>
169-
<CollectionCreate />
170-
</Overlay>
171-
)}
172-
</RootPage>
173-
);
174-
}
140+
</div>
141+
142+
<div className="dashboard-content">
143+
<MediaQuery maxWidth={770}>
144+
{(mobile) => renderContent(currentTab, username, mobile)}
145+
</MediaQuery>
146+
</div>
147+
</main>
148+
{collectionCreateVisible && (
149+
<Overlay
150+
title={props.t('DashboardView.CreateCollectionOverlay')}
151+
closeOverlay={toggleCollectionCreate}
152+
>
153+
<CollectionCreate />
154+
</Overlay>
155+
)}
156+
</RootPage>
157+
);
175158
}
176159

177160
function mapStateToProps(state) {

0 commit comments

Comments
 (0)