index.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { useEffect, memo } from 'react'
  21. import { matchPath, useLocation } from 'react-router-dom'
  22. import { useDispatch, useSelector } from 'react-redux'
  23. import { useInjectReducer } from 'utils/injectReducer'
  24. import { useInjectSaga } from 'utils/injectSaga'
  25. import reducer from './reducer'
  26. import saga from './sagas'
  27. import organizationSaga from 'containers/Organizations/sagas'
  28. import organizationReducer from 'containers/Organizations/reducer'
  29. import { ProjectActions } from './actions'
  30. import { makeSelectCurrentProject } from './selectors'
  31. import { OrganizationActions } from 'containers/Organizations/actions'
  32. const { loadProjectRoles, loadOrganizationMembers } = OrganizationActions
  33. import { IRouteParams } from 'utils/types'
  34. const Project: React.FC<any> = (props) => {
  35. useInjectReducer({ key: 'project', reducer })
  36. useInjectSaga({ key: 'project', saga })
  37. useInjectReducer({key: 'organization', reducer: organizationReducer})
  38. useInjectSaga({key: 'organization', saga: organizationSaga})
  39. const { pathname } = useLocation()
  40. const dispatch = useDispatch()
  41. const currentProject = useSelector(makeSelectCurrentProject())
  42. // centralized handle for :projectId changing in router params
  43. // and update currentProject in redux
  44. useEffect(() => {
  45. const match = matchPath<IRouteParams>(pathname, {
  46. path: '/project/:projectId',
  47. exact: false,
  48. strict: false
  49. })
  50. const projectId = +match.params.projectId
  51. if (projectId) {
  52. dispatch(loadProjectRoles(projectId))
  53. }
  54. if (projectId && (!currentProject || +currentProject.id !== projectId)) {
  55. dispatch(ProjectActions.loadProjectDetail(projectId))
  56. }
  57. }, [pathname, currentProject])
  58. useEffect(() =>{
  59. if (currentProject) {
  60. const { orgId} = currentProject
  61. dispatch(loadOrganizationMembers(orgId))
  62. }
  63. }, [currentProject])
  64. useEffect(() => {
  65. // clear currentProject for not in router /project/:projectId when this project index component unmounted
  66. return () => {
  67. dispatch(ProjectActions.clearCurrentProject())
  68. }
  69. }, [])
  70. return <>{currentProject && props.children}</>
  71. }
  72. export default memo(Project)