1
- import PropTypes from 'prop-types' ;
2
- import React from 'react' ;
3
- import { connect } from 'react-redux' ;
1
+ import React , { useState , useCallback } from 'react' ;
2
+ import { useDispatch , useSelector } from 'react-redux' ;
4
3
import MediaQuery from 'react-responsive' ;
5
- import { withTranslation } from 'react-i18next' ;
4
+ import { useLocation , useParams } from 'react-router-dom' ;
5
+ import { useTranslation } from 'react-i18next' ;
6
6
7
- import browserHistory from '../../../browserHistory' ;
8
7
import Button from '../../../common/Button' ;
9
8
import Nav from '../../IDE/components/Header/Nav' ;
10
9
import Overlay from '../../App/components/Overlay' ;
@@ -13,7 +12,7 @@ import AssetSize from '../../IDE/components/AssetSize';
13
12
import CollectionList from '../../IDE/components/CollectionList' ;
14
13
import SketchList from '../../IDE/components/SketchList' ;
15
14
import RootPage from '../../../components/RootPage' ;
16
- import * as ProjectActions from '../../IDE/actions/project' ;
15
+ import { newProject } from '../../IDE/actions/project' ;
17
16
import {
18
17
CollectionSearchbar ,
19
18
SketchSearchbar
@@ -24,36 +23,24 @@ import DashboardTabSwitcherPublic, {
24
23
TabKey
25
24
} from '../components/DashboardTabSwitcher' ;
26
25
27
- class DashboardView extends React . Component {
28
- static defaultProps = {
29
- user : null
30
- } ;
26
+ const DashboardView = ( ) => {
27
+ const { t } = useTranslation ( ) ;
28
+
29
+ const params = useParams ( ) ;
30
+ const location = useLocation ( ) ;
31
31
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
- }
32
+ const dispatch = useDispatch ( ) ;
42
33
43
- closeAccountPage ( ) {
44
- browserHistory . push ( this . props . previousPath ) ;
45
- }
34
+ const user = useSelector ( ( state ) => state . user ) ;
46
35
47
- createNewSketch ( ) {
48
- this . props . newProject ( ) ;
49
- }
36
+ const [ collectionCreateVisible , setCollectionCreateVisible ] = useState ( false ) ;
50
37
51
- gotoHomePage ( ) {
52
- browserHistory . push ( '/' ) ;
53
- }
38
+ const createNewSketch = ( ) => {
39
+ dispatch ( newProject ( ) ) ;
40
+ } ;
54
41
55
- selectedTabKey ( ) {
56
- const path = this . props . location . pathname ;
42
+ const selectedTabKey = useCallback ( ( ) => {
43
+ const path = location . pathname ;
57
44
58
45
if ( / a s s e t s / . test ( path ) ) {
59
46
return TabKey . assets ;
@@ -62,57 +49,53 @@ class DashboardView extends React.Component {
62
49
}
63
50
64
51
return TabKey . sketches ;
65
- }
52
+ } , [ location . pathname ] ) ;
66
53
67
- ownerName ( ) {
68
- if ( this . props . params . username ) {
69
- return this . props . params . username ;
54
+ const ownerName = ( ) => {
55
+ if ( params . username ) {
56
+ return params . username ;
70
57
}
71
58
72
- return this . props . user . username ;
73
- }
59
+ return user . username ;
60
+ } ;
74
61
75
- isOwner ( ) {
76
- return this . props . user . username === this . props . params . username ;
77
- }
62
+ const isOwner = ( ) => params . username === user . username ;
78
63
79
- toggleCollectionCreate ( ) {
80
- this . setState ( ( prevState ) => ( {
81
- collectionCreateVisible : ! prevState . collectionCreateVisible
82
- } ) ) ;
83
- }
64
+ const toggleCollectionCreate = ( ) => {
65
+ setCollectionCreateVisible ( ( prevState ) => ! prevState ) ;
66
+ } ;
84
67
85
- renderActionButton ( tabKey , username , t ) {
68
+ const renderActionButton = ( tabKey ) => {
86
69
switch ( tabKey ) {
87
70
case TabKey . assets :
88
- return this . isOwner ( ) && < AssetSize /> ;
71
+ return isOwner ( ) && < AssetSize /> ;
89
72
case TabKey . collections :
90
73
return (
91
- this . isOwner ( ) && (
92
- < React . Fragment >
93
- < Button onClick = { this . toggleCollectionCreate } >
74
+ isOwner ( ) && (
75
+ < >
76
+ < Button onClick = { toggleCollectionCreate } >
94
77
{ t ( 'DashboardView.CreateCollection' ) }
95
78
</ Button >
96
79
< CollectionSearchbar />
97
- </ React . Fragment >
80
+ </ >
98
81
)
99
82
) ;
100
83
case TabKey . sketches :
101
84
default :
102
85
return (
103
- < React . Fragment >
104
- { this . isOwner ( ) && (
105
- < Button onClick = { this . createNewSketch } >
86
+ < >
87
+ { isOwner ( ) && (
88
+ < Button onClick = { createNewSketch } >
106
89
{ t ( 'DashboardView.NewSketch' ) }
107
90
</ Button >
108
91
) }
109
92
< SketchSearchbar />
110
- </ React . Fragment >
93
+ </ >
111
94
) ;
112
95
}
113
- }
96
+ } ;
114
97
115
- renderContent ( tabKey , username , mobile ) {
98
+ const renderContent = ( tabKey , username , mobile ) => {
116
99
switch ( tabKey ) {
117
100
case TabKey . assets :
118
101
return < AssetList key = { username } mobile = { mobile } username = { username } /> ;
@@ -126,80 +109,46 @@ class DashboardView extends React.Component {
126
109
< SketchList key = { username } mobile = { mobile } username = { username } />
127
110
) ;
128
111
}
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 >
157
-
158
- < div className = "dashboard-content" >
159
- < MediaQuery maxWidth = { 770 } >
160
- { ( mobile ) => this . renderContent ( currentTab , username , mobile ) }
161
- </ MediaQuery >
162
- </ 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
- }
175
- }
176
-
177
- function mapStateToProps ( state ) {
178
- return {
179
- previousPath : state . ide . previousPath ,
180
- user : state . user
181
112
} ;
182
- }
183
-
184
- const mapDispatchToProps = {
185
- ...ProjectActions
186
- } ;
187
113
188
- DashboardView . propTypes = {
189
- newProject : PropTypes . func . isRequired ,
190
- location : PropTypes . shape ( {
191
- pathname : PropTypes . string . isRequired
192
- } ) . isRequired ,
193
- params : PropTypes . shape ( {
194
- username : PropTypes . string . isRequired
195
- } ) . isRequired ,
196
- previousPath : PropTypes . string . isRequired ,
197
- user : PropTypes . shape ( {
198
- username : PropTypes . string
199
- } ) ,
200
- t : PropTypes . func . isRequired
114
+ const currentTab = selectedTabKey ( ) ;
115
+ const actions = renderActionButton ( currentTab ) ;
116
+
117
+ return (
118
+ < RootPage fixedHeight = "100%" >
119
+ < Nav layout = "dashboard" />
120
+
121
+ < main className = "dashboard-header" >
122
+ < div className = "dashboard-header__header" >
123
+ < h2 className = "dashboard-header__header__title" > { ownerName ( ) } </ h2 >
124
+ < div className = "dashboard-header__nav" >
125
+ < DashboardTabSwitcherPublic
126
+ currentTab = { currentTab }
127
+ isOwner = { isOwner ( ) }
128
+ username = { params . username }
129
+ />
130
+ { actions && (
131
+ < div className = "dashboard-header__actions" > { actions } </ div >
132
+ ) }
133
+ </ div >
134
+ </ div >
135
+
136
+ < div className = "dashboard-content" >
137
+ < MediaQuery maxWidth = { 770 } >
138
+ { ( mobile ) => renderContent ( currentTab , params . username , mobile ) }
139
+ </ MediaQuery >
140
+ </ div >
141
+ </ main >
142
+ { collectionCreateVisible && (
143
+ < Overlay
144
+ title = { t ( 'DashboardView.CreateCollectionOverlay' ) }
145
+ closeOverlay = { toggleCollectionCreate }
146
+ >
147
+ < CollectionCreate />
148
+ </ Overlay >
149
+ ) }
150
+ </ RootPage >
151
+ ) ;
201
152
} ;
202
153
203
- export default withTranslation ( ) (
204
- connect ( mapStateToProps , mapDispatchToProps ) ( DashboardView )
205
- ) ;
154
+ export default DashboardView ;
0 commit comments