index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import {
  2. actionAboutMe,
  3. actionAllPostsUser,
  4. actionAboutUser,
  5. actionPostsFeed,
  6. actionPostsFeedCount,
  7. actionOnePost,
  8. actionAddComment,
  9. actionAddLike,
  10. actionDeleteLike,
  11. actionPostsCount,
  12. actionAuthLogout,
  13. actionAllClearPromise,
  14. actionLogin,
  15. actionAuthLogin,
  16. } from '../../actions'
  17. import { history } from '../../helpers'
  18. import{actionClearDataUserType} from '../reducers/profileUserPage/profileUserReducer'
  19. import { actionProfilePageDataType } from '../reducers/profileData/profileReducer'
  20. import { actionFullAllGetPosts } from '../../actions'
  21. import {
  22. actionAddLikePostInTape,
  23. actionDeleteLikePostInTape,
  24. actionAddCommentPostInTape,
  25. actionClearFeedPosts,
  26. actionFeedType
  27. } from '../reducers/feed/feedReducer'
  28. import { actionProfilePageDataTypeUser } from '../reducers/profileUserPage/profileUserReducer'
  29. import { actionRemoveDataAboutMe } from '../reducers/profileData/profileReducer'
  30. import { all, put,take, fork, takeEvery, takeLatest, takeLeading, select,call } from 'redux-saga/effects'; //
  31. import {actionPending,actionFulfilled,actionRejected} from '../../actions'
  32. //promise
  33. export function* promiseWorker(action){ //это типа actionPromise который thunk
  34. const {name, promise} = action
  35. yield put(actionPending(name)) //это как dispatch
  36. try {
  37. let data = yield promise //а это как await
  38. yield put(actionFulfilled(name, data))
  39. return data //а этот результать можно забрать через yield call(promiseWorker, actionPromise(......) /*какой-то объект action*/)
  40. }
  41. catch (error) {
  42. yield put(actionRejected(name, error))
  43. }
  44. }
  45. export function* promiseWatcher(){
  46. yield takeEvery('PROMISE_START', promiseWorker)
  47. }
  48. //login
  49. function* loginWorker({login, password}){ //обработчик экшона FULL_LOGIN
  50. let token = yield call(promiseWorker,actionLogin(login, password)) //dispatch(actionLogin(login, password));
  51. if (token) {
  52. yield put(actionAuthLogin(token));
  53. }
  54. }
  55. export function* loginWatcher() {
  56. yield takeEvery("FULL_LOGIN", loginWorker)
  57. }
  58. //profile page about me
  59. export const actionFullProfilePage = () =>
  60. ({
  61. type:"FULLPROFILE_PAGE",
  62. })
  63. function* fullProfilePageWorker() {
  64. const { auth } = yield select()
  65. console.log('auth', auth)
  66. if (auth?.payload?.sub?.id) {
  67. const aboutMe = yield call(promiseWorker, actionAboutMe(auth?.payload?.sub.id))
  68. console.log('aboutMe in worker', aboutMe)
  69. if (aboutMe) {
  70. yield put(actionProfilePageDataType(aboutMe))
  71. }
  72. }
  73. }
  74. export function* fullProfilePageWatcher() {
  75. yield takeEvery("FULLPROFILE_PAGE", fullProfilePageWorker)
  76. }
  77. //aboutUser
  78. //full profile user
  79. export const actionFullProfilePageUser = (_id) =>
  80. ({ type: "USER_PAGE", _id })
  81. function* fullPageAboutUserWorker({ _id }) {
  82. console.log('_id ', _id)
  83. const aboutUser = yield call(promiseWorker, actionAboutUser(_id))
  84. console.log('about user', aboutUser)
  85. const allPosts = yield call(promiseWorker, actionAllPostsUser(_id))
  86. if (aboutUser && allPosts) {
  87. yield put(actionProfilePageDataTypeUser(aboutUser, allPosts))
  88. }
  89. }
  90. export function* fullPageAboutUserWatcher() {
  91. yield takeLeading("USER_PAGE", fullPageAboutUserWorker)
  92. }
  93. function* feedWorker() {
  94. const {
  95. feed: { postsFeed, postsFeedCount },
  96. profileData: { aboutMe},
  97. } = yield select()
  98. let myFollowing = aboutMe?.following && aboutMe?.following?.map(({ _id }) => _id)
  99. const myId = (yield select()).auth?.payload?.sub?.id
  100. if (!aboutMe) {
  101. yield call(fullProfilePageWorker, actionFullProfilePage())
  102. }
  103. myFollowing = (yield select()).profileData.aboutMe?.following &&
  104. (yield select()).profileData.aboutMe?.following?.map(({ _id }) => _id)
  105. // console.log('myFollowing after if', myFollowing)
  106. if (postsFeed.length !== (postsFeedCount ? postsFeedCount : 1)) {
  107. const newPosts = yield call(promiseWorker,
  108. actionPostsFeed([...(myFollowing || []), myId], postsFeed.length),
  109. )
  110. console.log('newPosts', newPosts)
  111. const newPostsFeedCount = yield call(promiseWorker, (
  112. actionPostsFeedCount([...(myFollowing || []), myId])))
  113. if (newPosts && newPostsFeedCount) {
  114. console.log('newPosts', newPosts)
  115. yield put(actionFeedType(newPosts, newPostsFeedCount))
  116. }
  117. }
  118. }
  119. export function* feedWatcher() {
  120. yield takeLeading("FEED_POSTS", feedWorker)
  121. }
  122. export function* clearFeedWatcher() {
  123. yield takeLeading("CLEAR-POSTS",
  124. yield put(actionClearFeedPosts()))
  125. }
  126. //feed
  127. export const actionAddFullLikeFeed = (postId) => async (dispatch, getState) => {
  128. await dispatch(actionAddLike(postId))
  129. const {
  130. promise: {
  131. addLike: { status },
  132. },
  133. } = getState()
  134. if (status === 'FULFILLED') {
  135. const onePost = await dispatch(actionOnePost(postId))
  136. if (onePost) await dispatch(actionAddLikePostInTape(postId))
  137. }
  138. }
  139. export const actionDeleteFullLikeFeed = (likeId, postId) => async (
  140. dispatch,
  141. getState,
  142. ) => {
  143. await dispatch(actionDeleteLike(likeId, postId))
  144. const {
  145. promise: {
  146. deleteLike: { status },
  147. },
  148. } = getState()
  149. if (status === 'FULFILLED') {
  150. const onePost = await dispatch(actionOnePost(postId))
  151. if (onePost) await dispatch(actionDeleteLikePostInTape(likeId, postId))
  152. }
  153. }
  154. //comment
  155. export const actionAddFullCommentFeed = (postId, newResult) => async (
  156. dispatch,
  157. getState,
  158. ) => {
  159. await dispatch(actionAddComment(postId, newResult))
  160. const {
  161. promise: {
  162. addComment: { status },
  163. },
  164. } = getState()
  165. if (status === 'FULFILLED') {
  166. const onePost = await dispatch(actionOnePost(postId))
  167. if (onePost) await dispatch(actionAddCommentPostInTape(postId, newResult))
  168. }
  169. // await dispatch(actionOnePost(postId));
  170. }
  171. //clear user data after log out
  172. export const actionClearUserData = () => async (dispatch) => {
  173. const logOut = await dispatch(actionAuthLogout())
  174. if (logOut) {
  175. history.push('/input')
  176. await dispatch(actionClearDataUserType())
  177. await dispatch(actionClearFeedPosts())
  178. await dispatch(actionRemoveDataAboutMe())
  179. await dispatch(actionAllClearPromise())
  180. }
  181. }