index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. actionAddSubCommentFeedType
  15. } from '../../../actions/types/commentTypes'
  16. function* addCommentOnePostWorker({ postId, text }) {
  17. yield call(promiseWorker, actionAddComment(postId, text))
  18. const {
  19. promise: {
  20. addComment: { status },
  21. },
  22. } = yield select()
  23. if (status === 'FULFILLED') {
  24. yield call(promiseWorker, actionOnePost(postId))
  25. const { comments } = yield call(
  26. promiseWorker,
  27. actionGetCommentsOnePost(postId),
  28. )
  29. if (comments) yield put(actionAddCommentType(comments))
  30. }
  31. }
  32. export function* addCommentOnePostWatcher() {
  33. yield takeLeading('ONE_POST_COMMENT', addCommentOnePostWorker)
  34. }
  35. function* addCommentFeedWorker({ postId, text }) {
  36. yield call(promiseWorker, actionAddComment(postId, text))
  37. const {
  38. promise: {
  39. addComment: { status },
  40. },
  41. } = yield select()
  42. if (status === 'FULFILLED') {
  43. yield call(promiseWorker, actionOnePost(postId))
  44. const { comments } = yield call(
  45. promiseWorker,
  46. actionGetCommentsOnePost(postId),
  47. )
  48. if (comments) yield put(actionAddCommentPostFeedType(postId, comments))
  49. }
  50. }
  51. export function* addCommentFeedWatcher() {
  52. yield takeLeading('FEED_POST_COMMENT', addCommentFeedWorker)
  53. }
  54. function* addSubCommentWorker({ commentId, newResult }) {
  55. yield call(promiseWorker, actionAddSubComment(commentId, newResult))
  56. const {
  57. promise: {
  58. addSubComment: { status },
  59. },
  60. } = yield select()
  61. if (status === 'FULFILLED') {
  62. yield call(getSubCommentWorker, { commentId })
  63. }
  64. }
  65. export function* addSubCommentWatcher() {
  66. yield takeEvery('POST_SUB_COMMENT', addSubCommentWorker)
  67. }
  68. function* addSubCommentFeedWorker({ commentId, newResult }) {
  69. yield call(promiseWorker, actionAddSubComment(commentId, newResult))
  70. const {
  71. promise: {
  72. addSubComment: { status },
  73. },
  74. } = yield select()
  75. if (status === 'FULFILLED') {
  76. yield call(getSubCommentFeedWorker, { commentId })
  77. }
  78. }
  79. export function* addSubCommentFeedWatcher() {
  80. yield takeEvery('FEED_POST_SUB_COMMENT', addSubCommentFeedWorker)
  81. }
  82. function* getSubCommentWorker({ commentId }) {
  83. const { answers } = yield call(promiseWorker,
  84. actionFindSubComment(commentId))
  85. if (answers) {
  86. yield put(actionAddSubCommentType(commentId, answers))
  87. }
  88. }
  89. export function* getSubCommentWatcher() {
  90. yield takeEvery('GET_SUB_COMMENT', getSubCommentWorker)
  91. }
  92. function* getSubCommentFeedWorker({ commentId }) {
  93. const { answers } = yield call(promiseWorker,
  94. actionFindSubComment(commentId))
  95. if (answers) {
  96. yield put(actionAddSubCommentFeedType(commentId, answers))
  97. }
  98. }
  99. export function* getSubCommentFeedWatcher() {
  100. yield takeEvery('GET_SUB_FEED_COMMENT', getSubCommentFeedWorker)
  101. }