index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {
  2. actionChangeSubscribe,
  3. actionGetFollowers,
  4. actionGetFollowing,
  5. } from '../../../actions/query/subscribeQuery'
  6. import { promiseWorker } from '../promise'
  7. import { put, takeEvery, call, select } from 'redux-saga/effects'
  8. import {
  9. actionChangeFollowingType,
  10. actionChangeFollowersType,
  11. } from '../../../actions/types/subscribeTypes'
  12. function* changeSubscribeWorker({ followId, checkFollowId }) {
  13. const {
  14. myData: {
  15. aboutMe: { _id, following },
  16. },
  17. } = yield select()
  18. const oldFollowing = checkFollowId
  19. ? {
  20. _id,
  21. following: [
  22. ...(following || [])
  23. .filter((item) => item._id !== followId)
  24. .map(({ _id }) => ({ _id })),
  25. ],
  26. }
  27. : {
  28. _id,
  29. following: [
  30. ...(following || []).map(({ _id }) => ({ _id })),
  31. { _id: followId },
  32. ],
  33. }
  34. yield call(promiseWorker, actionChangeSubscribe(oldFollowing))
  35. const updateUserFollowers = yield call(
  36. promiseWorker,
  37. actionGetFollowers(followId),
  38. )
  39. const updateMyFollowing = yield call(promiseWorker, actionGetFollowing(_id))
  40. if (updateMyFollowing)
  41. yield put(actionChangeFollowingType(updateMyFollowing?.following))
  42. if (updateUserFollowers)
  43. yield put(actionChangeFollowersType(updateUserFollowers?.followers))
  44. }
  45. export function* changeSubscribeWatcher() {
  46. yield takeEvery('CHANGE_SUBSCRIBE', changeSubscribeWorker)
  47. }