improvement: moved toast error logic into single function

This commit is contained in:
canove
2021-01-06 21:23:20 -03:00
parent 8ecd52e2b1
commit dcf1902fe1
21 changed files with 77 additions and 378 deletions

View File

@@ -39,6 +39,7 @@ import ConfirmationModal from "../../components/ConfirmationModal";
import QrcodeModal from "../../components/QrcodeModal";
import { i18n } from "../../translate/i18n";
import { WhatsAppsContext } from "../../context/WhatsApp/WhatsAppsContext";
import toastError from "../../errors/toastError";
const useStyles = makeStyles(theme => ({
mainPaper: {
@@ -114,16 +115,7 @@ const Connections = () => {
try {
await api.post(`/whatsappsession/${whatsAppId}`);
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
};
@@ -131,16 +123,7 @@ const Connections = () => {
try {
await api.put(`/whatsappsession/${whatsAppId}`);
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
};
@@ -195,16 +178,7 @@ const Connections = () => {
try {
await api.delete(`/whatsappsession/${confirmModalInfo.whatsAppId}`);
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
}
@@ -213,16 +187,7 @@ const Connections = () => {
await api.delete(`/whatsapp/${confirmModalInfo.whatsAppId}`);
toast.success(i18n.t("connections.toasts.deleted"));
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
}

View File

@@ -31,6 +31,7 @@ import MainHeader from "../../components/MainHeader";
import Title from "../../components/Title";
import MainHeaderButtonsWrapper from "../../components/MainHeaderButtonsWrapper";
import MainContainer from "../../components/MainContainer";
import toastError from "../../errors/toastError";
const reducer = (state, action) => {
if (action.type === "LOAD_CONTACTS") {
@@ -117,16 +118,7 @@ const Contacts = () => {
setHasMore(data.hasMore);
setLoading(false);
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
};
fetchContacts();
@@ -176,16 +168,7 @@ const Contacts = () => {
});
history.push(`/tickets/${ticket.id}`);
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
setLoading(false);
};
@@ -200,16 +183,7 @@ const Contacts = () => {
await api.delete(`/contacts/${contactId}`);
toast.success(i18n.t("contacts.toasts.deleted"));
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
setDeletingContact(null);
setSearchParam("");
@@ -221,16 +195,7 @@ const Contacts = () => {
await api.post("/contacts/import");
history.go(0);
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
};

View File

@@ -10,6 +10,7 @@ import { toast } from "react-toastify";
import api from "../../services/api";
import { i18n } from "../../translate/i18n.js";
import toastError from "../../errors/toastError";
const useStyles = makeStyles(theme => ({
root: {
@@ -43,16 +44,7 @@ const Settings = () => {
const { data } = await api.get("/settings");
setSettings(data);
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
};
fetchSession();
@@ -62,7 +54,6 @@ const Settings = () => {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
socket.on("settings", data => {
if (data.action === "update") {
// dispatch({ type: "UPDATE_USERS", payload: data.user });
setSettings(prevState => {
const aux = [...prevState];
const settingIndex = aux.findIndex(s => s.key === data.setting.key);
@@ -87,16 +78,7 @@ const Settings = () => {
});
toast.success(i18n.t("settings.success"));
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
};

View File

@@ -21,6 +21,7 @@ import Container from "@material-ui/core/Container";
import { i18n } from "../../translate/i18n";
import api from "../../services/api";
import toastError from "../../errors/toastError";
// const Copyright = () => {
// return (
@@ -47,7 +48,7 @@ const useStyles = makeStyles(theme => ({
backgroundColor: theme.palette.secondary.main,
},
form: {
width: "100%", // Fix IE 11 issue.
width: "100%",
marginTop: theme.spacing(3),
},
submit: {
@@ -78,16 +79,7 @@ const SignUp = () => {
toast.success(i18n.t("signup.toasts.success"));
history.push("/login");
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
};

View File

@@ -29,6 +29,7 @@ import TableRowSkeleton from "../../components/TableRowSkeleton";
import UserModal from "../../components/UserModal";
import ConfirmationModal from "../../components/ConfirmationModal";
import { Avatar } from "@material-ui/core";
import toastError from "../../errors/toastError";
const reducer = (state, action) => {
if (action.type === "LOAD_USERS") {
@@ -113,16 +114,7 @@ const Users = () => {
setHasMore(data.hasMore);
setLoading(false);
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
};
fetchUsers();
@@ -171,16 +163,7 @@ const Users = () => {
await api.delete(`/users/${userId}`);
toast.success(i18n.t("users.toasts.deleted"));
} catch (err) {
const errorMsg = err.response?.data?.error;
if (errorMsg) {
if (i18n.exists(`backendErrors.${errorMsg}`)) {
toast.error(i18n.t(`backendErrors.${errorMsg}`));
} else {
toast.error(err.response.data.error);
}
} else {
toast.error("Unknown error");
}
toastError(err);
}
setDeletingUser(null);
setSearchParam("");