Skip to content

Commit

Permalink
fix(axios): handling response error on client
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-ojha committed May 29, 2022
1 parent b2bd77b commit 6f539dd
Show file tree
Hide file tree
Showing 31 changed files with 233 additions and 122 deletions.
12 changes: 8 additions & 4 deletions client/src/components/CommentBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ const ReturnCommentContent = () => {
}
dispatch(stopProgressBar());
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down Expand Up @@ -128,8 +130,10 @@ const ReturnCommentContent = () => {
}
dispatch(stopProgressBar());
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down
18 changes: 12 additions & 6 deletions client/src/components/FollowedBy.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ const FollowedBy = () => {
dispatch(stopProgressBar());
}
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down Expand Up @@ -103,8 +105,10 @@ const FollowedBy = () => {
dispatch(stopProgressBar());
}
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down Expand Up @@ -135,8 +139,10 @@ const FollowedBy = () => {
}
dispatch(stopProgressBar());
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down
11 changes: 6 additions & 5 deletions client/src/components/HomePage/DisplayFollowedUserPost.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ const DisplayFollowedUserPost = () => {
/>
);
});
if (
followedUserPostDataStore.length === 1 &&
postElement[0].props.userMainInformation.userID ===
if (followedUserPostDataStore.length === 1 && postElement[0]) {
if (
postElement[0].props.userMainInformation.userID ===
admin.adminUserID
) {
return [<DefaultSocialPost key={key} />, ...postElement];
) {
return [<DefaultSocialPost key={key} />, ...postElement];
}
} else if (postElement.length > 0) {
return postElement;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ const PostButton = () => {
dispatch(showLoadingSpinner(false));
dispatch(setHomePagePostFieldViewValue("min"));
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down
12 changes: 8 additions & 4 deletions client/src/components/MainPageMsgAndNtfBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ const MainPageMsgAndNtfBar = () => {
toastError(data.msg);
}
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand All @@ -82,8 +84,10 @@ const MainPageMsgAndNtfBar = () => {
}
dispatch(stopProgressBar());
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down
1 change: 0 additions & 1 deletion client/src/components/MainPageStoryComp/MainPageStory.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import FriendStory from "./FriendStory";
const MainPageStory = () => {
const userStoriesStore = useSelector((state) => state.userStoriesReducer);
// console.log(userStoriesStore);
const storiesData = userStoriesStore.data;
const isMobile = useMediaQuery({
query: "(max-width: 480px)",
});
Expand Down
68 changes: 36 additions & 32 deletions client/src/components/MessageBox/MessagesListSingleMessage.jsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@
import React from "react";
import User_Profile_Icon from "../../assets/svg/User_profile_Icon.svg";
import socket from "../../services/socket";
import {
currentUserMessageAction,
mainPageMessageInnerViewOnOff,
} from "../../services/redux-actions/index";
import { useDispatch } from "react-redux";
import { instance as axios } from "../../services/axios";
import { toastError } from "../../services/toast";

const MessagesListSingleMessage = (props) => {
const dispatch = useDispatch();
const showInnerMessage = async () => {
// before getting new message we will reset the previous message stored into redux
dispatch(
currentUserMessageAction({
messageToId: props.messageInfo.messageToId,
messageToUserId: props.messageInfo.messageToUserId,
receiverPicture: props.messageInfo.receiverPicture,
message: [],
fetchedInnerMessage: false,
})
);
dispatch(mainPageMessageInnerViewOnOff(true));
const resMessage = await axios({
// sending receiver userID to get message data of that user
method: "POST",
url: "/u/getMessage",
headers: {
"Content-Type": "application/json",
},
data: JSON.stringify({
userID: props.messageInfo.messageToUserId,
id: props.messageInfo.messageToId,
}),
withCredentials: true,
});
if (resMessage.status !== 200) {
const error = await resMessage.data;
} else {
const resData = await resMessage.data;
// after getting message we will store that message into redux
try {
// before getting new message we will reset the previous message stored into redux
dispatch(
currentUserMessageAction({
messageToId: props.messageInfo.messageToId,
messageToUserId: props.messageInfo.messageToUserId,
receiverPicture: props.messageInfo.receiverPicture,
message: resData.message,
fetchedInnerMessage: true,
message: [],
fetchedInnerMessage: false,
})
);
dispatch(mainPageMessageInnerViewOnOff(true));
const resMessage = await axios({
// sending receiver userID to get message data of that user
method: "POST",
url: "/u/getMessage",
headers: {
"Content-Type": "application/json",
},
data: JSON.stringify({
userID: props.messageInfo.messageToUserId,
id: props.messageInfo.messageToId,
}),
withCredentials: true,
});
if (resMessage.status !== 200) {
const error = await resMessage.data;
} else {
const resData = await resMessage.data;
// after getting message we will store that message into redux
dispatch(
currentUserMessageAction({
messageToId: props.messageInfo.messageToId,
messageToUserId: props.messageInfo.messageToUserId,
receiverPicture: props.messageInfo.receiverPicture,
message: resData.message,
fetchedInnerMessage: true,
})
);
}
} catch (err) {
toastError("Some Problem Occur, Please Try again later!!!");
}
};

Expand Down
6 changes: 4 additions & 2 deletions client/src/components/MessagePage/MessagesList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ const MessagesList = () => {
}
setShowLoadingSpinner(false);
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ const MessageListSingleMessage = (props) => {
);
}
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down
3 changes: 2 additions & 1 deletion client/src/components/MessagePage/SendMessageInputField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "../../services/redux-actions/index";
import { Icon } from "@iconify/react";
import { isEmptyString } from "../../funcs/isEmptyString";
import { toastError } from "../../services/toast";

const SendMessageInputField = (props) => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -57,7 +58,7 @@ const SendMessageInputField = (props) => {
}
});
} catch (err) {
// console.log(err);
toastError("Some Problem Occur, Please Try again later!!!");
}
};

Expand Down
6 changes: 4 additions & 2 deletions client/src/components/PostBox/CommentField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ const CommentField = (props) => {
}
dispatch(stopProgressBar());
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down
6 changes: 4 additions & 2 deletions client/src/components/PostBox/CommentedUser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ const CommentedUser = (props) => {
}
dispatch(stopProgressBar());
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down
12 changes: 8 additions & 4 deletions client/src/components/PostBox/LikeCommentShare.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ const LikeCommentShare = (props) => {
});
}
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down Expand Up @@ -80,8 +82,10 @@ const LikeCommentShare = (props) => {
}
dispatch(stopProgressBar());
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down
6 changes: 4 additions & 2 deletions client/src/components/PostBox/PostInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ const PostInfo = (props) => {
}
dispatch(stopProgressBar());
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur, Please Try again later!!!");
}
Expand Down
24 changes: 15 additions & 9 deletions client/src/components/ProfileFriends.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ const ProfileFriends = () => {
toastError("Some Error Occur While Fetching Friends Data");
}
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur while fetching friends data");
toastError("Some Problem Occur, Please Try again later!!!");
}
}
};
Expand All @@ -82,10 +84,12 @@ const ProfileFriends = () => {
toastError("Some Error Occur While Fetching Friends Data");
}
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur while fetching friends data");
toastError("Some Problem Occur, Please Try again later!!!");
}
}
};
Expand All @@ -104,10 +108,12 @@ const ProfileFriends = () => {
toastError("Some Error Occur While Fetching Friends Data");
}
} catch (err) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
if (err.response) {
if (err.response.data.success === false) {
toastError(err.response.data.msg);
}
} else {
toastError("Some Problem Occur while fetching friends data");
toastError("Some Problem Occur, Please Try again later!!!");
}
}
};
Expand Down
Loading

1 comment on commit 6f539dd

@roman-ojha
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for social ready!

✅ Preview
https://social-jofgs60mb-razzroman98-gmailcom.vercel.app
https://rsocial.vercel.app

Built with commit 6f539dd.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.