123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import AjaxErrorHandler from '../../assets/js/errorHandler'
- const state = {
- thread: '',
- threadId: undefined,
- posts: [],
- reply: {
- username: '',
- id: null
- },
- editor: {
- show: false,
- value: ''
- },
- loadingPosts: false,
- nextURL: '',
- previousURL: ''
- }
- const getters = {
- sortedPosts (state) {
- return state.posts.sort((a, b) => {
- return new Date(a.createdAt) - new Date(b.createdAt)
- })
- }
- }
- const actions = {
- addPostAsync ({ state, commit, rootState }, vue) {
- var post = {
- content: state.editor.value,
- threadId: +vue.$route.params.id
- };
- if(state.reply.id) {
- post.replyingToId = state.reply.id;
- }
- vue.axios
- .post('/api/v1/post', post)
- .then(res => {
- commit('addPost', res.data);
- commit('addReplyBubble', res.data)
- commit('setThreadEditorValue', '');
- commit('setThreadEditorState', false);
- commit({
- type: 'setReply',
- username: '',
- id: ''
- });
- })
- .catch(AjaxErrorHandler(vue.$store))
- },
- loadInitialPostsAsync ({ state, commit, rootState }, vue) {
- let postId = vue.$route.params.post_id
- let apiURL = '/api/v1/thread/' + vue.$route.params.id
- if(postId) {
- apiURL += '?postId=' + postId
- }
- vue.axios
- .get(apiURL)
- .then(res => {
- commit('setThread', res.data)
- commit('setNextURL', res.data.meta.nextURL)
- commit('setPreviousURL', res.data.meta.previousURL)
- commit('setPosts', res.data.Posts)
- if(postId) {
- vue.highlightPost(+postId)
- }
- }).catch(AjaxErrorHandler(vue.$store))
- },
- loadPostsAsync ({ state, commit, rootState }, { vue, previous }) {
- let URL
- commit('setLoadingPostsState', true)
- if(previous) {
- URL = state.previousURL
- } else {
- URL = state.nextURL
- }
- if(URL === null) {
- commit('setLoadingPostsState', false)
- } else {
- vue.axios
- .get(URL)
- .then(res => {
- let currentPostsIds = state.posts.map(p => p.id)
- let filteredPosts =
- res.data.Posts.filter(p => !currentPostsIds.includes(p.id))
- if(previous) {
- commit('prependPosts', filteredPosts)
- commit('setPreviousURL', res.data.meta.previousURL)
- } else {
- commit('addPost', filteredPosts)
- commit('setNextURL', res.data.meta.nextURL)
- }
- commit('setLoadingPostsState', false)
- })
- .catch(AjaxErrorHandler(vue.$store))
- }
- }
- }
- const mutations = {
- setReply (state, payload) {
- state.reply.username = payload.username;
- state.reply.id = payload.id;
- },
- addPost (state, post) {
- if(Array.isArray(post)) {
- state.posts.push(...post)
- } else {
- state.posts.push(post)
- }
- },
- prependPosts (state, posts) {
- state.posts.unshift(...posts)
- },
- addReplyBubble (state, post) {
- let repliedToPost = {}, index
- if(post.replyId) {
- state.posts.forEach((p, i) => {
- if(p.id === post.replyId) {
- index = i
- repliedToPost = p
- }
- })
- repliedToPost.Replies.push(post)
- state.posts.splice(index, 1, repliedToPost)
- }
- },
- setThreadEditorValue (state, value) {
- state.editor.value = value
- },
- setThreadEditorState (state, value) {
- state.editor.show = value
- },
- setLoadingPostsState (state, value) {
- state.loadingPosts = value
- },
- setPosts (state, value) {
- state.posts = value
- },
- setThread (state, obj) {
- state.thread = obj.name
- state.threadId = obj.id
- },
- setNextURL (state, URL) {
- state.nextURL = URL
- },
- setPreviousURL (state, URL) {
- state.previousURL = URL
- }
- }
- export default {
- state,
- getters,
- actions,
- mutations
- }
|