feedReducer.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 } 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 =
  26. ({ skip = 0, orderBy = "_id" }) =>
  27. async (dispatch, getState) => {
  28. await dispatch(actionGoodsAll({ skip, limit: 1, promiseName: "feedGoodsAll", orderBy }));
  29. };
  30. const actionFeedCategoryGoods = ({ skip = 0, orderBy = "_id", category } = {}) => ({
  31. type: "FEED_CATEGORY_GOODS",
  32. payload: { skip, orderBy, category },
  33. });
  34. function* feedCategoryGoodsWorker(action) {
  35. const { skip = 0, orderBy = "_id", category } = action.payload || {};
  36. yield put(actionCategoryGoods({ skip, limit: 1, promiseName: "feedCategoryGoods", orderBy, category }));
  37. }
  38. const actionFeedGoodsFind =
  39. ({ skip = 0, text = "", orderBy = "_id" }) =>
  40. async (dispatch, getState) => {
  41. await dispatch(actionGoodsFind({ skip, limit: 1, promiseName: "feedGoodsFind", text, orderBy }));
  42. };
  43. const actionFeedCatsFind =
  44. ({ skip = 0, text = "", orderBy = "_id" }) =>
  45. async (dispatch, getState) => {
  46. await dispatch(actionCatsFind({ skip, promiseName: "feedCatsFind", text, limit: 1, orderBy }));
  47. };
  48. const actionFeedCats =
  49. ({ skip = 0, orderBy = "_id" }) =>
  50. async (dispatch, getState) => {
  51. await dispatch(actionCatAll({ promiseName: "feedCatAll", skip, limit: 1, orderBy }));
  52. };
  53. const actionFeedOrders =
  54. ({ skip = 0, orderBy = "_id", status = 0 }) =>
  55. async (dispatch, getState) => {
  56. await dispatch(actionOrdersAll({ skip, limit: 1, promiseName: "feedOrdersAll", orderBy, status }));
  57. };
  58. const actionFeedOrdersFind =
  59. ({ skip = 0, text = "", orderBy = "_id", status = "0" }) =>
  60. async (dispatch, getState) => {
  61. await dispatch(actionOrdersFind({ skip, limit: 1, promiseName: "feedOrdersFind", text, orderBy, status }));
  62. };
  63. const actionFeedUsersFind =
  64. ({ skip = 0, text = "", orderBy = "_id" }) =>
  65. async (dispatch, getState) => {
  66. await dispatch(actionUsersFind({ skip, promiseName: "feedUsersFind", text, limit: 1, orderBy }));
  67. };
  68. const actionFeedUsers =
  69. ({ skip = 0, orderBy = "_id" }) =>
  70. async (dispatch, getState) => {
  71. await dispatch(actionUsersAll({ promiseName: "feedUsersAll", skip, limit: 1, orderBy }));
  72. };
  73. export {
  74. actionFeedCats,
  75. actionFeedCatsFind,
  76. actionFeedGoods,
  77. actionFeedClear,
  78. actionFeedAdd,
  79. actionFeedGoodsFind,
  80. feedReducer,
  81. actionFeedOrders,
  82. actionFeedOrdersFind,
  83. actionFeedCategoryGoods,
  84. actionFeedUsers,
  85. actionFeedUsersFind,
  86. };
  87. export function* feedWatcher() {
  88. yield takeLatest("FEED_CATEGORY_GOODS", feedCategoryGoodsWorker);
  89. }