index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { promiseWorker } from '../promise'
  2. import { put, takeEvery, call, select } from 'redux-saga/effects'
  3. import {
  4. actionProfilePageDataType,
  5. actionUpdateAvatarType,
  6. } from '../../../actions/types/myDataTypes'
  7. import {
  8. actionAboutMe,
  9. actionUserUpsert,
  10. actionAvatar,
  11. actionGetAvatar,
  12. } from '../../../actions/query/aboutMeQuery'
  13. import { fullPageAboutUserWorker } from '../userProfile'
  14. export function* fullProfilePageWorker() {
  15. const { auth } = yield select()
  16. if (auth?.payload?.sub?.id) {
  17. const aboutMe = yield call(
  18. promiseWorker,
  19. actionAboutMe(auth?.payload?.sub.id),
  20. )
  21. if (aboutMe) {
  22. yield put(actionProfilePageDataType(aboutMe))
  23. }
  24. }
  25. }
  26. export function* fullProfilePageWatcher() {
  27. yield takeEvery('FULLPROFILE_PAGE', fullProfilePageWorker)
  28. }
  29. function* userUpdateWorker({ user }) {
  30. const {
  31. myData: {
  32. aboutMe: { _id },
  33. },
  34. } = yield select()
  35. const userUpsert = yield call(promiseWorker, actionUserUpsert(user, _id))
  36. if (userUpsert) {
  37. yield call(fullPageAboutUserWorker, { _id })
  38. yield call(fullProfilePageWorker)
  39. }
  40. }
  41. export function* userUpdateWatcher() {
  42. yield takeEvery('USER_UPDATE', userUpdateWorker)
  43. }
  44. function* setAvatarWorker({ file }) {
  45. const {
  46. myData: {
  47. aboutMe: { _id },
  48. },
  49. } = yield select()
  50. const setAvatar = yield call(promiseWorker, actionAvatar(file, _id))
  51. const { avatar } = yield call(promiseWorker, actionGetAvatar(_id))
  52. if (setAvatar) {
  53. yield call(fullPageAboutUserWorker, { _id })
  54. yield put(actionUpdateAvatarType(avatar))
  55. }
  56. }
  57. export function* setAvatarWatcher() {
  58. yield takeEvery('SET_AVATAR', setAvatarWorker)
  59. }