currentWorkspace.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {
  2. SET,
  3. APPEND,
  4. WORKSPACE_DETAIL,
  5. WORKSPACE_MEMBER_LIST,
  6. WORKSPACE_READ_STATUS_LIST,
  7. WORKSPACE_RECENT_ACTIVITY_LIST
  8. } from '../action-creator.sync.js'
  9. import { handleRouteFromApi } from '../helper.js'
  10. import { generateAvatarFromPublicName } from 'tracim_frontend_lib'
  11. const defaultWorkspace = {
  12. id: 0,
  13. slug: '',
  14. label: '',
  15. description: '',
  16. sidebarEntryList: [],
  17. memberList: [],
  18. recentActivityList: [],
  19. recentActivityForUserList: [],
  20. contentReadStatusList: []
  21. }
  22. export default function currentWorkspace (state = defaultWorkspace, action) {
  23. switch (action.type) {
  24. case `${SET}/${WORKSPACE_DETAIL}`:
  25. return {
  26. ...state,
  27. id: action.workspaceDetail.workspace_id,
  28. slug: action.workspaceDetail.slug,
  29. label: action.workspaceDetail.label,
  30. description: action.workspaceDetail.description,
  31. sidebarEntryList: action.workspaceDetail.sidebar_entries.map(sbe => ({
  32. slug: sbe.slug,
  33. route: handleRouteFromApi(sbe.route),
  34. faIcon: sbe.fa_icon,
  35. hexcolor: sbe.hexcolor,
  36. label: sbe.label
  37. }))
  38. }
  39. case `${SET}/${WORKSPACE_MEMBER_LIST}`:
  40. return {
  41. ...state,
  42. memberList: action.workspaceMemberList.map(m => ({
  43. id: m.user_id,
  44. publicName: m.user.public_name,
  45. avatarUrl: m.user.avatar_url
  46. ? m.user.avatar_url
  47. : m.user.public_name ? generateAvatarFromPublicName(m.user.public_name) : '',
  48. role: m.role,
  49. isActive: m.is_active
  50. }))
  51. }
  52. case `${SET}/${WORKSPACE_RECENT_ACTIVITY_LIST}`:
  53. return {
  54. ...state,
  55. recentActivityList: action.workspaceRecentActivityList.map(ra => ({
  56. id: ra.content_id,
  57. slug: ra.slug,
  58. label: ra.label,
  59. type: ra.content_type,
  60. idParent: ra.parent_id,
  61. showInUi: ra.show_in_ui,
  62. isArchived: ra.is_archived,
  63. isDeleted: ra.is_deleted,
  64. statusSlug: ra.status,
  65. subContentTypeSlug: ra.sub_content_types
  66. }))
  67. }
  68. case `${APPEND}/${WORKSPACE_RECENT_ACTIVITY_LIST}`:
  69. return {
  70. ...state,
  71. recentActivityList: [
  72. ...state.recentActivityList,
  73. ...action.workspaceRecentActivityList.map(ra => ({
  74. id: ra.content_id,
  75. slug: ra.slug,
  76. label: ra.label,
  77. type: ra.content_type,
  78. idParent: ra.parent_id,
  79. showInUi: ra.show_in_ui,
  80. isArchived: ra.is_archived,
  81. isDeleted: ra.is_deleted,
  82. statusSlug: ra.status,
  83. subContentTypeSlug: ra.sub_content_types
  84. }))
  85. ]
  86. }
  87. case `${SET}/${WORKSPACE_READ_STATUS_LIST}`:
  88. return {
  89. ...state,
  90. contentReadStatusList: action.workspaceReadStatusList
  91. .filter(content => content.read_by_user)
  92. .map(content => content.content_id)
  93. }
  94. default:
  95. return state
  96. }
  97. }