浏览代码

added subscrie/unsubscribe

makstravm 2 年之前
父节点
当前提交
9796a08187

+ 2 - 2
src/App.scss

@@ -309,7 +309,7 @@ select {
     }
     }
     button {
     button {
         color: #000;
         color: #000;
-        padding: 0;
-        border: none;
+        // padding: 0;
+        // border: none;
     }
     }
 }
 }

+ 8 - 6
src/actions/index.js

@@ -17,7 +17,9 @@ export const actionAddLikePostAC = (postId, newResult) => ({ type: 'ADD-POST-LIK
 export const actionRemoveLikePostAC = (postId, newResult) => ({ type: 'REMOVE-POST-LIKE', postId, newResult })
 export const actionRemoveLikePostAC = (postId, newResult) => ({ type: 'REMOVE-POST-LIKE', postId, newResult })
 export const actionAddCommentAC = (postId, newResult) => ({ type: 'ADD-COMMENT', postId, newResult })
 export const actionAddCommentAC = (postId, newResult) => ({ type: 'ADD-COMMENT', postId, newResult })
 
 
-export const actionAddProfileDataAC = (userData, userPosts) => ({ type: 'ADD-PROFILE-DATA', userData, userPosts })
+export const actionProfilePageDataAC = (userData, userPosts) => ({ type: 'PROFILE-PAGE-DATA', userData, userPosts })
+
+export const actionUpdateFollowingAC = (newResult) => ({ type: 'UPDATE-FOLLOWING', newResult })
 
 
 //****************---Action Authirization ---*************************//
 //****************---Action Authirization ---*************************//
 
 
@@ -36,7 +38,7 @@ export const actionRegister = (login, password) =>
 export const actionAboutMe = () =>
 export const actionAboutMe = () =>
     async (dispatch, getState) => {
     async (dispatch, getState) => {
         const { auth: { payload: { sub: { id } } } } = getState()
         const { auth: { payload: { sub: { id } } } } = getState()
-        await dispatch(actionPromise('dataProfileAuth', gql(`query userOned($myID:String!){
+        await dispatch(actionPromise('aboutMe', gql(`query userOned($myID:String!){
                         UserFindOne(query: $myID){
                         UserFindOne(query: $myID){
                             _id  login nick
                             _id  login nick
                             avatar { _id url }
                             avatar { _id url }
@@ -105,7 +107,7 @@ export const actionFindComment = (postId) =>
 
 
 //****************---Action ProfileData ---*************************//
 //****************---Action ProfileData ---*************************//
 
 
-export const actionUserData = (_id) =>
+export const actionProfilePageData = (_id) =>
     actionPromise('userOneData', gql(` query userOned($id:String!){
     actionPromise('userOneData', gql(` query userOned($id:String!){
                         UserFindOne(query: $id){
                         UserFindOne(query: $id){
                             _id  login nick
                             _id  login nick
@@ -116,7 +118,7 @@ export const actionUserData = (_id) =>
                 }
                 }
             } `, { id: JSON.stringify([{ _id }]) }))
             } `, { id: JSON.stringify([{ _id }]) }))
 
 
-export const actionUserPost = (_id) =>
+export const actionProfilePagePost = (_id) =>
     actionPromise('userOneData', gql(` query userOned($id:String!){
     actionPromise('userOneData', gql(` query userOned($id:String!){
                 PostFind(query:$id){
                 PostFind(query:$id){
                     _id   images{url _id}
                     _id   images{url _id}
@@ -141,12 +143,12 @@ export const actionSubscribe = (myID, myFollowing, userId) =>
         }
         }
       }`, { user: { _id: myID, following: [...myFollowing || [], { _id: userId }] } }))
       }`, { user: { _id: myID, following: [...myFollowing || [], { _id: userId }] } }))
 
 
-export const actionUnSubscribe = (myID, myFollowing, userId) =>
+export const actionUnSubscribe = (myID, myFollowing ) =>
     actionPromise('unSubscribe', gql(`mutation followingUn($user:UserInput){
     actionPromise('unSubscribe', gql(`mutation followingUn($user:UserInput){
         UserUpsert( user:$user){
         UserUpsert( user:$user){
             following{_id}
             following{_id}
         }
         }
-      }`, { user: { _id: myID, following: [...myFollowing || [], { _id: userId }] } }))
+      }`, { user: { _id: myID, following: [...myFollowing] } }))
 
 
 
 
 
 

+ 6 - 6
src/components/header/Header.jsx

@@ -10,13 +10,13 @@ import Layout, { Header } from 'antd/lib/layout/layout';
 import { Avatar, Col, Menu, Popover, Row } from 'antd';
 import { Avatar, Col, Menu, Popover, Row } from 'antd';
 import { UserOutlined, CompassOutlined, SettingOutlined, HomeOutlined, ImportOutlined, MessageOutlined, PlusCircleOutlined } from '@ant-design/icons/lib/icons';
 import { UserOutlined, CompassOutlined, SettingOutlined, HomeOutlined, ImportOutlined, MessageOutlined, PlusCircleOutlined } from '@ant-design/icons/lib/icons';
 
 
-const UserNav = ({ id}) => {
+const UserNav = ({ id }) => {
     return <div className='UserNav'>
     return <div className='UserNav'>
         <CUserNavIcon />
         <CUserNavIcon />
     </div>
     </div>
 }
 }
 
 
-export const UserAvatar = ({ avatarSize, avatar}) => {
+export const UserAvatar = ({ avatarSize, avatar }) => {
     return (
     return (
         <>
         <>
             <Avatar style={{
             <Avatar style={{
@@ -32,10 +32,10 @@ export const UserAvatar = ({ avatarSize, avatar}) => {
     )
     )
 }
 }
 
 
-const ProfileDropMenu = ({ onLogOut }) =>
+const ProfileDropMenu = ( {myID, onLogOut }) =>
     <Menu className='dropMenu'>
     <Menu className='dropMenu'>
         <Menu.Item key={'0'}>
         <Menu.Item key={'0'}>
-            <Link to={'/'}><UserOutlined /> My Profile</Link>
+            <Link to={`${myID}`}><UserOutlined /> My Profile</Link>
         </Menu.Item>
         </Menu.Item>
         <Menu.Item key={'1'}>
         <Menu.Item key={'1'}>
             <Link to={'/'}><SettingOutlined /> Settings</Link>
             <Link to={'/'}><SettingOutlined /> Settings</Link>
@@ -70,9 +70,9 @@ const UserNavIcon = ({ userData: { _id, avatar, login } }) =>
         </Col>
         </Col>
     </Row >
     </Row >
 
 
-const CUserNav = connect(state => ({ id: state.auth?.payload.sub.id || {} }) )(UserNav)
+const CUserNav = connect(state => ({ id: state.auth?.payload.sub.id || {} }))(UserNav)
 
 
-const CUserNavIcon = connect(state => ({ userData: state.promise?.dataProfileAuth.payload || {} }))(UserNavIcon)
+const CUserNavIcon = connect(state => ({ userData: state.promise?.aboutMe?.payload || {} }))(UserNavIcon)
 
 
 const Logo = () =>
 const Logo = () =>
     <Link className='Logo' to='/'>
     <Link className='Logo' to='/'>

+ 3 - 1
src/components/header/Search.jsx

@@ -27,7 +27,9 @@ const FindUsersResult = ({ usersRes }) => {
 export const FieldSearch = ({ usersRes, findUsers }) => {
 export const FieldSearch = ({ usersRes, findUsers }) => {
     return (
     return (
         <>
         <>
-            <Popover placement="bottom" content={<FindUsersResult usersRes={usersRes} />}
+            <Popover placement="bottom"
+                content={<FindUsersResult usersRes={usersRes} />}
+                destroyTooltipOnHide={true}
                 trigger="focus">
                 trigger="focus">
                 <></>
                 <></>
                 <Search className='Header__search'
                 <Search className='Header__search'

+ 1 - 1
src/components/main/Add.js

@@ -43,4 +43,4 @@ const Add = ({ imageUrl, onUploadFile }) => {
     )
     )
 }
 }
 
 
-export const CAdd = connect(state => ({ imageUrl: state.promise?.dataProfileAuth?.payload?.avatar.url }), { onUploadFile: actionSetAvatar })(Add)
+export const CAdd = connect(state => ({ imageUrl: state.promise?.Z?.payload?.avatar.url }), { onUploadFile: actionSetAvatar })(Add)

+ 11 - 12
src/components/main/Profile.js

@@ -3,7 +3,7 @@ import Modal from 'antd/lib/modal/Modal'
 import React, { useEffect, useState } from 'react'
 import React, { useEffect, useState } from 'react'
 import { connect } from 'react-redux'
 import { connect } from 'react-redux'
 import { Link } from 'react-router-dom'
 import { Link } from 'react-router-dom'
-import { actionFullSubscribe, actionProfilePageData } from '../../redux/redux-thunk'
+import { actionFullProfilePageData, actionFullSubscribe, actionFullUnSubscribe, actionProfilePageData } from '../../redux/redux-thunk'
 import { UserAvatar } from '../header/Header'
 import { UserAvatar } from '../header/Header'
 
 
 const ModalFolower = ({ statusModal, data, title }) => {
 const ModalFolower = ({ statusModal, data, title }) => {
@@ -42,22 +42,21 @@ const CModalFollowers = connect(state => ({ data: state?.profileData?.userData?.
 const CModalFollowing = connect(state => ({ data: state?.profileData?.userData?.following || [] }))(ModalFolower)
 const CModalFollowing = connect(state => ({ data: state?.profileData?.userData?.following || [] }))(ModalFolower)
 
 
 
 
-const ProfileSetting = ({ userId, onSubsuscribe }) => {
-    useEffect(() => {
-
-        return () => {
-
-        }
-    }, [])
-    console.log(userId);
+const ProfileSetting = ({ myID, userId, followers, onSubsuscribe, onUnSubsuscribe }) => {
+    const followCheck = followers.find(f => f._id === myID && true)
     return (
     return (
         <Col className='Profile__seting' offset={4}>
         <Col className='Profile__seting' offset={4}>
-            <Button onClick={() => onSubsuscribe(userId)} type="primary">Primary Button</Button>
+            {!!followCheck ?
+                <Button onClick={() => onUnSubsuscribe(userId)}>Primary Button</Button> :
+                <Button onClick={() => onSubsuscribe(userId)} type="primary">Primary Button</Button>}
         </Col>
         </Col>
     )
     )
 }
 }
 
 
-const CProfileSetting = connect(null, { onSubsuscribe: actionFullSubscribe })(ProfileSetting)
+const CProfileSetting = connect(state => ({
+    myID: state?.auth?.payload?.sub.id,
+    followers: state?.profileData?.userData?.followers || []
+}), { onSubsuscribe: actionFullSubscribe, onUnSubsuscribe: actionFullUnSubscribe })(ProfileSetting)
 
 
 const ProfilePageData = ({ data: { _id, avatar, login, nick, followers, following }, posts, setFollowing, setFollowers }) => {
 const ProfilePageData = ({ data: { _id, avatar, login, nick, followers, following }, posts, setFollowing, setFollowers }) => {
 
 
@@ -117,4 +116,4 @@ const ProfilePage = ({ match: { params: { _id, id } }, getProfileUser }) => {
     )
     )
 }
 }
 
 
-export const CProfilePage = connect(null, { getProfileUser: actionProfilePageData })(ProfilePage)
+export const CProfilePage = connect(null, { getProfileUser: actionFullProfilePageData })(ProfilePage)

+ 4 - 4
src/redux/profile-reducer.js

@@ -1,16 +1,16 @@
 import React from 'react'
 import React from 'react'
 
 
-export const profileReducer = (state = {}, { type, userData, userPosts }) => {
+export const profileReducer = (state = {}, { type, userData, userPosts, newResult }) => {
     const types = {
     const types = {
-        'ADD-PROFILE-DATA': () => {
+        'PROFILE-PAGE-DATA': () => {
             return {
             return {
                 ...state, userData, userPosts
                 ...state, userData, userPosts
             }
             }
         },
         },
-        'UPDATE-FOLLOWING':()=>{
+        'UPDATE-FOLLOWING': () => {
             return {
             return {
                 ...state,
                 ...state,
-                
+                userData: { ...state.userData, followers: [...newResult] }
             }
             }
         }
         }
     }
     }

+ 30 - 21
src/redux/redux-thunk.js

@@ -1,4 +1,4 @@
-import { actionAddComment, actionAddCommentAC, actionAddLikePost, actionAddLikePostAC, actionAddPostsFeedAC, actionAuthLogin, actionFindComment, actionLogin, actionMyLikePost, actionPending, actionRegister, actionRejected, actionRemoveLikePost, actionRemoveLikePostAC, actionResolved, actionUserData, actionUserPost, actionAddProfileDataAC, actionSubscribe, actionUpdateFollowers, actionAboutMe } from "../actions"
+import { actionAddComment, actionAddCommentAC, actionAddLikePost, actionAddLikePostAC, actionAddPostsFeedAC, actionAuthLogin, actionFindComment, actionLogin, actionMyLikePost, actionPending, actionRegister, actionRejected, actionRemoveLikePost, actionRemoveLikePostAC, actionResolved, actionSubscribe, actionUpdateFollowers, actionAboutMe, actionProfilePagePost, actionProfilePageDataAC, actionProfilePageData, actionUnSubscribe, actionUpdateFollowingAC } from "../actions"
 import { actionMyFolowisgPosts } from "./post-reducer"
 import { actionMyFolowisgPosts } from "./post-reducer"
 
 
 export const actionPromise = (name, promise) =>
 export const actionPromise = (name, promise) =>
@@ -25,7 +25,7 @@ export const actionFullLogin = (login, password, remember) =>
 
 
 export const actionFullRegister = (login, password, remember) =>
 export const actionFullRegister = (login, password, remember) =>
     async dispatch => {
     async dispatch => {
-        await actionRegister(login, password)
+        await dispatch(actionRegister(login, password))
         let token = await dispatch(actionLogin(login, password))
         let token = await dispatch(actionLogin(login, password))
         if (token) {
         if (token) {
             await dispatch(actionAuthLogin(token, remember))
             await dispatch(actionAuthLogin(token, remember))
@@ -34,9 +34,8 @@ export const actionFullRegister = (login, password, remember) =>
 
 
 export const actionFullRemoveLikePost = (likeId, postId) =>
 export const actionFullRemoveLikePost = (likeId, postId) =>
     async dispatch => {
     async dispatch => {
-        await actionRemoveLikePost(likeId)
+        await dispatch(actionRemoveLikePost(likeId))
         const { likes } = await dispatch(actionMyLikePost(postId))
         const { likes } = await dispatch(actionMyLikePost(postId))
-        console.log(likes);
         if (likes) {
         if (likes) {
             await dispatch(actionRemoveLikePostAC(postId, likes))
             await dispatch(actionRemoveLikePostAC(postId, likes))
         }
         }
@@ -44,7 +43,7 @@ export const actionFullRemoveLikePost = (likeId, postId) =>
 
 
 export const actionFullAddLikePost = (postId) =>
 export const actionFullAddLikePost = (postId) =>
     async dispatch => {
     async dispatch => {
-        await actionAddLikePost(postId)
+        await dispatch(actionAddLikePost(postId))
         const { likes } = await dispatch(actionMyLikePost(postId))
         const { likes } = await dispatch(actionMyLikePost(postId))
         if (likes) {
         if (likes) {
             await dispatch(actionAddLikePostAC(postId, likes))
             await dispatch(actionAddLikePostAC(postId, likes))
@@ -68,26 +67,36 @@ export const actionFullAddComment = (postId, text) =>
         }
         }
     }
     }
 
 
-export const actionProfilePageData = (id) =>
+export const actionFullProfilePageData = (id) =>
     async dispatch => {
     async dispatch => {
-        const userData = await dispatch(actionUserData(id))
-        const userPosts = await dispatch(actionUserPost(id))
-        if (userData, userPosts) {
-            await dispatch(actionAddProfileDataAC(userData, userPosts))
+        const userData = await dispatch(actionProfilePageData(id))
+        const userPosts = await dispatch(actionProfilePagePost(id))
+        if (userData && userPosts) {
+            await dispatch(actionProfilePageDataAC(userData))
+            await dispatch(actionAddPostsFeedAC (userPosts))
         }
         }
     }
     }
 
 
 export const actionFullSubscribe = (userId) =>
 export const actionFullSubscribe = (userId) =>
     async (dispatch, getState) => {
     async (dispatch, getState) => {
         const { auth: { payload: { sub: { id } } },
         const { auth: { payload: { sub: { id } } },
-            promise: { dataProfileAuth: { payload: { following } } } } = getState()
-        console.log(id, following, userId)
-        // actionSubscribe(id, following, userId)
-        const followers = await dispatch(actionUpdateFollowers(userId))
-        console.log(followers);
-        // const userData = await dispatch(actionUserData(id))
-        // const userPosts = await dispatch(actionUserPost(id))
-        // if (userData, userPosts) {
-        //     dispatch(actionAddProfileDataAC(userData, userPosts))
-        // }
-    }
+            promise: { aboutMe: { payload: { following } } } } = getState()
+        await dispatch(actionSubscribe(id, following, userId))
+        const { followers } = await dispatch(actionUpdateFollowers(userId))
+        if (followers) {
+            await dispatch(actionUpdateFollowingAC(followers))
+        }
+    }
+
+export const actionFullUnSubscribe = (userId) =>
+    async (dispatch, getState) => {
+        const { auth: { payload: { sub: { id } } },
+            promise: { aboutMe: { payload: { following } } } } = getState()
+        const newArrFollowing = [...following].filter(f => f._id !== userId)
+        await dispatch(actionUnSubscribe(id, newArrFollowing))
+        const { followers } = await dispatch(actionUpdateFollowers(userId))
+        if (followers) {
+            await dispatch(actionUpdateFollowingAC(followers))
+        }
+    }
+