feedReducer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { actionCatAll } from "../actions/actionCatAll";
  2. import { actionGoodsFind } from "../actions/actionGoodsFind";
  3. import { actionCatsFind } from "../actions/actionCatsFind";
  4. import { actionGoodsAll } from "../actions/actionGoodsAll";
  5. import { actionOrdersAll } from "../actions/actionOrdersAll";
  6. import { actionOrdersFind } from "../actions/actionOrdersFind";
  7. import { actionCategoryGoods } from "../actions/actionCategoryGoods";
  8. import { actionUsersFind } from "../actions/actionUsersFind";
  9. import { actionUsersAll } from "../actions/actionUsersAll";
  10. import { put, takeLatest, takeLeading } from "redux-saga/effects";
  11. function feedReducer(state = { payload: [] }, { type, payload = [] }) {
  12. if (type === "FEED_ADD") {
  13. return {
  14. ...state,
  15. payload: [...new Map([...state["payload"], ...payload].map((item) => [item["_id"], item])).values()],
  16. };
  17. }
  18. if (type === "FEED_CLEAR") {
  19. return { payload: [] };
  20. }
  21. return state || { payload: [] };
  22. }
  23. const actionFeedAdd = (payload) => ({ type: "FEED_ADD", payload });
  24. const actionFeedClear = () => ({ type: "FEED_CLEAR" });
  25. const actionFeedGoods = ({ skip = 0, orderBy = "_id" } = {}) => ({
  26. type: "FEED_GOODS",
  27. payload: { skip, orderBy },
  28. });
  29. function* feedGoodsWorker(action) {
  30. const { skip = 0, orderBy = "_id" } = action.payload || {};
  31. yield put(actionGoodsAll({ skip, limit: 1, promiseName: "feedGoodsAll", orderBy }));
  32. }
  33. const actionFeedCategoryGoods = ({ skip = 0, orderBy = "_id", category } = {}) => ({
  34. type: "FEED_CATEGORY_GOODS",
  35. payload: { skip, orderBy, category },
  36. });
  37. function* feedCategoryGoodsWorker(action) {
  38. const { skip = 0, orderBy = "_id", category } = action.payload || {};
  39. yield put(actionCategoryGoods({ skip, limit: 1, promiseName: "feedCategoryGoods", orderBy, category }));
  40. }
  41. const actionFeedGoodsFind = ({ skip = 0, text = "", orderBy = "_id" } = {}) => ({
  42. type: "FEED_GOODS_FIND",
  43. payload: { skip, text, orderBy },
  44. });
  45. function* feedGoodsFindWorker(action) {
  46. const { skip = 0, text = "", orderBy = "_id" } = action.payload || {};
  47. yield put(actionGoodsFind({ skip, limit: 1, promiseName: "feedGoodsFind", text, orderBy }));
  48. }
  49. const actionFeedCatsFind =
  50. ({ skip = 0, text = "", orderBy = "_id" }) =>
  51. async (dispatch, getState) => {
  52. await dispatch(actionCatsFind({ skip, promiseName: "feedCatsFind", text, limit: 1, orderBy }));
  53. };
  54. const actionFeedCats = ({ skip = 0, orderBy = "_id" } = {}) => ({
  55. type: "FEED_CATS",
  56. payload: { skip, orderBy },
  57. });
  58. function* feedCatsWorker(action) {
  59. const { skip = 0, orderBy = "_id" } = action.payload || {};
  60. yield put(actionCatAll({ promiseName: "feedCatAll", skip, limit: 1, orderBy }));
  61. }
  62. const actionFeedOrders =
  63. ({ skip = 0, orderBy = "_id", status = 0 }) =>
  64. async (dispatch, getState) => {
  65. await dispatch(actionOrdersAll({ skip, limit: 1, promiseName: "feedOrdersAll", orderBy, status }));
  66. };
  67. const actionFeedOrdersFind =
  68. ({ skip = 0, text = "", orderBy = "_id", status = "0" }) =>
  69. async (dispatch, getState) => {
  70. await dispatch(actionOrdersFind({ skip, limit: 1, promiseName: "feedOrdersFind", text, orderBy, status }));
  71. };
  72. const actionFeedUsersFind =
  73. ({ skip = 0, text = "", orderBy = "_id" }) =>
  74. async (dispatch, getState) => {
  75. await dispatch(actionUsersFind({ skip, promiseName: "feedUsersFind", text, limit: 1, orderBy }));
  76. };
  77. const actionFeedUsers =
  78. ({ skip = 0, orderBy = "_id" }) =>
  79. async (dispatch, getState) => {
  80. await dispatch(actionUsersAll({ promiseName: "feedUsersAll", skip, limit: 1, orderBy }));
  81. };
  82. export {
  83. actionFeedCats,
  84. actionFeedCatsFind,
  85. actionFeedGoods,
  86. actionFeedClear,
  87. actionFeedAdd,
  88. actionFeedGoodsFind,
  89. feedReducer,
  90. actionFeedOrders,
  91. actionFeedOrdersFind,
  92. actionFeedCategoryGoods,
  93. actionFeedUsers,
  94. actionFeedUsersFind,
  95. };
  96. export function* feedWatcher() {
  97. yield takeLeading("FEED_CATEGORY_GOODS", feedCategoryGoodsWorker);
  98. yield takeLeading("FEED_GOODS_FIND", feedGoodsFindWorker);
  99. yield takeLeading("FEED_GOODS", feedGoodsWorker);
  100. yield takeLeading("FEED_CATS", feedCatsWorker);
  101. }