Browse Source

fixed route handling

Skylsmoi 5 years ago
parent
commit
bdb63cf2d5

+ 2 - 2
src/appFactory.js View File

@@ -15,7 +15,7 @@ export function appFactory (WrappedComponent) {
15 15
       content
16 16
     })
17 17
 
18
-    renderCreateContentApp = (appConfig, user, idWorkspace, idfolder) => GLOBAL_renderCreateContentApp({
18
+    renderCreateContentApp = (appConfig, user, idWorkspace, idFolder) => GLOBAL_renderCreateContentApp({
19 19
       loggedUser: user.logged ? user : {},
20 20
       config: {
21 21
         ...appConfig,
@@ -25,7 +25,7 @@ export function appFactory (WrappedComponent) {
25 25
         apiHeader: FETCH_CONFIG.headers
26 26
       },
27 27
       idWorkspace,
28
-      idfolder
28
+      idFolder
29 29
     })
30 30
 
31 31
     emitEventApp = (type, data) => GLOBAL_dispatchEvent(type, data)

+ 41 - 4
src/container/Dashboard.jsx View File

@@ -1,11 +1,21 @@
1
-import React, { Component } from 'react'
1
+import React from 'react'
2
+import { connect } from 'react-redux'
2 3
 import Sidebar from './Sidebar.jsx'
3 4
 import imgProfil from '../img/imgProfil.png'
4
-
5
-class Dashboard extends Component {
5
+import {
6
+  getAppList,
7
+  getContentTypeList, getWorkspaceList
8
+} from '../action-creator.async.js'
9
+import {
10
+  setAppList,
11
+  setContentTypeList, setWorkspaceListIsOpenInSidebar, updateWorkspaceListData
12
+} from '../action-creator.sync.js'
13
+
14
+class Dashboard extends React.Component {
6 15
   constructor (props) {
7 16
     super(props)
8 17
     this.state = {
18
+      workspaceIdInUrl: props.match.params.idws ? parseInt(props.match.params.idws) : null, // this is used to avoid handling the parseInt everytime
9 19
       displayNewMemberDashboard: false,
10 20
       displayNotifBtn: false,
11 21
       displayWebdavBtn: false,
@@ -13,6 +23,32 @@ class Dashboard extends Component {
13 23
     }
14 24
   }
15 25
 
26
+  async componentDidMount () {
27
+    const { workspaceIdInUrl } = this.state
28
+    const { user, workspaceList, app, contentType, dispatch } = this.props
29
+
30
+    console.log('<Dashboard> componentDidMount')
31
+
32
+    if (app.length === 0) {
33
+      const fetchGetAppList = await dispatch(getAppList())
34
+      if (fetchGetAppList.status === 200) dispatch(setAppList(fetchGetAppList.json))
35
+    }
36
+
37
+    if (contentType.length === 0) {
38
+      const fetchGetContentTypeList = await dispatch(getContentTypeList())
39
+      if (fetchGetContentTypeList.status === 200) dispatch(setContentTypeList(fetchGetContentTypeList.json))
40
+    }
41
+
42
+    if (user.user_id !== -1 && workspaceList.length === 0) {
43
+      const fetchGetWorkspaceList = await dispatch(getWorkspaceList(user.user_id))
44
+
45
+      if (fetchGetWorkspaceList.status === 200) {
46
+        dispatch(updateWorkspaceListData(fetchGetWorkspaceList.json))
47
+        dispatch(setWorkspaceListIsOpenInSidebar(workspaceIdInUrl || fetchGetWorkspaceList.json[0].workspace_id, true))
48
+      }
49
+    }
50
+  }
51
+
16 52
   handleToggleNewMemberDashboard = () => this.setState(prevState => ({
17 53
     displayNewMemberDashboard: !prevState.displayNewMemberDashboard
18 54
   }))
@@ -551,4 +587,5 @@ class Dashboard extends Component {
551 587
   }
552 588
 }
553 589
 
554
-export default Dashboard
590
+const mapStateToProps = ({ user, app, contentType, workspaceList }) => ({ user, app, contentType, workspaceList })
591
+export default connect(mapStateToProps)(Dashboard)

+ 2 - 1
src/container/OpenContentApp.jsx View File

@@ -10,7 +10,8 @@ export class OpenContentApp extends React.Component {
10 10
     if (isNaN(idWorkspace)) return
11 11
 
12 12
     if (['type', 'idcts'].every(p => p in match.params) && match.params.type !== 'contents' && workspaceContent.id !== -1 && workspaceContent.length) {
13
-      // @TODO test validity of params, idcts isNaN and type includes list of available content type
13
+      if (isNaN(match.params.idcts) || !contentType.map(c => c.slug).includes(match.params.type)) return
14
+
14 15
       const contentToOpen = {
15 16
         content_id: parseInt(match.params.idcts),
16 17
         workspace_id: parseInt(idWorkspace),

+ 1 - 0
src/container/Sidebar.jsx View File

@@ -22,6 +22,7 @@ class Sidebar extends React.Component {
22 22
   }
23 23
 
24 24
   componentDidUpdate (prevProps, prevState) {
25
+    console.log('<Sidebar> Did Update')
25 26
     if (this.props.match.params.idws === undefined) return
26 27
 
27 28
     const newWorkspaceId = parseInt(this.props.match.params.idws)

+ 8 - 8
src/container/Tracim.jsx View File

@@ -11,7 +11,8 @@ import WorkspaceContent from './WorkspaceContent.jsx'
11 11
 import WIPcomponent from './WIPcomponent.jsx'
12 12
 import {
13 13
   Route,
14
-  withRouter
14
+  withRouter,
15
+  Switch
15 16
 } from 'react-router-dom'
16 17
 import PrivateRoute from './PrivateRoute.jsx'
17 18
 import { PAGE } from '../helper.js'
@@ -54,14 +55,13 @@ class Tracim extends React.Component {
54 55
             <div className='tracim__content'> {/* uses of <Switch> component in react router ? */}
55 56
               <Route path={PAGE.LOGIN} component={Login} />
56 57
 
57
-              <PrivateRoute exact path={PAGE.HOME} component={WorkspaceContent} />
58
-              <PrivateRoute path={PAGE.ACCOUNT} component={Account} />
59
-              {/* bellow, the '?' is important, it avoid to have to declare another route for CONTENT_LIST which could double match */}
60
-              {/* <PrivateRoute path={PAGE.WORKSPACE.CONTENT(':idws', ':type?', ':idcts?')} component={WorkspaceContent} /> */}
61
-
62
-              <Route path='/workspaces/:idws/' component={WorkspaceContent} />
58
+              <Switch>
59
+                <PrivateRoute path={PAGE.WORKSPACE.DASHBOARD(':idws')} component={Dashboard} />
60
+                <PrivateRoute path={PAGE.WORKSPACE.CALENDAR(':idws')} component={() => <div><br /><br /><br /><br />NYI</div>} />
61
+                <PrivateRoute path={PAGE.WORKSPACE.CONTENT(':idws', ':type?', ':idcts?')} component={WorkspaceContent} />
62
+              </Switch>
63 63
 
64
-              <PrivateRoute exact path={PAGE.WORKSPACE.DASHBOARD(':idws')} component={Dashboard} />
64
+              <PrivateRoute path={PAGE.ACCOUNT} component={Account} />
65 65
               <PrivateRoute path={'/wip/:cp'} component={WIPcomponent} /> {/* for testing purpose only */}
66 66
 
67 67
               <Footer />

+ 16 - 9
src/container/WorkspaceContent.jsx View File

@@ -1,6 +1,6 @@
1 1
 import React from 'react'
2 2
 import { connect } from 'react-redux'
3
-import { withRouter, Route } from 'react-router-dom'
3
+import { withRouter } from 'react-router-dom'
4 4
 import appFactory from '../appFactory.js'
5 5
 import { PAGE } from '../helper.js'
6 6
 import Sidebar from './Sidebar.jsx'
@@ -64,7 +64,7 @@ class WorkspaceContent extends React.Component {
64 64
     const { workspaceIdInUrl } = this.state
65 65
     const { user, workspaceList, app, contentType, match, location, dispatch } = this.props
66 66
 
67
-    console.log('componentDidMount')
67
+    console.log('<WorkspaceContent> componentDidMount')
68 68
 
69 69
     if (app.length === 0) {
70 70
       const fetchGetAppList = await dispatch(getAppList())
@@ -100,16 +100,25 @@ class WorkspaceContent extends React.Component {
100 100
     else dispatch(newFlashMessage('Error while loading workspace', 'danger'))
101 101
   }
102 102
 
103
-  componentDidUpdate (prevProps, prevState) {
104
-    console.log('componentDidUpdate')
103
+  async componentDidUpdate (prevProps, prevState) {
104
+    const { match, location, dispatch } = this.props
105
+
106
+    console.log('<WorkspaceContent> componentDidUpdate')
105 107
 
106 108
     if (this.state.workspaceIdInUrl === null) return
107 109
 
108
-    const idWorkspace = parseInt(this.props.match.params.idws)
110
+    const idWorkspace = parseInt(match.params.idws)
109 111
 
110 112
     if (isNaN(idWorkspace)) return
111 113
 
112
-    if (prevState.workspaceIdInUrl !== idWorkspace) this.setState({workspaceIdInUrl: idWorkspace})
114
+    if (prevState.workspaceIdInUrl !== idWorkspace) {
115
+      this.setState({workspaceIdInUrl: idWorkspace})
116
+
117
+      const wsContent = await dispatch(getWorkspaceContentList(idWorkspace, 0))
118
+
119
+      if (wsContent.status === 200) dispatch(setWorkspaceContent(wsContent.json, qs.parse(location.search).type))
120
+      else dispatch(newFlashMessage('Error while loading workspace', 'danger'))
121
+    }
113 122
 
114 123
     // if (user.user_id !== -1 && prevProps.user.id !== user.id) dispatch(getWorkspaceList(user.user_id, idWorkspace))
115 124
   }
@@ -183,9 +192,7 @@ class WorkspaceContent extends React.Component {
183 192
       <div className='sidebarpagecontainer'>
184 193
         <Sidebar />
185 194
 
186
-        <Route path={`${match.url}/:type/:idcts`} render={() =>
187
-          <OpenContentApp idWorkspace={match.params.idws} appOpened={this.state.appOpened} updateAppOpened={this.handleUpdateAppOpened} />}
188
-        />
195
+        <OpenContentApp idWorkspace={match.params.idws} appOpened={this.state.appOpened} updateAppOpened={this.handleUpdateAppOpened} />
189 196
 
190 197
         <PageWrapper customeClass='workspace'>
191 198
           <PageTitle

+ 1 - 1
src/helper.js View File

@@ -15,7 +15,7 @@ export const PAGE = {
15 15
     NEW: '/workspaces/new',
16 16
     CALENDAR: (idws = ':idws') => `/workspaces/${idws}/calendar`,
17 17
     CONTENT_LIST: (idws = ':idws') => `/workspaces/${idws}/contents`,
18
-    CONTENT: (idws = ':idws', type = ':type', idcts = ':idcts') => `/workspaces/${idws}/${type}/${idcts}`,
18
+    CONTENT: (idws = ':idws', type = ':type?', idcts = ':idcts?') => `/workspaces/${idws}/${type}/${idcts}`,
19 19
     CONTENT_NEW: (idws = ':idws', ctstype = ':ctstype') => `/workspaces/${idws}/contents/${ctstype}/new`,
20 20
     CONTENT_EDIT: (idws = ':idws', idcts = ':idcts') => `/workspaces/${idws}/contents/${idcts}/edit`,
21 21
     CONTENT_TITLE_EDIT: (idws = ':idws', idcts = ':idcts') => `/workspaces/${idws}/contents/${idcts}/title/edit`,