Skip to content

Commit

Permalink
feat(react_hook): added routeToProfilePage on PostBox
Browse files Browse the repository at this point in the history
fixing #124
  • Loading branch information
roman-ojha committed Jul 8, 2022
1 parent 5b06824 commit d3992e7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 211 deletions.
63 changes: 10 additions & 53 deletions client/src/components/FollowedBy.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react";
import { useSelector, useDispatch } from "react-redux";
import "../styles/components/userSuggestionFollowdBySponsoredBy.css";
import { useHistory } from "react-router-dom";
import { toastError, toastSuccess } from "../services/toast";
// import {
// profilePageDataAction,
Expand All @@ -12,24 +11,23 @@ import { toastError, toastSuccess } from "../services/toast";
// } from "../services/redux-actions";
import { instance as axios } from "../services/axios";
import User_Profile_Icon from "../assets/svg/User_profile_Icon.svg";
import GlobalApi from "../services/api/global";
import constant from "../constant/constant";
import { useMediaQuery } from "react-responsive";
import { AppState, actionCreators } from "../services/redux";
import { bindActionCreators } from "redux";
import { AxiosError } from "axios";
import { Button } from "@mui/material";
import { makeStyles } from "@mui/styles";
import { ProfilePageDataState } from "../services/redux/pages/profile/profilePageData/types";
import useRouteToProfilePage from "../hooks/useRouteToProfilePage";

const buttonStyle = makeStyles({
root: {},
buttonRipple: { color: "var(--white-opacity-3)" },
});

const FollowedBy = (): JSX.Element => {
const history = useHistory();
const dispatch = useDispatch();
const routeToProfilePage = useRouteToProfilePage();
const mainPageMessageOnOffState = useSelector(
(state: AppState) => state.changeMainPageMessageView
);
Expand All @@ -43,13 +41,8 @@ const FollowedBy = (): JSX.Element => {
query: `(max-width:${constant.mediaQueryRes.screen850}px)`,
});

const {
profilePageDataAction,
isFollowedFollowedByUser,
startProgressBar,
stopProgressBar,
openRightPartDrawer,
} = bindActionCreators(actionCreators, dispatch);
const { isFollowedFollowedByUser, startProgressBar, stopProgressBar } =
bindActionCreators(actionCreators, dispatch);

interface FollowedUserProps {
userInformation: any;
Expand Down Expand Up @@ -147,39 +140,6 @@ const FollowedBy = (): JSX.Element => {
}
};

const routeToProfile = async (userID: string): Promise<void> => {
try {
startProgressBar();
const res = await GlobalApi.getFriendData(userID);
const userData = await res.data;
if (res.status === 200 && userData.success) {
const userObj: ProfilePageDataState = {
...userData.searchedUser,
isRootUserFollowed: userData.isRootUserFollowed,
throughRouting: true,
};
profilePageDataAction(userObj);
if (isMax850px) {
openRightPartDrawer(false);
}
history.push(`/u/profile/${userID}/posts`);
} else {
toastError(userData.msg);
}
stopProgressBar();
} catch (error) {
const err = error as AxiosError;
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
stopProgressBar();
}
};

