Procházet zdrojové kódy

Add exploreWatcher

LenDoc před 1 rokem
rodič
revize
02bc085371

+ 4 - 23
src/actions/index.js

@@ -654,34 +654,15 @@ export const actionFullAllGetPosts = () => ({
   type:"FEED_POSTS"
 })
 
-export const actionFullExplorePosts = () => async (dispatch, getState) => {
-  const {
-    explore: { explorePosts, explorePostsCount },
-    promise,
-  } = getState()
-  console.log('explorePosts', explorePosts)
-
-  if (explorePosts?.length !== (explorePostsCount ? explorePostsCount : 1)) {
-    console.log('explorePosts', explorePosts)
-
-    const newPosts = await dispatch(actionExplorePosts(explorePosts?.length))
-
-    console.log('newPosts', newPosts)
-
-    const newPostsExploreCount = await dispatch(actionExplorePostsCount())
-    if (newPostsExploreCount && newPosts)
-      await dispatch(actionExploreType(newPosts, newPostsExploreCount))
-
-    // if (promise?.explorePosts?.status == 'PENDING')
-    //   await dispatch(actionClearExplorePosts())
-  }
-}
+export const actionFullExplorePosts = () => ({
+  type:"EXPLORE_POSTS"
+})
 
 export const actionPostsFeedCount = (myFollowing) =>
   actionPromise(
     'postsFeedCount',
     gql(
-      ` query CountAllPostsUser($_id:String!){
+      ` query CountAllPostsFeed($_id:String!){
                 PostCount(query:$_id)
 
                 }`,

+ 2 - 1
src/redux/reducers/explore/exploreReducer.js

@@ -46,6 +46,7 @@ export const actionClearExplorePosts = () =>
   ({ type: 'CLEAR-EXPLORE-POSTS' })
 
 export const actionExploreType = (newPosts,newPostsExploreCount) => 
-  ({ type: 'ADD-EXPLORE-POSTS', newPosts,newPostsExploreCount })
+  ({ type: 'ADD-EXPLORE-POSTS', newPosts, newPostsExploreCount })
+  
   export const actionExploreTypeCount = (explorePostsCount) => 
   ({ type: 'EXPLORE-COUNT', explorePostsCount })

+ 2 - 0
src/redux/reducers/index.js

@@ -14,6 +14,7 @@ import {
   fullPageAboutUserWatcher,
   feedWatcher,
   clearFeedWatcher,
+  exploreWatcher
 } from '../saga'
 import createSagaMiddleware from 'redux-saga' //функция по созданию middleware
 import {
@@ -46,6 +47,7 @@ function* rootSaga() {
     fullPageAboutUserWatcher(),
     clearFeedWatcher(),
     feedWatcher(),
+    exploreWatcher()
   ])
 }
 sagaMiddleware.run(rootSaga)

+ 11 - 2
src/redux/reducers/profileUserPage/profileUserReducer.js

@@ -1,7 +1,7 @@
 
 export const profileUserReducer = (
     state = {},
-    { type, aboutUser, allPosts, newPosts },
+    { type, aboutUser, allPosts, newPosts, countPosts },
   ) => {
     const types = {
       'PROFILE-PAGE-USER': () => {
@@ -23,6 +23,13 @@ export const profileUserReducer = (
           aboutUser,
         }
       },
+      'COUNT_ALL_POSTS': () => {
+        
+        return {
+          ...state,
+          countPosts
+        }
+      }
     }
   
     if (type in types) {
@@ -40,7 +47,9 @@ export const actionProfilePageDataTypeUser = (aboutUser, allPosts) => ({
   aboutUser,
   allPosts,
 })
-
+export const actionCountPostsType = (countPosts) =>
+  ({ type: "COUNT_ALL_POSTS", countPosts })
+  
 export const actionProfilePageData = (id) => 
 ({ type: 'DATA_PROFILE', id })
 

+ 31 - 3
src/redux/saga/index.js

@@ -27,10 +27,11 @@ import {
   actionClearFeedPosts,
   actionFeedType
 } from '../reducers/feed/feedReducer'
-import { actionProfilePageDataTypeUser } from '../reducers/profileUserPage/profileUserReducer'
+import { actionProfilePageDataTypeUser,actionCountPostsType } from '../reducers/profileUserPage/profileUserReducer'
 import { actionRemoveDataAboutMe } from '../reducers/profileData/profileReducer'
+import {actionExploreType} from '../reducers/explore/exploreReducer'
 import { all, put,take, fork, takeEvery, takeLatest, takeLeading, select,call } from 'redux-saga/effects'; //
-import {actionPending,actionFulfilled,actionRejected} from '../../actions'
+import {actionPending,actionFulfilled,actionRejected,actionExplorePosts,actionExplorePostsCount} from '../../actions'
 
 
   //promise
@@ -98,10 +99,13 @@ function* fullPageAboutUserWorker({ _id }) {
   const aboutUser = yield call(promiseWorker, actionAboutUser(_id))
   console.log('about user',  aboutUser)
   const allPosts = yield call(promiseWorker, actionAllPostsUser(_id))
+  const countPosts = yield call(promiseWorker, actionPostsCount(_id))
+
   if (aboutUser && allPosts) {
     yield put(actionProfilePageDataTypeUser(aboutUser, allPosts))
-
   }
+  if (countPosts)
+  yield put(actionCountPostsType(countPosts))
 }
 
 export function* fullPageAboutUserWatcher() {
@@ -144,7 +148,31 @@ export function* clearFeedWatcher() {
   yield put(actionClearFeedPosts()))
 }
 
+//explore 
+function* exploreWorker(){
+  const {
+    explore: { explorePosts, explorePostsCount },
+  } = yield select()
+  console.log('explorePosts', explorePosts)
+
+  if (explorePosts?.length !== (explorePostsCount ? explorePostsCount : 1)) {
+    console.log('explorePosts', explorePosts)
+
+    const newPosts = yield call(promiseWorker, actionExplorePosts(explorePosts?.length))
 
+    console.log('newPosts', newPosts)
+
+    const newPostsExploreCount = yield call(promiseWorker,(actionExplorePostsCount()))
+    if (newPostsExploreCount && newPosts)
+      yield put(actionExploreType(newPosts, newPostsExploreCount))
+
+    // if (promise?.explorePosts?.status == 'PENDING')
+    //   await dispatch(actionClearExplorePosts())
+  }
+}
+export function* exploreWatcher() {
+  yield takeLeading("EXPLORE_POSTS", exploreWorker)
+}
 //feed
 
 export const actionAddFullLikeFeed = (postId) => async (dispatch, getState) => {