|
@@ -1,49 +1,75 @@
|
|
|
import {connect} from "react-redux";
|
|
|
import {useEffect, useState} from "react";
|
|
|
-import {gqlGetUserFollowers, gqlSubscribe, gqlUnSubscribe} from "../shared/services&utilits/gqlRequest";
|
|
|
+import {
|
|
|
+ gqlGetFollowings,
|
|
|
+ gqlGetUserFollowers,
|
|
|
+ gqlSubscribe,
|
|
|
+ gqlUnSubscribe
|
|
|
+} from "../shared/services&utilits/gqlRequest";
|
|
|
import {
|
|
|
setActiveUserData,
|
|
|
} from "../store/actionCreators/ActionCreators";
|
|
|
+import styled from 'styled-components';
|
|
|
+import {StyledButton} from "../shared/styledComponents";
|
|
|
+
|
|
|
+const SubscribeBtn = styled(StyledButton)`
|
|
|
+ width: 30%`
|
|
|
|
|
|
|
|
|
-function SubscribeButton({followers, id, activeUser, setActiveUserData, ownerData}){
|
|
|
+function SubscribeButton({followers, id, activeUser, setActiveUserData, ownerData}) {
|
|
|
const [isSabscribed, setIsSabscribed] = useState(isAlreadySubscribed())
|
|
|
- console.log(followers)
|
|
|
- useEffect(()=>{
|
|
|
- if (isAlreadySubscribed() && !isSabscribed){
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (isAlreadySubscribed() && !isSabscribed) {
|
|
|
setIsSabscribed(true)
|
|
|
}
|
|
|
- if (!isAlreadySubscribed() && isSabscribed){
|
|
|
+ if (!isAlreadySubscribed() && isSabscribed) {
|
|
|
setIsSabscribed(false)
|
|
|
}
|
|
|
})
|
|
|
|
|
|
function isAlreadySubscribed() {
|
|
|
- return followers && followers.find((follower)=>follower._id === id);
|
|
|
+ return followers && followers.find((follower) => follower._id === id);
|
|
|
+ }
|
|
|
+
|
|
|
+ async function getFollowings() {
|
|
|
+ let data = null;
|
|
|
+ const responce = await gqlGetFollowings(id);
|
|
|
+ if (responce.ok){
|
|
|
+ const following = await responce.json();
|
|
|
+ data = following.data.UserFindOne.following;
|
|
|
+ }
|
|
|
+ return data;
|
|
|
}
|
|
|
|
|
|
- function clickHandler(){
|
|
|
- if (!isSabscribed){
|
|
|
- gqlSubscribe(id, activeUser._id, []).then((res)=> res.ok && updateUserFollowers())
|
|
|
+
|
|
|
+ async function clickHandler() {
|
|
|
+ const following = await getFollowings();
|
|
|
+ const followingIdArrayToAdd = following.map((item) => ({_id: item._id}))
|
|
|
+ const followingIdArrayToRemove = followingIdArrayToAdd.filter(item => item._id !== activeUser._id)
|
|
|
+
|
|
|
+ if (!isSabscribed) {
|
|
|
+ gqlSubscribe(id, activeUser._id, followingIdArrayToAdd).then((res) => res.json()).then(data => data && updateUserFollowers())
|
|
|
} else {
|
|
|
- gqlUnSubscribe(id, []).then((res)=> res.ok && updateUserFollowers())
|
|
|
+ gqlUnSubscribe(id, followingIdArrayToRemove).then((res) => res.json()).then(data => data && updateUserFollowers())
|
|
|
}
|
|
|
}
|
|
|
- async function updateUserFollowers(){
|
|
|
- const a= await gqlGetUserFollowers(activeUser._id)
|
|
|
+
|
|
|
+ async function updateUserFollowers() {
|
|
|
+ const a = await gqlGetUserFollowers(activeUser._id)
|
|
|
const res = await a.json()
|
|
|
- if (res){
|
|
|
- activeUser.followers = await res.data.UserFindOne.followers;
|
|
|
- console.log(activeUser)
|
|
|
+ if (res) {
|
|
|
+ activeUser.followers = await res.data.UserFindOne.followers;
|
|
|
setActiveUserData(activeUser);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return <button onClick={clickHandler}>{isSabscribed ? "unsabscribe" : "sabscribe"}</button>
|
|
|
+ return <SubscribeBtn onClick={clickHandler}>{isSabscribed ? "unsabscribe" : "sabscribe"}</SubscribeBtn>
|
|
|
}
|
|
|
+
|
|
|
const mapStateToProps = (state) => {
|
|
|
return {
|
|
|
- id: state.auth.user.sub.id,
|
|
|
+ id: state.auth?.user?.sub?.id,
|
|
|
ownerData: state.ownerData,
|
|
|
activeUser: state.activeUser,
|
|
|
}
|