checkShareDownloadPermission.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import React from 'react'
  2. import { IProject } from 'containers/Projects/types'
  3. export default function<T> (project?: IProject, type?: string) {
  4. return (WrapperComponent) => {
  5. class ShareDownloadPermission extends React.PureComponent<T, {}> {
  6. private getPermissionByCurrentProject = () => {
  7. let permission = ''
  8. if (project) {
  9. const projectPermission = project.permission
  10. for (const attr in projectPermission) {
  11. if (`${type}Permission` === attr) {
  12. permission = projectPermission[attr]
  13. break
  14. }
  15. }
  16. }
  17. return permission
  18. }
  19. private computePermission = () => {
  20. const permission = this.getPermissionByCurrentProject()
  21. const defaultComponent = <span/>
  22. if (!project) {
  23. return defaultComponent
  24. }
  25. switch (Number(permission)) {
  26. case 0:
  27. return defaultComponent
  28. case 1:
  29. return <WrapperComponent {...this.props}>{this.props.children}</WrapperComponent>
  30. default:
  31. return defaultComponent
  32. }
  33. }
  34. public render () {
  35. const result = this.computePermission()
  36. return result
  37. }
  38. }
  39. return ShareDownloadPermission
  40. }
  41. }