index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. actionClearPromise,
  17. actionGetCommentsOnePost,
  18. actionFindLikes,
  19. // actionOnePost
  20. } from '../../actions'
  21. import { history } from '../../helpers'
  22. import{actionClearDataUserType} from '../reducers/userData/userProfileReducer'
  23. import { actionProfilePageDataType } from '../reducers/myData/myProfileReducer'
  24. import { actionFullAllGetPosts } from '../../actions'
  25. import {
  26. actionAddLikePostInTape,
  27. actionDeleteLikePostInTape,
  28. actionAddCommentPostInTape,
  29. actionClearFeedPosts,
  30. actionFeedType
  31. } from '../reducers/feed/feedReducer'
  32. import { actionProfilePageDataTypeUser,actionCountPostsType } from '../reducers/userData/userProfileReducer'
  33. import { actionRemoveDataAboutMe } from '../reducers/myData/myProfileReducer'
  34. import {actionExploreType,actionClearExplorePosts} from '../reducers/explore/exploreReducer'
  35. import { all, put,take, fork, takeEvery, takeLatest, takeLeading, select,call, join } from 'redux-saga/effects'; //
  36. import {actionPending,actionFulfilled,actionRejected,actionExplorePosts,actionExplorePostsCount} from '../../actions'
  37. import { actionOnePostType, actionChangeLikeType } from '../../actions/types/postActionTypes'
  38. import {actionAddCommentType} from '../../actions/types/postActionTypes'
  39. //promise
  40. export function* promiseWorker(action){ //это типа actionPromise который thunk
  41. const {name, promise} = action
  42. yield put(actionPending(name)) //это как dispatch
  43. try {
  44. let data = yield promise //а это как await
  45. yield put(actionFulfilled(name, data))
  46. return data //а этот результать можно забрать через yield call(promiseWorker, actionPromise(......) /*какой-то объект action*/)
  47. }
  48. catch (error) {
  49. yield put(actionRejected(name, error))
  50. }
  51. }
  52. export function* promiseWatcher(){
  53. yield takeEvery('PROMISE_START', promiseWorker)
  54. }
  55. //login
  56. function* loginWorker({login, password}){ //обработчик экшона FULL_LOGIN
  57. let token = yield call(promiseWorker,actionLogin(login, password)) //dispatch(actionLogin(login, password));
  58. if (token) {
  59. yield put(actionAuthLogin(token));
  60. }
  61. }
  62. export function* loginWatcher() {
  63. yield takeEvery("FULL_LOGIN", loginWorker)
  64. }
  65. //profile page about me
  66. export const actionFullProfilePage = () =>
  67. ({
  68. type:"FULLPROFILE_PAGE",
  69. })
  70. function* fullProfilePageWorker() {
  71. const { auth } = yield select()
  72. console.log('auth', auth)
  73. if (auth?.payload?.sub?.id) {
  74. const aboutMe = yield call(promiseWorker, actionAboutMe(auth?.payload?.sub.id))
  75. console.log('aboutMe in worker', aboutMe)
  76. if (aboutMe) {
  77. yield put(actionProfilePageDataType(aboutMe))
  78. }
  79. }
  80. }
  81. export function* fullProfilePageWatcher() {
  82. yield takeEvery("FULLPROFILE_PAGE", fullProfilePageWorker)
  83. }
  84. //aboutUser
  85. //full profile user
  86. export const actionFullProfilePageUser = (_id) =>
  87. ({ type: "USER_PAGE", _id })
  88. function* fullPageAboutUserWorker({ _id }) {
  89. console.log('_id ', _id)
  90. const aboutUser = yield call(promiseWorker, actionAboutUser(_id))
  91. console.log('about user', aboutUser)
  92. const allPosts = yield call(promiseWorker, actionAllPostsUser(_id))
  93. const countPosts = yield call(promiseWorker, actionPostsCount(_id))
  94. if (aboutUser && allPosts) {
  95. yield put(actionProfilePageDataTypeUser(aboutUser, allPosts))
  96. }
  97. if (countPosts)
  98. yield put(actionCountPostsType(countPosts))
  99. }
  100. export function* fullPageAboutUserWatcher() {
  101. yield takeLeading("USER_PAGE", fullPageAboutUserWorker)
  102. }
  103. function* feedWorker() {
  104. const {
  105. feed: { postsFeed, postsFeedCount },
  106. myData: { aboutMe},
  107. } = yield select()
  108. let myFollowing = aboutMe?.following && aboutMe?.following?.map(({ _id }) => _id)
  109. const myId = (yield select()).auth?.payload?.sub?.id
  110. if (!aboutMe) {
  111. yield call(fullProfilePageWorker, actionFullProfilePage())
  112. }
  113. myFollowing = (yield select()).myData.aboutMe?.following &&
  114. (yield select()).myData.aboutMe?.following?.map(({ _id }) => _id)
  115. // console.log('myFollowing after if', myFollowing)
  116. if (postsFeed?.length !== (postsFeedCount ? postsFeedCount : 1)) {
  117. const newPosts = yield call(promiseWorker,
  118. actionPostsFeed([...(myFollowing || []), myId], postsFeed?.length),
  119. )
  120. console.log('newPosts', newPosts)
  121. const newPostsFeedCount = yield call(promiseWorker, (
  122. actionPostsFeedCount([...(myFollowing || []), myId])))
  123. if (newPosts && newPostsFeedCount) {
  124. console.log('newPosts', newPosts)
  125. yield put(actionFeedType(newPosts, newPostsFeedCount))
  126. }
  127. }
  128. }
  129. export function* feedWatcher() {
  130. yield takeLeading("FEED_POSTS", feedWorker)
  131. }
  132. //explore
  133. function* exploreWorker(){
  134. const {
  135. explore: { explorePosts, explorePostsCount },
  136. } = yield select()
  137. console.log('explorePosts', explorePosts)
  138. if (explorePosts?.length !== (explorePostsCount ? explorePostsCount : 1)) {
  139. console.log('explorePosts', explorePosts)
  140. const newPosts = yield fork(promiseWorker, actionExplorePosts(explorePosts?.length))
  141. console.log('newPosts', newPosts)
  142. const newPostsExploreCount = yield fork(promiseWorker, (actionExplorePostsCount()))
  143. const [posts,exploreCount] = yield join([newPosts,newPostsExploreCount])
  144. if (posts && exploreCount)
  145. yield put(actionExploreType(posts, exploreCount))
  146. }
  147. }
  148. export function* exploreWatcher() {
  149. yield takeLeading("EXPLORE_POSTS", exploreWorker)
  150. }
  151. //feed
  152. function* addCommentFeedWorker({ postId, newResult }) {
  153. yield call(promiseWorker,actionAddComment(postId, newResult))
  154. const { comments } = yield call(promiseWorker, actionGetCommentsOnePost(postId))
  155. console.log('commentsss', comments)
  156. if (comments)
  157. yield put(actionAddCommentPostInTape(postId, newResult))
  158. }
  159. // const {
  160. // promise: {
  161. // addComment
  162. // }
  163. // } = yield select()
  164. // if (addComment?.status === 'FULFILLED') {
  165. // console.log('УРААА')
  166. // const onePost = yield call(promiseWorker, actionOnePost(postId))
  167. // if (onePost)
  168. // yield call(promiseWorker,actionAddCommentPostInTape(postId, newResult))
  169. // }
  170. // }
  171. export function* addCommentFeedWatcher() {
  172. yield takeLeading("ADD_COMMENT_FEED", addCommentFeedWorker)
  173. }
  174. // export const actionAddFullCommentFeed = (postId, newResult) => async (
  175. // dispatch,
  176. // getState,
  177. // ) => {
  178. // await dispatch(actionAddComment(postId, newResult))
  179. // const {
  180. // promise: {
  181. // addComment: { status },
  182. // },
  183. // } = getState()
  184. // if (status === 'FULFILLED') {
  185. // const onePost = await dispatch(actionOnePost(postId))
  186. // if (onePost) await dispatch(actionAddCommentPostInTape(postId, newResult))
  187. // }
  188. // // await dispatch(actionOnePost(postId));
  189. // }
  190. //one post
  191. function* onePostWorker({ _id }) {
  192. const onePost = yield call(promiseWorker,actionOnePost(_id))
  193. if (onePost)
  194. yield put(actionOnePostType(onePost))
  195. }
  196. export function* onePostWatcher(){
  197. yield takeLeading("ONE_POST",onePostWorker)
  198. }
  199. //comment
  200. function* addCommentOnePostWorker({ postId, text }) {
  201. console.log('post id', postId)
  202. console.log('comment', text)
  203. const add= yield call(promiseWorker, actionAddComment(postId, text))
  204. console.log('add', add)
  205. const {
  206. promise: {
  207. addComment: { status },
  208. },
  209. } = yield select()
  210. if (status === 'FULFILLED') {
  211. yield call(promiseWorker, actionOnePost(postId))
  212. const { comments } = yield call(promiseWorker, actionGetCommentsOnePost(postId))
  213. console.log('add comments', comments)
  214. if (comments)
  215. yield put (actionAddCommentType(comments))
  216. }
  217. }
  218. export function* addCommentOnePostWatcher(){
  219. yield takeLeading("ONE_POST_COMMENT",addCommentOnePostWorker)
  220. }
  221. // await dispatch(actionOnePost(postId));
  222. // }}
  223. // export const actionAddFullLike = (postId) => async (dispatch, getState) => {
  224. // await dispatch(actionAddLike(postId))
  225. // const {
  226. // promise: {
  227. // addLike: { status },
  228. // },
  229. // } = getState()
  230. // if (status === 'FULFILLED') {
  231. // const onePost = await dispatch(actionOnePost(postId))
  232. // if (onePost) await dispatch(actionAddLikePostInTape(postId))
  233. // }
  234. // }
  235. export const actionChangeLike = (likeId, postId) =>
  236. ({
  237. type:"CHANGE_LIKE_POST", likeId,postId
  238. })
  239. function* changeLikePostWorker({ likeId, postId }) {
  240. console.log('likeId', likeId)
  241. console.log('postId', postId)
  242. const changeOneLike = () =>
  243. likeId ? actionDeleteLike(likeId, postId) : actionAddLike(postId)
  244. yield call(promiseWorker, changeOneLike())
  245. const { likes } = yield call(promiseWorker, actionFindLikes(postId))
  246. console.log('likes in worker', likes)
  247. if (likes) {
  248. yield call(promiseWorker, actionOnePost(postId))
  249. yield put(actionChangeLikeType(likes))
  250. }
  251. // const { likes } = yield call(promiseWorker, actionFindLikes(postId))
  252. // console.log('likes', likes)
  253. // if (likes)
  254. // yield call(promiseWorker, actionOnePost(postId))
  255. }
  256. export function* changeLikePostWatcher() {
  257. yield takeLeading("CHANGE_LIKE_POST", changeLikePostWorker)
  258. }
  259. // export const actionDeleteFullLikeFeed = (likeId, postId) => async (
  260. // dispatch,
  261. // getState,
  262. // ) => {
  263. // await dispatch(actionDeleteLike(likeId, postId))
  264. // const {
  265. // promise: {
  266. // deleteLike: { status },
  267. // },
  268. // } = getState()
  269. // if (status === 'FULFILLED') {
  270. // const onePost = await dispatch(actionOnePost(postId))
  271. // if (onePost) await dispatch(actionDeleteLikePostInTape(likeId, postId))
  272. // }
  273. // }
  274. //comment
  275. export const actionAddFullCommentFeed = (postId, newResult) => ({
  276. type:"ADD_COMMENT_FEED", postId, newResult
  277. })
  278. //clear user data after log out
  279. export const actionClearUserData = () => async (dispatch) => {
  280. const logOut = await dispatch(actionAuthLogout())
  281. if (logOut) {
  282. history.push('/input')
  283. await dispatch(actionClearDataUserType())
  284. await dispatch(actionClearFeedPosts())
  285. await dispatch(actionRemoveDataAboutMe())
  286. await dispatch(actionAllClearPromise())
  287. }
  288. }