index.js 11 KB

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