Browse Source

fixed sidebar filter and route for url '/'

Skylsmoi 6 years ago
parent
commit
c891ae84af

+ 3 - 9
src/action-creator.async.js View File

@@ -13,12 +13,10 @@ import {
13 13
   setUserRole,
14 14
   WORKSPACE,
15 15
   WORKSPACE_LIST,
16
-  updateWorkspaceListData,
17 16
   FOLDER,
18 17
   setFolderData,
19 18
   APP_LIST,
20
-  setAppList,
21
-  setWorkspaceListIsOpenInSidebar
19
+  setAppList
22 20
 } from './action-creator.sync.js'
23 21
 
24 22
 /*
@@ -155,17 +153,13 @@ export const updateUserLang = newLang => async dispatch => { // unused
155 153
 //   console.log('jsonResponseNoData', fetchResponseNoData)
156 154
 // }
157 155
 
158
-export const getWorkspaceList = (userId, workspaceIdToOpen) => async dispatch => {
159
-  const fetchGetWorkspaceList = await fetchWrapper({
156
+export const getWorkspaceList = userId => dispatch => {
157
+  return fetchWrapper({
160 158
     url: `${FETCH_CONFIG.mockApiUrl}/user/${userId}/workspace`,
161 159
     param: {...FETCH_CONFIG.header, method: 'GET'},
162 160
     actionName: WORKSPACE_LIST,
163 161
     dispatch
164 162
   })
165
-  if (fetchGetWorkspaceList.status === 200) {
166
-    dispatch(updateWorkspaceListData(fetchGetWorkspaceList.json))
167
-    dispatch(setWorkspaceListIsOpenInSidebar(workspaceIdToOpen, true))
168
-  }
169 163
 }
170 164
 
171 165
 export const getWorkspaceContent = workspaceId => dispatch => {

+ 1 - 1
src/component/Sidebar/WorkspaceListItem.jsx View File

@@ -95,7 +95,7 @@ const WorkspaceListItem = props => {
95 95
 
96 96
                 <div className='dropdown__title' id='navbarDropdown'>
97 97
                   <div className='dropdown__title__text'>
98
-                    { props.app[a].label[props.lang.id] }
98
+                    {props.app[a].label[props.lang.id]}
99 99
                   </div>
100 100
                 </div>
101 101
               </div>

+ 3 - 15
src/container/Sidebar.jsx View File

@@ -4,7 +4,6 @@ import { withRouter } from 'react-router'
4 4
 import classnames from 'classnames'
5 5
 import { translate } from 'react-i18next'
6 6
 import WorkspaceListItem from '../component/Sidebar/WorkspaceListItem.jsx'
7
-import { getWorkspaceList } from '../action-creator.async.js'
8 7
 import {
9 8
   setWorkspaceListIsOpenInSidebar,
10 9
   updateWorkspaceFilter
@@ -20,22 +19,11 @@ class Sidebar extends React.Component {
20 19
     }
21 20
   }
22 21
 
23
-  componentDidMount () {
24
-    const { workspaceIdInUrl } = this.state
25
-    const { user, workspaceList, dispatch } = this.props
26
-
27
-    user.id !== -1 && workspaceList.length === 0 && dispatch(getWorkspaceList(user.id, workspaceIdInUrl))
28
-  }
29
-
30 22
   componentDidUpdate (prevProps, prevState) {
31
-    const { user, match, dispatch } = this.props
32
-
33
-    if (this.state.workspaceIdInUrl === null) return
34
-
35
-    const newWorkspaceId = parseInt(match.params.idws)
36
-    prevState.workspaceIdInUrl !== newWorkspaceId && this.setState({workspaceIdInUrl: newWorkspaceId})
23
+    if (this.props.match.params.idws === undefined) return
37 24
 
38
-    user.id !== -1 && prevProps.user.id !== user.id && dispatch(getWorkspaceList(user.id, newWorkspaceId))
25
+    const newWorkspaceId = parseInt(this.props.match.params.idws)
26
+    if (prevState.workspaceIdInUrl !== newWorkspaceId) this.setState({workspaceIdInUrl: newWorkspaceId})
39 27
   }
40 28
 
41 29
   handleClickWorkspace = (idWs, newIsOpenInSidebar) => this.props.dispatch(setWorkspaceListIsOpenInSidebar(idWs, newIsOpenInSidebar))

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

@@ -58,9 +58,9 @@ class Tracim extends React.Component {
58 58
               <Route path={PAGE.LOGIN} component={Login} />
59 59
 
60 60
               <PrivateRoute exact path={PAGE.HOME} component={WorkspaceContent} />
61
+              <PrivateRoute path={PAGE.ACCOUNT} component={Account} />
61 62
               {/* bellow, the '?' is important, it avoid to have to declare another route for CONTENT_LIST which could double match */}
62 63
               <PrivateRoute path={PAGE.WORKSPACE.CONTENT(':idws', ':idcts?')} component={WorkspaceContent} />
