thread.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import AjaxErrorHandler from '../../assets/js/errorHandler'
  2. const state = {
  3. thread: '',
  4. threadId: undefined,
  5. posts: [],
  6. reply: {
  7. username: '',
  8. id: null
  9. },
  10. editor: {
  11. show: false,
  12. value: ''
  13. },
  14. loadingPosts: false,
  15. nextURL: '',
  16. previousURL: ''
  17. }
  18. const getters = {
  19. sortedPosts (state) {
  20. return state.posts.sort((a, b) => {
  21. return new Date(a.createdAt) - new Date(b.createdAt)
  22. })
  23. }
  24. }
  25. const actions = {
  26. addPostAsync ({ state, commit, rootState }, vue) {
  27. var post = {
  28. content: state.editor.value,
  29. threadId: +vue.$route.params.id
  30. };
  31. if(state.reply.id) {
  32. post.replyingToId = state.reply.id;
  33. }
  34. vue.axios
  35. .post('/api/v1/post', post)
  36. .then(res => {
  37. commit('addPost', res.data);
  38. commit('addReplyBubble', res.data)
  39. commit('setThreadEditorValue', '');
  40. commit('setThreadEditorState', false);
  41. commit({
  42. type: 'setReply',
  43. username: '',
  44. id: ''
  45. });
  46. })
  47. .catch(AjaxErrorHandler(vue.$store))
  48. },
  49. loadInitialPostsAsync ({ state, commit, rootState }, vue) {
  50. let postId = vue.$route.params.post_id
  51. let apiURL = '/api/v1/thread/' + vue.$route.params.id
  52. if(postId) {
  53. apiURL += '?postId=' + postId
  54. }
  55. vue.axios
  56. .get(apiURL)
  57. .then(res => {
  58. commit('setThread', res.data)
  59. commit('setNextURL', res.data.meta.nextURL)
  60. commit('setPreviousURL', res.data.meta.previousURL)
  61. commit('setPosts', res.data.Posts)
  62. if(postId) {
  63. vue.highlightPost(+postId)
  64. }
  65. }).catch(AjaxErrorHandler(vue.$store))
  66. },
  67. loadPostsAsync ({ state, commit, rootState }, { vue, previous }) {
  68. let URL
  69. commit('setLoadingPostsState', true)
  70. if(previous) {
  71. URL = state.previousURL
  72. } else {
  73. URL = state.nextURL
  74. }
  75. if(URL === null) {
  76. commit('setLoadingPostsState', false)
  77. } else {
  78. vue.axios
  79. .get(URL)
  80. .then(res => {
  81. let currentPostsIds = state.posts.map(p => p.id)
  82. let filteredPosts =
  83. res.data.Posts.filter(p => !currentPostsIds.includes(p.id))
  84. if(previous) {
  85. commit('prependPosts', filteredPosts)
  86. commit('setPreviousURL', res.data.meta.previousURL)
  87. } else {
  88. commit('addPost', filteredPosts)
  89. commit('setNextURL', res.data.meta.nextURL)
  90. }
  91. commit('setLoadingPostsState', false)
  92. })
  93. .catch(AjaxErrorHandler(vue.$store))
  94. }
  95. }
  96. }
  97. const mutations = {
  98. setReply (state, payload) {
  99. state.reply.username = payload.username;
  100. state.reply.id = payload.id;
  101. },
  102. addPost (state, post) {
  103. if(Array.isArray(post)) {
  104. state.posts.push(...post)
  105. } else {
  106. state.posts.push(post)
  107. }
  108. },
  109. prependPosts (state, posts) {
  110. state.posts.unshift(...posts)
  111. },
  112. addReplyBubble (state, post) {
  113. let repliedToPost = {}, index
  114. if(post.replyId) {
  115. state.posts.forEach((p, i) => {
  116. if(p.id === post.replyId) {
  117. index = i
  118. repliedToPost = p
  119. }
  120. })
  121. repliedToPost.Replies.push(post)
  122. state.posts.splice(index, 1, repliedToPost)
  123. }
  124. },
  125. setThreadEditorValue (state, value) {
  126. state.editor.value = value
  127. },
  128. setThreadEditorState (state, value) {
  129. state.editor.show = value
  130. },
  131. setLoadingPostsState (state, value) {
  132. state.loadingPosts = value
  133. },
  134. setPosts (state, value) {
  135. state.posts = value
  136. },
  137. setThread (state, obj) {
  138. state.thread = obj.name
  139. state.threadId = obj.id
  140. },
  141. setNextURL (state, URL) {
  142. state.nextURL = URL
  143. },
  144. setPreviousURL (state, URL) {
  145. state.previousURL = URL
  146. }
  147. }
  148. export default {
  149. state,
  150. getters,
  151. actions,
  152. mutations
  153. }