mutations.js 4.53 KB
import * as Types from './types';
import {getArticleImageSize} from 'utils/image-handler';
import { get, first } from 'lodash';

export default {
  [Types.FETCH_ARTICLE_LIST_REQUEST](state, {refresh}) {
    state.fetchArticleList = true;
    if (refresh) {
      state.articleList = [];
    }
  },
  [Types.FETCH_ARTICLE_LIST_SUCCESS](state, {data}) {
    state.fetchArticleList = false;
    data.forEach(item => {
      get(item, 'productList', []).forEach(product => {
        product.favorite = false;
      });
      item.blockIndex = 1;
      item.lazy = true;
    });
    state.articleList = state.articleList.concat(data);

    state.articleList.forEach((item, index) => {
      const imageBlocks = get(item, 'blockList', []).filter(block => block.templateKey === 'image');
      const firstImage = first(imageBlocks);

      if (firstImage) {
        let {width, height} = getArticleImageSize(firstImage);

        firstImage.width = width;
        firstImage.height = height;
      }
      item.index = index;
    });
  },
  [Types.FETCH_ARTICLE_LIST_FAILD](state) {
    state.fetchArticleList = false;
  },
  [Types.FETCH_ARTICLE_PRODUCT_SUCCESS](state, {articles, favs, articleProductType}) {
    articles.forEach(article => {
      if (article.articleProductType === articleProductType) {
        get(article, 'productList', []).forEach(product => {
          const find = favs.find(f => f.id === product.productId);

          if (find) {
            product.favorite = find.favorite;
          }
        });
      }
    });
  },
  [Types.FETCH_GUANG_SUCCESS](state, data) {
    state.articleDetail = data;
  },
  [Types.CHANGE_AUTHOR_FOLLOW](state, {authorUid, follow}) {
    state.articleList.forEach(article => {
      if (article.authorUid === authorUid) {
        article.hasAttention = follow ? 'Y' : 'N';
      }
    });
  },
  [Types.CHANGE_ARTICLE_LIST_SLIDE](state, {articleId, index}) {
    state.articleList.forEach(article => {
      if (article.articleId === articleId) {
        article.blockIndex = index;
        return;
      }
    });
  },
  [Types.CHANGE_ARTICLE_LIST_SCROLL](state, {articleId}) {
    let inx;

    state.articleList.forEach((article, index) => {
      if (article.articleId === articleId) {
        inx = index;
        article.lazy = false;
      } else if (inx && index - inx < 3) {
        article.lazy = false;
      }
    });
  },
  [Types.CHANGE_AUTHOR_TOPIC_FOLLOW](state, {authorUid, follow}) {
    state.articleListByTopic.forEach(article => {
      if (article.authorUid === authorUid) {
        article.hasAttention = follow ? 'Y' : 'N';
      }
    });
  },
  [Types.CHANGE_ARTICLE_TOPIC_SLIDE](state, {articleId, index}) {
    state.articleListByTopic.forEach(article => {
      if (article.articleId === articleId) {
        article.blockIndex = index;
        return;
      }
    });
  },
  [Types.FETCH_ARTICLE_LIST_UPDATE](state, {data}) {
    const find = state.articleList.find(article => article.articleId === data.articleId);

    if (find) {
      find.commentCount = data.commentCount;
      find.comments = data.comments;
    }
  },
  [Types.FETCH_ARTICLE_TOPIC_UPDATE](state, {data}) {

  },
  [Types.CHANGE_ARTICLE_TOPIC_SCROLL](state, {articleId}) {
    let inx;

    state.articleListByTopic.forEach((article, index) => {
      if (article.articleId === articleId) {
        inx = index;
        article.lazy = false;
      } else if (inx && index - inx < 3) {
        article.lazy = false;
      }
    });
  },
  [Types.FETCH_ARTICLE_TOPIC_REQUEST](state, {page}) {
    state.fetchArticleListByTopic = true;
    if (page === 1) {
      state.articleLastedTimeByTopic = 0;
      state.articleListByTopic = [];
    }
  },
  [Types.FETCH_ARTICLE_TOPIC_SUCCESS](state, {data}) {
    state.fetchArticleListByTopic = false;
    state.articleLastedTimeByTopic = data.lastedTime;
    data.detailList.forEach(item => {
      get(item, 'productList', []).forEach(product => {
        product.favorite = false;
      });
      item.blockIndex = 1;
      item.lazy = true;
    });
    state.articleListByTopic = state.articleListByTopic.concat(data.detailList);

    state.articleListByTopic.forEach((item, index) => {
      const imageBlocks = get(item, 'blockList', []).filter(block => block.templateKey === 'image');
      const firstImage = first(imageBlocks);

      if (firstImage) {
        let {width, height} = getArticleImageSize(firstImage);

        firstImage.width = width;
        firstImage.height = height;
      }
      item.index = index;
    });
  },
  [Types.FETCH_ARTICLE_TOPIC_FAILD](state) {
    state.fetchArticleListByTopic = false;
  }
};