63
-              <PrivateRoute path={PAGE.ACCOUNT} component={Account} />
64 64
               <PrivateRoute exact path={PAGE.WORKSPACE.DASHBOARD()} component={Dashboard} />
65 65
               <PrivateRoute path={'/wip/:cp'} component={WIPcomponent} /> {/* for testing purpose only */}
66 66
 

+ 34 - 6
src/container/WorkspaceContent.jsx View File

@@ -14,11 +14,14 @@ import PopupCreateContent from '../component/PopupCreateContent/PopupCreateConta
14 14
 import {
15 15
   getAppList,
16 16
   getWorkspaceContent,
17
-  getFolderContent
17
+  getFolderContent,
18
+  getWorkspaceList
18 19
 } from '../action-creator.async.js'
19 20
 import {
20 21
   newFlashMessage,
21
-  setWorkspaceData
22
+  setWorkspaceData,
23
+  setWorkspaceListIsOpenInSidebar,
24
+  updateWorkspaceListData
22 25
 } from '../action-creator.sync.js'
23 26
 
24 27
 const qs = require('query-string')
@@ -31,18 +34,32 @@ class WorkspaceContent extends React.Component {
31 34
         display: false,
32 35
         type: undefined,
33 36
         folder: undefined
34
-      }
37
+      },
38
+      workspaceIdInUrl: props.match.params.idws ? parseInt(props.match.params.idws) : null
35 39
     }
36 40
   }
37 41
 
38 42
   async componentDidMount () {
39
-    const { workspaceList, app, match, location, dispatch } = this.props
43
+    const { workspaceIdInUrl } = this.state
44
+    const { user, workspaceList, app, match, location, dispatch } = this.props
40 45
 
41 46
     if (Object.keys(app).length === 0) dispatch(getAppList()) // async but no need await
42 47
 
43 48
     let wsToLoad = null
44 49
     if (match.params.idws !== undefined) wsToLoad = match.params.idws
45
-    else if (workspaceList.length > 0) wsToLoad = workspaceList[0].id // load first ws if none specified
50
+
51
+    if (user.user_id !== -1 && workspaceList.length === 0) {
52
+      const fetchGetWorkspaceList = await dispatch(getWorkspaceList(user.user_id))
53
+
54
+      if (fetchGetWorkspaceList.status === 200) {
55
+        dispatch(updateWorkspaceListData(fetchGetWorkspaceList.json))
56
+        dispatch(setWorkspaceListIsOpenInSidebar(workspaceIdInUrl || fetchGetWorkspaceList.json[0].id, true))
57
+
58
+        if (match.params.idws === undefined && fetchGetWorkspaceList.json.length > 0) {
59
+          wsToLoad = fetchGetWorkspaceList.json[0].id // load first ws if none specified
60
+        }
61
+      }
62
+    }
46 63
 
47 64
     if (wsToLoad === null) return // ws already loaded
48 65
 
@@ -59,6 +76,17 @@ class WorkspaceContent extends React.Component {
59 76
     } else dispatch(newFlashMessage('Error while loading workspace', 'danger'))
60 77
   }
61 78
 
79
+  componentDidUpdate (prevProps, prevState) {
80
+    const { user, match, dispatch } = this.props
81
+
82
+    if (this.state.workspaceIdInUrl === null) return
83
+
84
+    const newWorkspaceId = parseInt(match.params.idws)
85
+    prevState.workspaceIdInUrl !== newWorkspaceId && this.setState({workspaceIdInUrl: newWorkspaceId})
86
+
87
+    if (user.id !== -1 && prevProps.user.id !== user.id) dispatch(getWorkspaceList(user.user_id, newWorkspaceId))
88
+  }
89
+
62 90
   handleClickContentItem = content => {
63 91
     this.props.history.push(`${PAGE.WORKSPACE.CONTENT(content.workspace_id, content.id)}${this.props.location.search}`)
64 92
     this.props.renderApp(this.props.app[content.type], this.props.user, {...content, workspace: this.props.workspace})
@@ -115,7 +143,7 @@ class WorkspaceContent extends React.Component {
115 143
     const filterWorkspaceContent = (contentList, filter) => filter.length === 0
116 144
       ? contentList
117 145
       : contentList.filter(c => c.type === 'folder' || filter.includes(c.type)) // keep unfiltered files and folders
118
-        .map(c => c.type !== 'folder' ? c : {...c, content: this.filterWorkspaceContent(c.content, filter)}) // recursively filter folder content
146
+        .map(c => c.type !== 'folder' ? c : {...c, content: filterWorkspaceContent(c.content, filter)}) // recursively filter folder content
119 147
     // .filter(c => c.type !== 'folder' || c.content.length > 0) // remove empty folder => 2018/05/21 - since we load only one lvl of content, don't remove empty folders
120 148
 
121 149
     const filteredWorkspaceContent = filterWorkspaceContent(workspace.content, workspace.filter)