mutations.js 1.28 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) {
    state.isFetchingArticleList = false;
    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 = state.articleList[index];

    if (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';
        }
      }
    }
  }
};