reducer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {fromJS} from 'immutable';
  2. import * as constants from './constants';
  3. import {getrand} from '../../../lib/public'
  4. const defaultState = fromJS({
  5. featureList: [],
  6. blogList: [],
  7. articlePage: 1,
  8. thumbList: [
  9. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(44).jpg'},
  10. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(42).jpg'},
  11. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(41).jpg'},
  12. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(3).jpg'},
  13. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(28).jpg'},
  14. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(43).jpg'},
  15. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(47).jpg'},
  16. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(48).jpg'},
  17. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(49).jpg'},
  18. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(55).jpg'},
  19. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(56).jpg'},
  20. {img: 'https://cos.nosum.cn/nosum/blog/1/nosum-blog-1%20(57).jpg'}
  21. ],
  22. finished: false,
  23. loading: true,
  24. isList: false,
  25. socialList:[]
  26. });
  27. const setFeature = (state, action) => {
  28. return state.merge({
  29. featureList: action.data
  30. })
  31. };
  32. const list = (thumbList, data) => {
  33. const list = data.toJS();
  34. const Img = thumbList.toJS();
  35. let arr = [];
  36. for (let i = 0; i < list.length; i++) {
  37. arr.push({
  38. id: list[i].id,
  39. title: list[i].title,
  40. thumbnail: list[i].thumbnail || Img[getrand(0, Img.length - 1)].img,
  41. comments: list[i].comments,
  42. status: list[i].status,
  43. summary: list[i].summary,
  44. views: list[i].views,
  45. createTime: list[i].createTime,
  46. syncStatus: list[i].syncStatus,
  47. author: list[i].author,
  48. categoryName: list[i].categoryName
  49. })
  50. }
  51. return arr
  52. };
  53. const setBlogList = (state, action) => {
  54. const arr = list(state.get('thumbList'), action.data);
  55. return action.override ? state.merge({
  56. blogList: action.override ? fromJS(arr) : state.get('blogList').concat(fromJS(arr)),
  57. articlePage: action.nextPage,
  58. finished: false,
  59. loading: false,
  60. isList: true
  61. }) : state.merge({
  62. blogList: action.override ? fromJS(arr) : state.get('blogList').concat(fromJS(arr)),
  63. articlePage: action.nextPage,
  64. loading: false,
  65. isList: true
  66. })
  67. };
  68. export default (state = defaultState, action) => {
  69. switch (action.type) {
  70. case constants.GET_FEATURE:
  71. return setFeature(state, action);
  72. case constants.GET_BLOGLIST:
  73. return setBlogList(state, action);
  74. case constants.SET_FINISHED:
  75. return state.set('finished', true);
  76. case constants.LOADING_TRUE:
  77. return state.set('loading', true);
  78. case constants.GET_SOCIAL_LIST:
  79. return state.merge({
  80. socialList: action.data
  81. });
  82. default:
  83. return state;
  84. }
  85. }