index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. actionPostUpsert,
  20. actionClearPromiseForName,
  21. actionChangeSubscribe,
  22. actionGetFollowing,
  23. actionGetFollowers,
  24. // actionOnePost
  25. } from '../../actions'
  26. import { history } from '../../helpers'
  27. import {
  28. actionClearDataUserType,
  29. actionChangeFollowersType,
  30. actionUserAllPostsType
  31. } from '../reducers/userData/userProfileReducer'
  32. import { actionProfilePageDataType,actionChangeFollowingType } from '../reducers/myData/myProfileReducer'
  33. import { actionFullAllGetPosts } from '../../actions'
  34. import {
  35. actionAddLikePostInTape,
  36. actionDeleteLikePostInTape,
  37. actionAddCommentPostInTape,
  38. actionClearFeedPosts,
  39. actionFeedType
  40. } from '../reducers/feed/feedReducer'
  41. import { actionProfilePageDataTypeUser,actionCountPostsType } from '../reducers/userData/userProfileReducer'
  42. import { actionRemoveDataAboutMe } from '../reducers/myData/myProfileReducer'
  43. import {actionExploreType,actionClearExplorePosts} from '../reducers/explore/exploreReducer'
  44. import { all, put,take, fork, takeEvery, takeLatest, takeLeading, select,call, join } from 'redux-saga/effects'; //
  45. import {actionPending,actionFulfilled,actionRejected,actionExplorePosts,actionExplorePostsCount} from '../../actions'
  46. import { actionOnePostType, actionChangeLikeType } from '../../actions/types/postActionTypes'
  47. import {actionAddCommentType} from '../../actions/types/postActionTypes'
  48. //promise
  49. export function* promiseWorker(action){ //это типа actionPromise который thunk
  50. const {name, promise} = action
  51. yield put(actionPending(name)) //это как dispatch
  52. try {
  53. let data = yield promise //а это как await
  54. yield put(actionFulfilled(name, data))
  55. return data //а этот результать можно забрать через yield call(promiseWorker, actionPromise(......) /*какой-то объект action*/)
  56. }
  57. catch (error) {
  58. yield put(actionRejected(name, error))
  59. }
  60. }
  61. export function* promiseWatcher(){
  62. yield takeEvery('PROMISE_START', promiseWorker)
  63. }
  64. //login
  65. function* loginWorker({login, password}){ //обработчик экшона FULL_LOGIN
  66. let token = yield call(promiseWorker,actionLogin(login, password)) //dispatch(actionLogin(login, password));
  67. if (token) {
  68. yield put(actionAuthLogin(token));
  69. }
  70. }
  71. export function* loginWatcher() {
  72. yield takeEvery("FULL_LOGIN", loginWorker)
  73. }
  74. //profile page about me
  75. export const actionFullProfilePage = () =>
  76. ({
  77. type:"FULLPROFILE_PAGE",
  78. })
  79. function* fullProfilePageWorker() {
  80. const { auth } = yield select()
  81. // console.log('auth', auth)
  82. if (auth?.payload?.sub?.id) {
  83. const aboutMe = yield call(promiseWorker, actionAboutMe(auth?.payload?.sub.id))
  84. // console.log('aboutMe in worker', aboutMe)
  85. if (aboutMe) {
  86. yield put(actionProfilePageDataType(aboutMe))
  87. }
  88. }
  89. }
  90. export function* fullProfilePageWatcher() {
  91. yield takeEvery("FULLPROFILE_PAGE", fullProfilePageWorker)
  92. }
  93. //aboutUser
  94. //full profile user
  95. export const actionFullProfilePageUser = (_id) =>
  96. ({ type: "USER_PAGE", _id })
  97. function* fullPageAboutUserWorker({ _id }) {
  98. // console.log('_id ', _id)
  99. const aboutUser = yield call(promiseWorker, actionAboutUser(_id))
  100. // console.log('about user', aboutUser)
  101. const allPosts = yield call(promiseWorker, actionAllPostsUser(_id))
  102. const countPosts = yield call(promiseWorker, actionPostsCount(_id))
  103. if (aboutUser)
  104. yield put(actionProfilePageDataTypeUser(aboutUser))
  105. if(allPosts) {
  106. yield put(actionUserAllPostsType(allPosts))
  107. }
  108. if (countPosts)
  109. yield put(actionCountPostsType(countPosts))
  110. }
  111. export function* fullPageAboutUserWatcher() {
  112. yield takeLeading("USER_PAGE", fullPageAboutUserWorker)
  113. }
  114. function* feedWorker() {
  115. const {
  116. feed: { postsFeed, postsFeedCount },
  117. myData: { aboutMe},
  118. } = yield select()
  119. let myFollowing = aboutMe?.following && aboutMe?.following?.map(({ _id }) => _id)
  120. const myId = (yield select()).auth?.payload?.sub?.id
  121. if (!aboutMe) {
  122. yield call(fullProfilePageWorker, actionFullProfilePage())
  123. }
  124. myFollowing = (yield select()).myData.aboutMe?.following &&
  125. (yield select()).myData.aboutMe?.following?.map(({ _id }) => _id)
  126. // console.log('myFollowing after if', myFollowing)
  127. if (postsFeed?.length !== (postsFeedCount ? postsFeedCount : 1)) {
  128. const newPosts = yield call(promiseWorker,
  129. actionPostsFeed([...(myFollowing || []), myId], postsFeed?.length),
  130. )
  131. console.log('newPosts', newPosts)
  132. const newPostsFeedCount = yield call(promiseWorker, (
  133. actionPostsFeedCount([...(myFollowing || []), myId])))
  134. if (newPosts && newPostsFeedCount) {
  135. console.log('newPosts', newPosts)
  136. yield put(actionFeedType(newPosts, newPostsFeedCount))
  137. }
  138. }
  139. }
  140. export function* feedWatcher() {
  141. yield takeLeading("FEED_POSTS", feedWorker)
  142. }
  143. //explore
  144. function* exploreWorker(){
  145. const {
  146. explore: { explorePosts, explorePostsCount },
  147. } = yield select()
  148. console.log('explorePosts', explorePosts)
  149. if (explorePosts?.length !== (explorePostsCount ? explorePostsCount : 1)) {
  150. console.log('explorePosts', explorePosts)
  151. const newPosts = yield fork(promiseWorker, actionExplorePosts(explorePosts?.length))
  152. console.log('newPosts', newPosts)
  153. const newPostsExploreCount = yield fork(promiseWorker, (actionExplorePostsCount()))
  154. const [posts,exploreCount] = yield join([newPosts,newPostsExploreCount])
  155. if (posts && exploreCount)
  156. yield put(actionExploreType(posts, exploreCount))
  157. }
  158. }
  159. export function* exploreWatcher() {
  160. yield takeLeading("EXPLORE_POSTS", exploreWorker)
  161. }
  162. //feed
  163. function* addCommentFeedWorker({ postId, newResult }) {
  164. yield call(promiseWorker,actionAddComment(postId, newResult))
  165. const { comments } = yield call(promiseWorker, actionGetCommentsOnePost(postId))
  166. console.log('commentsss', comments)
  167. if (comments)
  168. yield put(actionAddCommentPostInTape(postId, newResult))
  169. }
  170. export function* addCommentFeedWatcher() {
  171. yield takeLeading("ADD_COMMENT_FEED", addCommentFeedWorker)
  172. }
  173. // export const actionAddFullCommentFeed = (postId, newResult) => async (
  174. // dispatch,
  175. // getState,
  176. // ) => {
  177. // await dispatch(actionAddComment(postId, newResult))
  178. // const {
  179. // promise: {
  180. // addComment: { status },
  181. // },
  182. // } = getState()
  183. // if (status === 'FULFILLED') {
  184. // const onePost = await dispatch(actionOnePost(postId))
  185. // if (onePost) await dispatch(actionAddCommentPostInTape(postId, newResult))
  186. // }
  187. // // await dispatch(actionOnePost(postId));
  188. // }
  189. //one post
  190. function* onePostWorker({ _id }) {
  191. const onePost = yield call(promiseWorker,actionOnePost(_id))
  192. if (onePost)
  193. yield put(actionOnePostType(onePost))
  194. }
  195. export function* onePostWatcher(){
  196. yield takeLeading("ONE_POST",onePostWorker)
  197. }
  198. //comment
  199. function* addCommentOnePostWorker({ postId, text }) {
  200. // console.log('post id', postId)
  201. // console.log('comment', text)
  202. const add= yield call(promiseWorker, actionAddComment(postId, text))
  203. // console.log('add', add)
  204. const {
  205. promise: {
  206. addComment: { status },
  207. },
  208. } = yield select()
  209. if (status === 'FULFILLED') {
  210. yield call(promiseWorker, actionOnePost(postId))
  211. const { comments } = yield call(promiseWorker, actionGetCommentsOnePost(postId))
  212. // console.log('add comments', comments)
  213. if (comments)
  214. yield put (actionAddCommentType(comments))
  215. }
  216. }
  217. export function* addCommentOnePostWatcher(){
  218. yield takeLeading("ONE_POST_COMMENT",addCommentOnePostWorker)
  219. }
  220. // await dispatch(actionOnePost(postId));
  221. // }}
  222. // export const actionAddFullLike = (postId) => async (dispatch, getState) => {
  223. // await dispatch(actionAddLike(postId))
  224. // const {
  225. // promise: {
  226. // addLike: { status },
  227. // },
  228. // } = getState()
  229. // if (status === 'FULFILLED') {
  230. // const onePost = await dispatch(actionOnePost(postId))
  231. // if (onePost) await dispatch(actionAddLikePostInTape(postId))
  232. // }
  233. // }
  234. //change like in post
  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. }
  252. export function* changeLikePostWatcher() {
  253. yield takeLeading("CHANGE_LIKE_POST", changeLikePostWorker)
  254. }
  255. // create and edit post
  256. function* editPostWorker({state }) {
  257. console.log('in worker default post', state)
  258. console.log('in worker post id', state?._id)
  259. const postUpsert = yield call(promiseWorker, actionPostUpsert(state,state?._id))
  260. console.log('post Upsert', postUpsert)
  261. // postUpsert
  262. // const upsertPost = yield call(promiseWorker, actionPostUpsert(post))
  263. // console.log('upsert POST', upsertPost)
  264. if (postUpsert) {
  265. yield put(actionClearPromiseForName('postUpsert'))
  266. yield put(actionClearPromiseForName('uploadFiles'))
  267. }
  268. }
  269. export function* editPostWatcher() {
  270. yield takeEvery("CREATE_EDIT_POST", editPostWorker)
  271. }
  272. export const actionCreateEditPost= (state) =>
  273. ({
  274. type:"CREATE_EDIT_POST", state
  275. })
  276. //change subscribe
  277. function* changeSubscribeWorker({ followId, checkFollowId }) {
  278. const { myData: { aboutMe: { _id, following } } } = yield select()
  279. // const checkFollowId = following?.find(
  280. // (follower) => follower?._id === followId,
  281. // )?._id
  282. console.log('my following', following)
  283. console.log('check follow id', checkFollowId)
  284. console.log('my id', _id)
  285. const oldFollowing = checkFollowId ?
  286. {
  287. _id,
  288. following: [...following.filter((item) => item._id !== followId).map(({ _id }) => ({ _id }))]
  289. }
  290. : {
  291. _id,
  292. following:[...following.map(({ _id }) => ({ _id })), {_id: followId}]
  293. }
  294. console.log('old following', oldFollowing)
  295. console.log('еще раз май фолловинг ', following)
  296. const change = yield call(promiseWorker, actionChangeSubscribe(oldFollowing))
  297. console.log('change', change)
  298. const updateUserFollowers = yield call(promiseWorker, actionGetFollowers(followId))
  299. console.log('update user followers', updateUserFollowers)
  300. const updateMyFollowing = yield call(promiseWorker, actionGetFollowing(_id))
  301. console.log('update my following', updateMyFollowing)
  302. if (updateMyFollowing)
  303. yield put(actionChangeFollowingType(updateMyFollowing?.following))
  304. if (updateUserFollowers)
  305. {
  306. yield put(actionChangeFollowersType(updateUserFollowers?.followers))
  307. }
  308. }
  309. export function* changeSubscribeWatcher() {
  310. yield takeEvery("CHANGE_SUBSCRIBE", changeSubscribeWorker)
  311. }
  312. export const actionChangeSubscribeSaga= (followId,checkFollowId) =>
  313. ({
  314. type:"CHANGE_SUBSCRIBE", followId,checkFollowId
  315. })
  316. // export const actionDeleteFullLikeFeed = (likeId, postId) => async (
  317. // dispatch,
  318. // getState,
  319. // ) => {
  320. // await dispatch(actionDeleteLike(likeId, postId))
  321. // const {
  322. // promise: {
  323. // deleteLike: { status },
  324. // },
  325. // } = getState()
  326. // if (status === 'FULFILLED') {
  327. // const onePost = await dispatch(actionOnePost(postId))
  328. // if (onePost) await dispatch(actionDeleteLikePostInTape(likeId, postId))
  329. // }
  330. // }
  331. //comment
  332. export const actionAddFullCommentFeed = (postId, newResult) => ({
  333. type:"ADD_COMMENT_FEED", postId, newResult
  334. })
  335. //clear user data after log out
  336. export const actionClearUserData = () => async (dispatch) => {
  337. const logOut = await dispatch(actionAuthLogout())
  338. if (logOut) {
  339. history.push('/input')
  340. await dispatch(actionClearDataUserType())
  341. await dispatch(actionClearFeedPosts())
  342. await dispatch(actionRemoveDataAboutMe())
  343. await dispatch(actionAllClearPromise())
  344. }
  345. }