return (
<>
<div className="MainPage_Followed_User_Container">
Expand All @@ -190,10 +150,9 @@ const FollowedBy = (): JSX.Element => {
? userInformation.picture
: User_Profile_Icon
}
onClick={() => {
onClick={async () => {
if (userInformation.type !== "bot") {
// history.push(`/u/profile/${props.userInformation.userID}`);
routeToProfile(userInformation.userID);
await routeToProfilePage({ userID: userInformation.userID });
} else {
toastError("Sorry!!, can't be able to open bot Profile");
}
Expand All @@ -203,10 +162,9 @@ const FollowedBy = (): JSX.Element => {
<div className="MainPage_Followed_User_Name_Container">
<p
className="MainPage_Followed_User_Name"
onClick={() => {
onClick={async () => {
if (userInformation.type !== "bot") {
// history.push(`/u/profile/${props.userInformation.userID}`);
routeToProfile(userInformation.userID);
await routeToProfilePage({ userID: userInformation.userID });
} else {
toastError("Sorry!!, can't be able to open bot Profile");
}
Expand All @@ -216,10 +174,9 @@ const FollowedBy = (): JSX.Element => {
</p>
<p
className="MainPage_Followed_User_Follower_Name"
onClick={() => {
onClick={async () => {
if (userInformation.type !== "bot") {
// history.push(`/u/profile/${props.userInformation.userID}`);
routeToProfile(userInformation.userID);
await routeToProfilePage({ userID: userInformation.userID });
} else {
toastError("Sorry!!, can't be able to open bot Profile");
}
Expand Down
40 changes: 3 additions & 37 deletions client/src/components/NotificationBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import User_Profile_Icon from "../assets/svg/User_profile_Icon.svg";
// profilePageDataAction,
// openRightPartDrawer,
// } from "../services/redux-actions";
import GlobalApi from "../services/api/global";
import { toastError } from "../services/toast";
import { useHistory } from "react-router-dom";
import { useMediaQuery } from "react-responsive";
import constant from "../constant/constant";
import { bindActionCreators } from "redux";
import { AppState, actionCreators } from "../services/redux";
import { Button } from "@mui/material";
import { makeStyles } from "@mui/styles";
import { ProfilePageDataState } from "../services/redux/pages/profile/profilePageData/types";
import useRouteToProfilePage from "../hooks/useRouteToProfilePage";

const buttonStyle = makeStyles({
root: {},
Expand All @@ -27,7 +25,7 @@ const buttonStyle = makeStyles({
const NotificationBox = (): JSX.Element => {
const ButtonClass = buttonStyle();
const dispatch = useDispatch();
const history = useHistory();
const routeToProfilePage = useRouteToProfilePage();
const notificationBoxState = useSelector(
(state: AppState) => state.notificationBox
);
Expand Down Expand Up @@ -65,39 +63,7 @@ const NotificationBox = (): JSX.Element => {
<div
id="Notification_Box_Single_Nft_Pic_and_Title"
onClick={async () => {
try {
startProgressBar();
const res = await GlobalApi.getFriendData(
data.userID
);
const userData = await res.data;
if (res.status === 200 && userData.success) {
// success
const userObj: ProfilePageDataState = {
...userData.searchedUser,
isRootUserFollowed: userData.isRootUserFollowed,
throughRouting: true,
};
profilePageDataAction(userObj);
history.push(`/u/profile/${data.userID}/posts`);
if (isMax850px) {
openRightPartDrawer(false);
}
} else {
// error
toastError(userData.msg);
}
stopProgressBar();
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
} else {
toastError(
"Some Problem Occur, Please Try again later!!!"
);
}
stopProgressBar();
}
await routeToProfilePage({ userID: data.userID });
}}
>
<img
Expand Down
64 changes: 13 additions & 51 deletions client/src/components/PostBox/CommentedUser.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import React, { useEffect, memo } from "react";
import User_Profile_Icon from "../../assets/svg/User_profile_Icon.svg";
import GlobalApi from "../../services/api/global";
import { useDispatch, useSelector } from "react-redux";
import { toastError } from "../../services/toast";
// import {
// startProgressBar,
// stopProgressBar,
// profilePageDataAction,
// setRootUserProfileDataState,
// commentBoxAction,
// } from "../../services/redux-actions";
import { useHistory } from "react-router-dom";
import { AppState, actionCreators } from "../../../src/services/redux";
import { CommentInfoState, PostBoxProps } from "./PostBox";
import { bindActionCreators } from "redux";
import { ProfilePageDataState } from "../../services/redux/pages/profile/profilePageData/types";
import useRouteToProfilePage from "../../hooks/useRouteToProfilePage";

interface CommentedUserProps {
commentInfo: CommentInfoState;
Expand All @@ -28,57 +25,14 @@ const CommentedUser: React.FC<CommentedUserProps> = ({
setCommentInfo,
}): JSX.Element => {
const dispatch = useDispatch();
const history = useHistory();
const routeToProfilePage = useRouteToProfilePage();
const userProfileDetailStore = useSelector(
(state: AppState) => state.setUserProfileDetailReducer
);
const commentBoxStore = useSelector(
(state: AppState) => state.commentBoxReducer
);
const {
startProgressBar,
stopProgressBar,
profilePageDataAction,
setRootUserProfileDataState,
commentBoxAction,
} = bindActionCreators(actionCreators, dispatch);

const routeToProfile = async (userID: string): Promise<void> => {
try {
startProgressBar();
const res = await GlobalApi.getFriendData(userID);
const userData = await res.data;
if (res.status === 200 && userData.success) {
// success
const userObj: ProfilePageDataState = {
...userData.searchedUser,
isRootUserFollowed: userData.isRootUserFollowed,
throughRouting: true,
};
profilePageDataAction(userObj);
if (userID === userProfileDetailStore.userID) {
setRootUserProfileDataState({
fetchedRootUserProfileData: true,
getRootUserProfileData: false,
});
}
history.push(`/u/profile/${userID}/posts`);
} else {
// error
toastError(userData.msg);
}
stopProgressBar();
} catch (err) {
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
stopProgressBar();
}
};
const { commentBoxAction } = bindActionCreators(actionCreators, dispatch);

useEffect(() => {
if (commentBoxStore.commented && commentBoxStore.postID === postId) {
Expand Down Expand Up @@ -117,13 +71,21 @@ const CommentedUser: React.FC<CommentedUserProps> = ({
}
alt={commentInfo.postCommentInfo.userID}
onClick={() => {
routeToProfile(commentInfo.postCommentInfo.userID);
// routeToProfile(commentInfo.postCommentInfo.userID);
routeToProfilePage({
userID: commentInfo.postCommentInfo.userID,
from: "postBox",
});
}}
/>
<div>
<h3
onClick={() => {
routeToProfile(commentInfo.postCommentInfo.userID);
// routeToProfile(commentInfo.postCommentInfo.userID);
routeToProfilePage({
userID: commentInfo.postCommentInfo.userID,
from: "postBox",
});
}}
>
{commentInfo.postCommentInfo.userID}
Expand Down
Loading

0 comments on commit d3992e7

Please sign in to comment.