Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import api from '../../../api/front/posts'
- import * as types from '../../mutations-types'
- export default {
- state: {
- posts : []
- },
- getters: {
- posts: state => state.posts,
- getLastCreated: state => {
- if(state.posts.length > 0)
- return state.posts[state.posts.length - 1].createdAt;
- else
- return null
- }
- },
- actions: {
- prependPost(store, post) {
- if(!post.replies) post.replies = [];
- store.commit(types.PREPEND_POST, post);
- },
- appendPost(store, post) {
- if(!post.replies) post.replies = [];
- store.commit(types.APPEND_POST, post);
- },
- appendReply(store, reply) {
- store.commit(types.APPEND_REPLY, reply);
- },
- addPost(store, ctx) {
- api.addPost(ctx.data, payload =>
- {
- if(payload.progress && !payload.data)
- ctx.progress = payload.progress;
- else if(!payload.progress && payload.data)
- {
- ctx.resetForm();
- if(!payload.data.replies)
- payload.data.replies = [];
- if(typeof payload.data.postId != 'undefined') {
- store.commit(types.APPEND_WALL_REPLY, payload.data);
- store.commit(types.APPEND_REPLY, payload.data);
- }
- else if(typeof payload.data.wallId != 'undefined') {
- store.commit(types.PREPEND_WALL_POST, payload.data);
- store.commit(types.PREPEND_POST, payload.data);
- }
- else {
- store.commit(types.PREPEND_POST, payload.data);
- }
- }
- });
- },
- setPosts(store, ctx) {
- api.getPosts({}, data => {
- ctx.loading = false;
- store.commit(types.SET_POSTS, data.posts);
- })
- },
- loadMorePosts(store, ctx) {
- if(ctx.since) {
- api.loadPosts(ctx.since, data => {
- ctx.loading = false;
- data.posts.forEach(post => {
- store.commit(types.APPEND_POST, post);
- })
- });
- }
- },
- deletePost(store, id) {
- api.deletePost(id, data => {
- if(typeof data != 'undefined')
- store.commit(types.DELETE_POST, id);
- });
- }
- },
- mutations: {
- [types.APPEND_REPLY](state, reply) {
- let post = state.posts.find(p => p.id == reply.postId);
- if(post) {
- post.nbReplies++;
- post.replies.push(reply);
- }
- },
- [types.PREPEND_POST](state, post) {
- state.posts.unshift(post);
- },
- [types.APPEND_POST](state, post) {
- state.posts.push(post);
- },
- [types.SET_POSTS](state, posts) {
- state.posts = posts;
- },
- [types.DELETE_POST](state, id) {
- state.posts = state.posts.filter(p => p.id != id);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement