index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. import { all, call, delay, fork, join, put, select, takeEvery, takeLatest, takeLeading } from "redux-saga/effects";
  2. import { actionAboutMe, actionAboutMeAC, actionAddComment, actionAddCommentAC, actionAddLikeComment, actionAddLikeCommentAC, actionAddLikePost, actionAddLikePostAC, actionAddPostInCollections, actionAddPostsFeedAC, actionAllPostsCount, actionAuthLogin, actionClearPromise, actionEditCommentAC, actionFindComment, actionFindCommentText, actionFindLikeComment, actionFindMyCollections, actionFindSubComment, actionFullAboutMe, actionFullLogIn, actionGetAllPosts, actionGetAvatar, actionGetFindFollowers, actionGetFindFollowing, actionGetFindLiked, actionGetPostAC, actionLoadSearchUsers, actionLoadSubscribe, actionloadUnSubscribe, actionLogIn, actionMyLikePost, actionOnLoadMyCollection, actionPending, actionPostsCount, actionPostsMyFollowing, actionProfileData, actionProfileDataAC, actionProfilePagePost, actionPromise, actionRegister, actionRejected, actionRemoveLikeComment, actionRemoveLikeCommentAC, actionRemoveLikePost, actionRemoveLikePostAC, actionRemovePostsFeedAC, actionResolved, actionSentPost, actionSetAvatar, actionSubAddComment, actionUpdateFollowers, actionUpdateFollowersAC, actionUpdateMyAvatart, actionUpdateMyFollowing, actionUpdateMyFollowingAC, actionUpdateSubCommentAC, actionUpsertAboutMe, actionUpsertCollectionAC, actionUpsertEditComment, actionUpsertLikeCommentAC, actionUpsertPost } from "../../actions";
  3. import { queries } from "../../actions/actionQueries";
  4. import { gql } from "../../helpers";
  5. //*************** Promise ******************//
  6. function* promiseWorker({ name, promise }) {
  7. yield put(actionPending(name))
  8. try {
  9. let data = yield promise
  10. yield put(actionResolved(name, data))
  11. return data
  12. }
  13. catch (error) {
  14. yield put(actionRejected(name, error))
  15. }
  16. }
  17. function* promiseWatcher() {
  18. yield takeEvery('PROMISE_START', promiseWorker)
  19. }
  20. //*************** ROUTE ******************//
  21. function* routeWorker({ match }) {
  22. if (match.path in queries) {
  23. const { name, query, variables } = queries[match.path](match)
  24. const result = yield call(promiseWorker, actionPromise(name, gql(query, variables)))
  25. if (result) {
  26. yield put(actionGetPostAC({ ...result, comments: result?.comments?.reverse() }))
  27. }
  28. }
  29. }
  30. function* routeWatcher() {
  31. yield takeEvery('ROUTE', routeWorker)
  32. }
  33. //*************** AUTHORIZATION ******************//
  34. function* logInWorker({ login, password, remember }) {
  35. const token = yield call(promiseWorker, actionLogIn(login, password))
  36. if (token) {
  37. yield put(actionAuthLogin(token, remember))
  38. yield put(actionFullAboutMe())
  39. }
  40. }
  41. function* fullRegisrterWorker({ login, password, remember }) {
  42. yield call(promiseWorker, actionRegister(login, password))
  43. yield put(actionFullLogIn(login, password, remember))
  44. }
  45. function* loginWatcher() {
  46. yield all([takeEvery('FULL_LOGIN', logInWorker), takeEvery('FULL_REGISTER', fullRegisrterWorker)])
  47. }
  48. //*************** ABOUT ME ******************//
  49. function* aboutMeWorker() {
  50. const { auth } = yield select()
  51. if (auth?.payload?.sub?.id) {
  52. const taskData = yield fork(promiseWorker, actionAboutMe(auth?.payload?.sub?.id))
  53. const taskCollection = yield fork(promiseWorker, actionFindMyCollections(auth?.payload?.sub?.id))
  54. const [dataResult, collectionResult] = yield join([taskData, taskCollection])
  55. if (dataResult) {
  56. yield put(actionAboutMeAC(dataResult))
  57. yield put(actionUpsertCollectionAC(collectionResult))
  58. }
  59. }
  60. }
  61. function* aboutMeUpsertWorker({ nick, login }) {
  62. const { myData: { _id } } = yield select()
  63. const newMyData = { _id, nick, login }
  64. const result = yield call(promiseWorker, actionUpsertAboutMe(newMyData))
  65. if (result) {
  66. yield put(actionAboutMeAC(result))
  67. yield put(actionClearPromise('upsertAboutMe'))
  68. }
  69. }
  70. function* aboutMeWatcher() {
  71. yield all([
  72. takeEvery('ABOUT_ME', aboutMeWorker),
  73. takeEvery('ABOUT_ME_UPSERT', aboutMeUpsertWorker),
  74. ])
  75. }
  76. //*************** POSTS FEED ******************//
  77. function* postsFeedWorker() {
  78. const {
  79. postsFeed: { posts, count },
  80. myData: { following, _id }
  81. } = yield select()
  82. !Array.isArray(posts) && (yield put(actionRemovePostsFeedAC()))
  83. const newArrFollowing = following.map(f => f._id)
  84. if (posts?.length !== (count ? count : 1)) {
  85. const taskPosts = yield fork(promiseWorker, actionPostsMyFollowing(posts?.length, [...newArrFollowing, _id]))
  86. const taskCount = yield fork(promiseWorker, actionPostsCount([...newArrFollowing, _id]))
  87. const [postsResult, countResult] = yield join([taskPosts, taskCount])
  88. if (postsResult && countResult) {
  89. yield put(actionAddPostsFeedAC(postsResult, countResult))
  90. }
  91. }
  92. }
  93. function* allPostsFeedWorker() {
  94. const {
  95. postsFeed: { posts, count }
  96. } = yield select()
  97. !Array.isArray(posts) && (yield put(actionRemovePostsFeedAC()))
  98. if (posts?.length !== (count ? count : 1)) {
  99. const taskPosts = yield fork(promiseWorker, actionGetAllPosts(posts?.length))
  100. const taskCount = yield fork(promiseWorker, actionAllPostsCount())
  101. const [postsResult, countResult] = yield join([taskPosts, taskCount])
  102. if (postsResult && countResult) {
  103. yield put(actionAddPostsFeedAC(postsResult, countResult))
  104. }
  105. }
  106. }
  107. function* postsFeedWatcher() {
  108. yield all([
  109. takeLeading('POSTS_FEED', postsFeedWorker),
  110. takeLeading('ALL_POSTS', allPostsFeedWorker),
  111. ])
  112. }
  113. //*************** DATA PROFILE ******************//
  114. function* dataProfileWorker({ id }) {
  115. const { postsFeed: { posts, count, userData } } = yield select()
  116. if ((posts?.length ? posts?.length : 0) < (count ? count : 1)) {
  117. if (!count) {
  118. yield put(actionRemovePostsFeedAC())
  119. const taskDataProfile = yield fork(promiseWorker, actionProfileData(id))
  120. const taskUserPosts = yield fork(promiseWorker, actionProfilePagePost(id, (posts?.length ? posts?.length : 0)))
  121. const taskCountPosts = yield fork(promiseWorker, actionPostsCount([id]))
  122. const [dataProfile, userPosts, countPosts] = yield join([taskDataProfile, taskUserPosts, taskCountPosts])
  123. if (dataProfile && userPosts) {
  124. yield put(actionProfileDataAC(userPosts, countPosts, dataProfile))
  125. }
  126. } else {
  127. const userPosts = yield call(promiseWorker, actionProfilePagePost(id, posts?.length))
  128. if (userPosts) {
  129. yield put(actionProfileDataAC(userPosts, count, userData))
  130. }
  131. }
  132. }
  133. }
  134. function* dataProfileWatcher() {
  135. yield takeLeading('DATA_PROFILE', dataProfileWorker)
  136. }
  137. //*************** Find Users ******************//
  138. function* findUserWorker({ value }) {
  139. yield delay(1500)
  140. yield call(promiseWorker, actionLoadSearchUsers(value))
  141. }
  142. function* findUserWatcher() {
  143. yield takeLatest('SEARCH_USERS', findUserWorker)
  144. }
  145. //*************** Like POST / COMMENT ******************//
  146. function* addLikePostWorker({ postId }) {
  147. yield call(promiseWorker, actionAddLikePost(postId))
  148. const { likes } = yield call(promiseWorker, actionMyLikePost(postId))
  149. if (likes) {
  150. yield put(actionAddLikePostAC(postId, likes))
  151. }
  152. }
  153. function* delLikePostWorker({ likeId, postId }) {
  154. yield call(promiseWorker, actionRemoveLikePost(likeId))
  155. const { likes } = yield call(promiseWorker, actionMyLikePost(postId))
  156. console.log(likes);
  157. if (likes) {
  158. yield put(actionRemoveLikePostAC(postId, likes))
  159. }
  160. }
  161. function* addLikeCommentWorker({ commentId }) {
  162. yield call(promiseWorker, actionAddLikeComment(commentId))
  163. const { likes } = yield call(promiseWorker, actionFindLikeComment(commentId))
  164. if (likes) {
  165. yield put(actionUpsertLikeCommentAC(commentId, likes))
  166. }
  167. }
  168. function* delLikeCommentWorker({ likeId, commentId }) {
  169. yield call(promiseWorker, actionRemoveLikeComment(likeId))
  170. const { likes } = yield call(promiseWorker, actionFindLikeComment(commentId))
  171. console.log(likes);
  172. if (likes) {
  173. yield put(actionUpsertLikeCommentAC(commentId, likes))
  174. }
  175. }
  176. function* likePostWatcher() {
  177. yield all([
  178. takeEvery('LIKE_POST', addLikePostWorker),
  179. takeEvery('DEL_LIKE_POST', delLikePostWorker),
  180. takeEvery('LIKE_COMMENT', addLikeCommentWorker),
  181. takeEvery('DEL_LIKE_COMMENT', delLikeCommentWorker),
  182. ])
  183. }
  184. //*************** SUBSCRIBE ******************//
  185. function* subscribeWorker({ userId }) {
  186. const { auth: { payload: { sub: { id } } },
  187. promise: { aboutMe: { payload: { following: f } } } } = yield select()
  188. yield call(promiseWorker, actionLoadSubscribe(id, f, userId))
  189. const taskFollowers = yield fork(promiseWorker, actionUpdateFollowers(userId))
  190. const taskMyFollowing = yield fork(promiseWorker, actionUpdateMyFollowing(id))
  191. const [{ followers }, { following }] = yield join([taskFollowers, taskMyFollowing])
  192. if (followers) {
  193. yield put(actionUpdateFollowersAC(followers))
  194. yield put(actionUpdateMyFollowingAC(following))
  195. }
  196. }
  197. function* unSubscribeWorker({ userId }) {
  198. const { auth: { payload: { sub: { id } } },
  199. promise: { aboutMe: { payload: { following: f } } } } = yield select()
  200. const newArrFollowing = [...f].filter(f => f._id !== userId)
  201. yield call(promiseWorker, actionloadUnSubscribe(id, newArrFollowing))
  202. const taskFollowers = yield fork(promiseWorker, actionUpdateFollowers(userId))
  203. const taskMyFollowing = yield fork(promiseWorker, actionUpdateMyFollowing(id))
  204. const [{ followers }, { following }] = yield join([taskFollowers, taskMyFollowing])
  205. if (followers) {
  206. yield put(actionUpdateFollowersAC(followers))
  207. yield put(actionUpdateMyFollowingAC(following))
  208. }
  209. }
  210. function* subscribeWatcher() {
  211. yield all([takeLeading('UN_SUBSCRIBE', unSubscribeWorker), takeLeading('SUBSCRIBE', subscribeWorker)])
  212. }
  213. //*************** COMMENTS ******************//
  214. function* addCommentWorker({ text }) {
  215. const { postsFeed: { posts: { _id } } } = yield select()
  216. yield call(promiseWorker, actionAddComment(_id, text))
  217. const { comments } = yield call(promiseWorker, actionFindComment(_id))
  218. console.log(comments, _id);
  219. if (comments) {
  220. yield put(actionAddCommentAC(comments?.reverse()))
  221. }
  222. }
  223. function* editCommentWorker({ commentId, text }) {
  224. yield call(promiseWorker, actionUpsertEditComment(commentId, text))
  225. const result = yield call(promiseWorker, actionFindCommentText(commentId))
  226. console.log(result);
  227. if (commentId, result) {
  228. yield put(actionEditCommentAC(commentId, result))
  229. }
  230. } function* addSubCommentWorker({ commentId, text }) {
  231. yield call(promiseWorker, actionSubAddComment(commentId, text))
  232. yield call(findSubCommentWorker, { commentId })
  233. }
  234. function* findSubCommentWorker({ commentId }) {
  235. const { answers } = yield call(promiseWorker, actionFindSubComment(commentId))
  236. if (commentId, answers) {
  237. yield put(actionUpdateSubCommentAC(commentId, answers))
  238. }
  239. }
  240. function* addCommentWatcher() {
  241. yield all([
  242. takeEvery('COMMENT_POST', addCommentWorker),
  243. takeEvery('COMMENT_EDIT', editCommentWorker),
  244. takeEvery('FIND_SUBCOMMENT', findSubCommentWorker),
  245. takeEvery('ADD_SUB_COMMENT', addSubCommentWorker)
  246. ])
  247. }
  248. //*************** UPLOAD AVATAR ******************//
  249. function* updateAvatarWorker({ file }) {
  250. const { auth: { payload: { sub: { id } } } } = yield select()
  251. console.log(file, id);
  252. yield call(promiseWorker, actionSetAvatar(file, id))
  253. const { avatar } = yield call(promiseWorker, actionGetAvatar(id))
  254. console.log(avatar);
  255. if (avatar) {
  256. yield put(actionUpdateMyAvatart(avatar))
  257. }
  258. }
  259. function* updateAvatarWatcher() {
  260. yield takeEvery('UPDATE_AVATAR', updateAvatarWorker)
  261. }
  262. //*************** FIND FOLLOWING/FOLLOWERS ******************//
  263. function* findFollowersWorker({ _id }) {
  264. yield call(promiseWorker, actionGetFindFollowers(_id))
  265. }
  266. function* findFollowingWorker({ _id }) {
  267. yield call(promiseWorker, actionGetFindFollowing(_id))
  268. }
  269. function* findLikedWorker({ _id }) {
  270. yield call(promiseWorker, actionGetFindLiked(_id))
  271. }
  272. function* findFollowWatcher() {
  273. yield all([
  274. takeEvery('FIND_FOLLOWERS', findFollowersWorker),
  275. takeEvery('FIND_FOLLOWING', findFollowingWorker),
  276. takeEvery('FIND_LIKED', findLikedWorker)
  277. ])
  278. }
  279. //*************** Create Post ******************//
  280. function* sentPostWorker({ images, text, title }) {
  281. let imagesId = images.map(im => ({ _id: im._id }))
  282. const { postsFeed: { posts: { _id } } } = yield select()
  283. const upSertPostObj = {
  284. _id,
  285. images: imagesId,
  286. text,
  287. title
  288. }
  289. const result = yield call(promiseWorker, actionUpsertPost(upSertPostObj))
  290. if (result) {
  291. yield put(actionClearPromise('sentPost'))
  292. }
  293. }
  294. function* sentPostWatcher() {
  295. yield takeEvery('CREATE_POST', sentPostWorker)
  296. }
  297. //*************** COLLECTION ******************//
  298. function* handlerCollectionWorker({ _id, flag }) {
  299. const { myData: { _id: myID, collections } } = yield select()
  300. const newArr = flag ? collections?.posts.filter(c => c._id !== _id) : [...collections?.posts || [], { _id }]
  301. yield call(promiseWorker, actionAddPostInCollections(collections?._id, newArr))
  302. const newData = yield call(promiseWorker, actionFindMyCollections(myID))
  303. if (newData) yield put(actionUpsertCollectionAC(newData))
  304. }
  305. function* loadCollectionWorker() {
  306. const { myData: { collections }, postsFeed } = yield select()
  307. !Array.isArray(postsFeed?.posts) && (yield put(actionRemovePostsFeedAC()))
  308. const [{ posts: newResult }] = yield call(promiseWorker, actionOnLoadMyCollection(collections?._id, postsFeed?.posts?.length))
  309. if (newResult) {
  310. yield put(actionAddPostsFeedAC(newResult))
  311. }
  312. }
  313. function* handlerCollectionWatcher() {
  314. yield all([
  315. takeEvery('HANDLER_UPSERT_COLLECTION', handlerCollectionWorker),
  316. takeEvery('LOAD_COLLECTION', loadCollectionWorker)
  317. ])
  318. }
  319. //
  320. export function* rootSaga() {
  321. yield all([
  322. promiseWatcher(),
  323. routeWatcher(),
  324. loginWatcher(),
  325. aboutMeWatcher(),
  326. postsFeedWatcher(),
  327. dataProfileWatcher(),
  328. findUserWatcher(),
  329. likePostWatcher(),
  330. subscribeWatcher(),
  331. addCommentWatcher(),
  332. updateAvatarWatcher(),
  333. findFollowWatcher(),
  334. sentPostWatcher(),
  335. handlerCollectionWatcher(),
  336. ])
  337. }