index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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, 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. if (auth?.payload?.sub?.id) {
  66. const aboutMe = yield call(promiseWorker, actionAboutMe(auth?.payload?.sub.id))
  67. if (aboutMe) {
  68. yield put(actionProfilePageDataType(aboutMe))
  69. console.log('about me _id',aboutMe?._id)
  70. yield put(actionFullProfilePageUser(aboutMe?._id))
  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 =
  99. aboutMe?.following && aboutMe?.following?.map(({ _id }) => _id)
  100. const myId = yield select().auth.payload?.sub?.id
  101. if (!myFollowing)
  102. yield put(actionFullProfilePage(myId))
  103. // await dispatch(actionFullProfilePage(myId))
  104. myFollowing = yield select().profileData.aboutMe?.following?.map(({ _id }) => _id)
  105. console.log('myFollowing ', 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. // const aboutMe = yield call(promiseWorker, actionAboutMe(_id))
  119. // if (aboutMe)
  120. // yield put(actionProfilePageDataType(aboutMe))
  121. }
  122. export function* feedWatcher() {
  123. yield takeEvery("FEED_POSTS", feedWorker)
  124. }
  125. //feed
  126. export const actionAddFullLikeFeed = (postId) => async (dispatch, getState) => {
  127. await dispatch(actionAddLike(postId))
  128. const {
  129. promise: {
  130. addLike: { status },
  131. },
  132. } = getState()
  133. if (status === 'FULFILLED') {
  134. const onePost = await dispatch(actionOnePost(postId))
  135. if (onePost) await dispatch(actionAddLikePostInTape(postId))
  136. }
  137. }
  138. export const actionDeleteFullLikeFeed = (likeId, postId) => async (
  139. dispatch,
  140. getState,
  141. ) => {
  142. await dispatch(actionDeleteLike(likeId, postId))
  143. const {
  144. promise: {
  145. deleteLike: { status },
  146. },
  147. } = getState()
  148. if (status === 'FULFILLED') {
  149. const onePost = await dispatch(actionOnePost(postId))
  150. if (onePost) await dispatch(actionDeleteLikePostInTape(likeId, postId))
  151. }
  152. }
  153. //comment
  154. export const actionAddFullCommentFeed = (postId, newResult) => async (
  155. dispatch,
  156. getState,
  157. ) => {
  158. await dispatch(actionAddComment(postId, newResult))
  159. const {
  160. promise: {
  161. addComment: { status },
  162. },
  163. } = getState()
  164. if (status === 'FULFILLED') {
  165. const onePost = await dispatch(actionOnePost(postId))
  166. if (onePost) await dispatch(actionAddCommentPostInTape(postId, newResult))
  167. }
  168. // await dispatch(actionOnePost(postId));
  169. }
  170. //clear user data after log out
  171. export const actionClearUserData = () => async (dispatch) => {
  172. const logOut = await dispatch(actionAuthLogout())
  173. if (logOut) {
  174. history.push('/input')
  175. await dispatch(actionClearDataUserType())
  176. await dispatch(actionClearFeedPosts())
  177. await dispatch(actionRemoveDataAboutMe())
  178. await dispatch(actionAllClearPromise())
  179. }
  180. }