checkMenuPermission.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React from 'react'
  2. import { IOrganization } from 'containers/Organizations/types'
  3. import { IProject } from 'containers/Projects/types'
  4. interface IModulePermissionProps {
  5. size?: string
  6. type?: string
  7. icon?: string
  8. onClick?: any
  9. className?: string
  10. permission?: IOrganization
  11. indexRoute?: string
  12. active?: any
  13. projectId?: string
  14. }
  15. export default (project?: IProject, item?: any) => (WrapperComponent) => {
  16. class MenuPermission extends React.PureComponent<IModulePermissionProps, {}> {
  17. private getPermissionByCurrentProject = () => {
  18. let permission = ''
  19. if (project) {
  20. const projectPermission = project.permission
  21. for (const attr in projectPermission) {
  22. if (attr) {
  23. const pStr = attr.slice(0, -10)
  24. if (pStr === item) {
  25. permission = projectPermission [attr]
  26. }
  27. }
  28. }
  29. }
  30. return permission
  31. }
  32. private computePermission = () => {
  33. const permission = this.getPermissionByCurrentProject()
  34. const defaultComponent = <span/>
  35. if (!project) {
  36. return defaultComponent
  37. }
  38. if (permission) {
  39. switch (Number(permission)) {
  40. case 0:
  41. return defaultComponent
  42. default:
  43. return <WrapperComponent {...this.props}>{this.props.children}</WrapperComponent>
  44. }
  45. } else {
  46. return defaultComponent
  47. }
  48. }
  49. public render () {
  50. const result = this.computePermission()
  51. return result
  52. }
  53. }
  54. return MenuPermission
  55. }