You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.0 KiB

  1. import CommentService from '@/services/comment.service'
  2. import UserService from '@/services/user.service'
  3. export const state = () => ({
  4. comments: [],
  5. selectedComments: [],
  6. totalComments: 0,
  7. userId: -1,
  8. loading: false
  9. })
  10. export const getters = {
  11. isCommentSelected(state) {
  12. return state.selectedComments.length > 0
  13. }
  14. }
  15. export const mutations = {
  16. setCommentList(state, payload) {
  17. state.comments = payload
  18. },
  19. addComment(state, payload) {
  20. state.comments.unshift(payload)
  21. },
  22. updateComment(state, payload) {
  23. const item = state.comments.find(item => item.id === payload.id)
  24. Object.assign(item, payload)
  25. },
  26. deleteComment(state, commentId) {
  27. state.comments = state.comments.filter(item => item.id !== commentId)
  28. },
  29. updateSelectedComments(state, selected) {
  30. state.selectedComments = selected
  31. },
  32. resetSelectedComments(state) {
  33. state.selectedComments = []
  34. },
  35. setTotalComments(state, payload) {
  36. state.totalComments = payload
  37. },
  38. setUserId(state, payload) {
  39. state.userId = payload.id
  40. },
  41. setLoading(state, payload) {
  42. state.loading = payload
  43. }
  44. }
  45. export const actions = {
  46. getCommentList({ commit, state }, payload) {
  47. commit('setLoading', true)
  48. return CommentService.getCommentList(payload.projectId, payload.docId)
  49. .then((response) => {
  50. commit('setCommentList', response.data)
  51. })
  52. .catch((error) => {
  53. alert(error)
  54. })
  55. .finally(() => {
  56. commit('setLoading', false)
  57. })
  58. },
  59. getProjectCommentList({ commit, state }, payload) {
  60. commit('setLoading', true)
  61. return CommentService.getProjectCommentList(payload.projectId)
  62. .then((response) => {
  63. commit('setCommentList', response.data)
  64. commit('setTotalComments', response.data.count)
  65. })
  66. .catch((error) => {
  67. alert(error)
  68. })
  69. .finally(() => {
  70. commit('setLoading', false)
  71. })
  72. },
  73. addComment({ commit, state }, payload) {
  74. CommentService.addComment(payload.projectId, payload.docId, payload)
  75. .then((response) => {
  76. commit('addComment', response.data)
  77. })
  78. .catch((error) => {
  79. alert(error)
  80. })
  81. },
  82. updateComment({ commit, state }, payload) {
  83. CommentService.updateComment(payload.projectId, payload.docId, payload.commentId, payload)
  84. .then((response) => {
  85. commit('updateComment', response.data)
  86. })
  87. .catch((error) => {
  88. alert(error)
  89. })
  90. },
  91. deleteComment({ commit, state }, payload) {
  92. for (const comment of state.selectedComments) {
  93. CommentService.deleteComment(payload.projectId, comment.document, comment.id)
  94. .then((response) => {
  95. commit('deleteComment', comment.id)
  96. })
  97. .catch((error) => {
  98. alert(error)
  99. })
  100. commit('resetSelectedComments')
  101. }
  102. },
  103. getMyUserId({ commit, state }) {
  104. UserService.getMe()
  105. .then((response) => {
  106. commit('setUserId', response.data)
  107. })
  108. .catch((error) => {
  109. alert(error)
  110. })
  111. }
  112. }