mutations.js 1.55 KB
import * as Types from './types';

export default {
  // 开始调用接口,方式重复调用
  [Types.FETCH_ARTICLE_LIST_REQUEST](state) {
    state.isFetchingArticleList = true;
  },

  // 调用失败
  [Types.FETCH_ARTICLE_LIST_FAILED](state) {
    state.isFetchingArticleList = false;
  },

  // 调用成功
  [Types.FETCH_ARTICLE_LIST_SUCCESS](state, {result, reload}) {
    console.log('reload');
    state.isFetchingArticleList = false;
    if (reload) {
      state.articleList = [];
      state.articleListInfo = {};
    }

    if (state.articleList.length === 0) {
      state.articleList = result.data.list;
    } else {
      if (result.data.pageNo > state.articleListInfo.pageNo) {
        state.articleList = state.articleList.concat(result.data.list);
      }
    }
    delete result.data.list;
    state.articleListInfo = result.data;
  },

  // 更新点赞
  [Types.UPDATE_ARTICLE_PRAISE](state, {articleId, status, index}) {
    let article = null;

    if (index) {
      article = state.articleList[index];
    } else {
      article = state.articleList.find(item => +item.articleId === +articleId);
    }

    if (article && article.articleId === parseInt(articleId, 10)) {
      if (article.praiseCount === 0 && status === 0) {
        article.praiseCount += 1;
        article.hasPraised = 'Y';
      } else if (article.praiseCount > 0) {
        if (status === 0) {
          article.praiseCount += 1;
          article.hasPraised = 'Y';
        } else {
          article.praiseCount -= 1;
          article.hasPraised = 'N';
        }
      }
    }
  }
};