index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { put, takeEvery, takeLeading, select, call } from 'redux-saga/effects' //
  2. import { promiseWorker } from '../promise'
  3. import {
  4. actionAddComment,
  5. actionGetCommentsOnePost,
  6. actionAddSubComment,
  7. actionFindSubComment,
  8. } from '../../../actions/query/commentQuery'
  9. import { actionOnePost } from '../../../actions/query/postQuery'
  10. import {
  11. actionAddCommentPostFeedType,
  12. actionAddCommentType,
  13. actionAddSubCommentType,
  14. } from '../../../actions/types/commentTypes'
  15. function* addCommentOnePostWorker({ postId, text }) {
  16. yield call(promiseWorker, actionAddComment(postId, text))
  17. const {
  18. promise: {
  19. addComment: { status },
  20. },
  21. } = yield select()
  22. if (status === 'FULFILLED') {
  23. yield call(promiseWorker, actionOnePost(postId))
  24. const { comments } = yield call(
  25. promiseWorker,
  26. actionGetCommentsOnePost(postId),
  27. )
  28. if (comments) yield put(actionAddCommentType(comments))
  29. }
  30. }
  31. export function* addCommentOnePostWatcher() {
  32. yield takeLeading('ONE_POST_COMMENT', addCommentOnePostWorker)
  33. }
  34. function* addCommentFeedWorker({ postId, text }) {
  35. yield call(promiseWorker, actionAddComment(postId, text))
  36. const {
  37. promise: {
  38. addComment: { status },
  39. },
  40. } = yield select()
  41. if (status === 'FULFILLED') {
  42. yield call(promiseWorker, actionOnePost(postId))
  43. const { comments } = yield call(
  44. promiseWorker,
  45. actionGetCommentsOnePost(postId),
  46. )
  47. if (comments) yield put(actionAddCommentPostFeedType(postId, comments))
  48. }
  49. }
  50. export function* addCommentFeedWatcher() {
  51. yield takeLeading('FEED_POST_COMMENT', addCommentFeedWorker)
  52. }
  53. function* addSubCommentWorker({ commentId, newResult }) {
  54. yield call(promiseWorker, actionAddSubComment(commentId, newResult))
  55. const {
  56. promise: {
  57. addSubComment: { status },
  58. },
  59. } = yield select()
  60. if (status === 'FULFILLED') {
  61. yield call(getSubCommentWorker, { commentId })
  62. }
  63. }
  64. export function* addSubCommentWatcher() {
  65. yield takeEvery('POST_SUB_COMMENT', addSubCommentWorker)
  66. }
  67. function* getSubCommentWorker({ commentId }) {
  68. const { answers } = yield call(promiseWorker, actionFindSubComment(commentId))
  69. if (answers) {
  70. yield put(actionAddSubCommentType(commentId, answers))
  71. }
  72. }
  73. export function* getSubCommentWatcher() {
  74. yield takeEvery('GET_SUB_COMMENT', getSubCommentWorker)
  75. }