From dcf1902fe132a1bf133a5c7ffd9fae96b5df1e5d Mon Sep 17 00:00:00 2001 From: canove Date: Wed, 6 Jan 2021 21:23:20 -0300 Subject: [PATCH] improvement: moved toast error logic into single function --- frontend/src/components/ContactModal/index.js | 26 ++--------- frontend/src/components/MessageInput/index.js | 35 ++------------- .../components/MessageOptionsMenu/index.js | 14 +----- frontend/src/components/MessagesList/index.js | 14 +----- .../src/components/NewTicketModal/index.js | 24 ++-------- frontend/src/components/QrcodeModal/index.js | 13 +----- frontend/src/components/Ticket/index.js | 16 +------ .../components/TicketActionButtons/index.js | 13 +----- .../src/components/TicketOptionsMenu/index.js | 14 +----- .../components/TransferTicketModal/index.js | 24 ++-------- frontend/src/components/UserModal/index.js | 26 ++--------- .../src/components/WhatsAppModal/index.js | 24 ++-------- frontend/src/context/Auth/useAuth.js | 12 +---- frontend/src/context/WhatsApp/useWhatsApps.js | 14 +----- frontend/src/errors/toastError.js | 21 +++++++++ frontend/src/hooks/useTickets/index.js | 14 +----- frontend/src/pages/Connections/index.js | 45 +++---------------- frontend/src/pages/Contacts/index.js | 45 +++---------------- frontend/src/pages/Settings/index.js | 24 ++-------- frontend/src/pages/Signup/index.js | 14 ++---- frontend/src/pages/Users/index.js | 23 ++-------- 21 files changed, 77 insertions(+), 378 deletions(-) create mode 100644 frontend/src/errors/toastError.js diff --git a/frontend/src/components/ContactModal/index.js b/frontend/src/components/ContactModal/index.js index 93aff6e..ce1e632 100644 --- a/frontend/src/components/ContactModal/index.js +++ b/frontend/src/components/ContactModal/index.js @@ -20,6 +20,7 @@ import CircularProgress from "@material-ui/core/CircularProgress"; import { i18n } from "../../translate/i18n"; import api from "../../services/api"; +import toastError from "../../errors/toastError"; const useStyles = makeStyles(theme => ({ root: { @@ -27,9 +28,7 @@ const useStyles = makeStyles(theme => ({ flexWrap: "wrap", }, textField: { - // marginLeft: theme.spacing(1), marginRight: theme.spacing(1), - // width: "25ch", flex: 1, }, @@ -40,7 +39,6 @@ const useStyles = makeStyles(theme => ({ }, btnWrapper: { - // margin: theme.spacing(1), position: "relative", }, @@ -97,16 +95,7 @@ const ContactModal = ({ open, onClose, contactId, initialValues, onSave }) => { setContact(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); } }; @@ -136,16 +125,7 @@ const ContactModal = ({ open, onClose, contactId, initialValues, onSave }) => { } toast.success(i18n.t("contactModal.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); } }; diff --git a/frontend/src/components/MessageInput/index.js b/frontend/src/components/MessageInput/index.js index f575909..a5222f7 100644 --- a/frontend/src/components/MessageInput/index.js +++ b/frontend/src/components/MessageInput/index.js @@ -2,7 +2,6 @@ import React, { useState, useEffect, useContext, useRef } from "react"; import "emoji-mart/css/emoji-mart.css"; import { useParams } from "react-router-dom"; import { Picker } from "emoji-mart"; -import { toast } from "react-toastify"; import MicRecorder from "mic-recorder-to-mp3"; import clsx from "clsx"; @@ -26,6 +25,7 @@ import { i18n } from "../../translate/i18n"; import api from "../../services/api"; import RecordingTimer from "./RecordingTimer"; import { ReplyMessageContext } from "../../context/ReplyingMessage/ReplyingMessageContext"; +import toastError from "../../errors/toastError"; const Mp3Recorder = new MicRecorder({ bitRate: 128 }); @@ -239,16 +239,7 @@ const MessageInput = ({ ticketStatus }) => { try { await api.post(`/messages/${ticketId}`, formData); } 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); @@ -271,16 +262,7 @@ const MessageInput = ({ ticketStatus }) => { try { await api.post(`/messages/${ticketId}`, message); } 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); } setInputMessage(""); @@ -320,16 +302,7 @@ const MessageInput = ({ ticketStatus }) => { await api.post(`/messages/${ticketId}`, formData); } 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); } setRecording(false); diff --git a/frontend/src/components/MessageOptionsMenu/index.js b/frontend/src/components/MessageOptionsMenu/index.js index 773feac..18fb243 100644 --- a/frontend/src/components/MessageOptionsMenu/index.js +++ b/frontend/src/components/MessageOptionsMenu/index.js @@ -1,7 +1,5 @@ import React, { useState, useContext } from "react"; -import { toast } from "react-toastify"; - import MenuItem from "@material-ui/core/MenuItem"; import { i18n } from "../../translate/i18n"; @@ -9,6 +7,7 @@ import api from "../../services/api"; import ConfirmationModal from "../ConfirmationModal"; import { Menu } from "@material-ui/core"; import { ReplyMessageContext } from "../../context/ReplyingMessage/ReplyingMessageContext"; +import toastError from "../../errors/toastError"; const MessageOptionsMenu = ({ message, menuOpen, handleClose, anchorEl }) => { const { setReplyingMessage } = useContext(ReplyMessageContext); @@ -18,16 +17,7 @@ const MessageOptionsMenu = ({ message, menuOpen, handleClose, anchorEl }) => { try { await api.delete(`/messages/${message.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); } }; diff --git a/frontend/src/components/MessagesList/index.js b/frontend/src/components/MessagesList/index.js index 22ac73a..3cb168e 100644 --- a/frontend/src/components/MessagesList/index.js +++ b/frontend/src/components/MessagesList/index.js @@ -26,9 +26,8 @@ import ModalImageCors from "../ModalImageCors"; import MessageOptionsMenu from "../MessageOptionsMenu"; import whatsBackground from "../../assets/wa-background.png"; -import { i18n } from "../../translate/i18n"; import api from "../../services/api"; -import { toast } from "react-toastify"; +import toastError from "../../errors/toastError"; const useStyles = makeStyles(theme => ({ messagesListWrapper: { @@ -343,16 +342,7 @@ const MessagesList = ({ ticketId, isGroup, setReplyingMessage }) => { } } catch (err) { setLoading(false); - 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); } }; fetchMessages(); diff --git a/frontend/src/components/NewTicketModal/index.js b/frontend/src/components/NewTicketModal/index.js index 746ae00..674f2e0 100644 --- a/frontend/src/components/NewTicketModal/index.js +++ b/frontend/src/components/NewTicketModal/index.js @@ -1,6 +1,5 @@ import React, { useState, useEffect } from "react"; import { useHistory } from "react-router-dom"; -import { toast } from "react-toastify"; import Button from "@material-ui/core/Button"; import TextField from "@material-ui/core/TextField"; @@ -18,6 +17,7 @@ import { i18n } from "../../translate/i18n"; import api from "../../services/api"; import ButtonWithSpinner from "../ButtonWithSpinner"; import ContactModal from "../ContactModal"; +import toastError from "../../errors/toastError"; const filter = createFilterOptions({ trim: true, @@ -50,16 +50,7 @@ const NewTicketModal = ({ modalOpen, onClose }) => { setLoading(false); } catch (err) { setLoading(false); - 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); } }; @@ -85,16 +76,7 @@ const NewTicketModal = ({ modalOpen, onClose }) => { }); 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); handleClose(); diff --git a/frontend/src/components/QrcodeModal/index.js b/frontend/src/components/QrcodeModal/index.js index 25ff7b4..78b75b6 100644 --- a/frontend/src/components/QrcodeModal/index.js +++ b/frontend/src/components/QrcodeModal/index.js @@ -1,7 +1,7 @@ import React, { useEffect, useState } from "react"; import QRCode from "qrcode.react"; import openSocket from "socket.io-client"; -import { toast } from "react-toastify"; +import toastError from "../../errors/toastError"; import { Dialog, DialogContent, Paper, Typography } from "@material-ui/core"; import { i18n } from "../../translate/i18n"; @@ -18,16 +18,7 @@ const QrcodeModal = ({ open, onClose, whatsAppId }) => { const { data } = await api.get(`/whatsapp/${whatsAppId}`); setQrCode(data.qrcode); } 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(); diff --git a/frontend/src/components/Ticket/index.js b/frontend/src/components/Ticket/index.js index 5075741..87c04e7 100644 --- a/frontend/src/components/Ticket/index.js +++ b/frontend/src/components/Ticket/index.js @@ -14,8 +14,8 @@ import TicketInfo from "../TicketInfo"; import TicketActionButtons from "../TicketActionButtons"; import MessagesList from "../MessagesList"; import api from "../../services/api"; -import { i18n } from "../../translate/i18n"; import { ReplyMessageProvider } from "../../context/ReplyingMessage/ReplyingMessageContext"; +import toastError from "../../errors/toastError"; const drawerWidth = 320; @@ -76,19 +76,7 @@ const Ticket = () => { setLoading(false); } catch (err) { setLoading(false); - 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); - } - if (err.response.status === 404) { - history.push("/tickets"); - } - } else { - toast.error("Unknown error"); - } + toastError(err); } }; fetchTicket(); diff --git a/frontend/src/components/TicketActionButtons/index.js b/frontend/src/components/TicketActionButtons/index.js index 1b05765..5a3a571 100644 --- a/frontend/src/components/TicketActionButtons/index.js +++ b/frontend/src/components/TicketActionButtons/index.js @@ -1,6 +1,5 @@ import React, { useState } from "react"; import { useHistory } from "react-router-dom"; -import { toast } from "react-toastify"; import { makeStyles } from "@material-ui/core/styles"; import { IconButton } from "@material-ui/core"; @@ -10,6 +9,7 @@ import { i18n } from "../../translate/i18n"; import api from "../../services/api"; import TicketOptionsMenu from "../TicketOptionsMenu"; import ButtonWithSpinner from "../ButtonWithSpinner"; +import toastError from "../../errors/toastError"; const useStyles = makeStyles(theme => ({ actionButtons: { @@ -55,16 +55,7 @@ const TicketActionButtons = ({ ticket }) => { } } catch (err) { setLoading(false); - 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); } }; diff --git a/frontend/src/components/TicketOptionsMenu/index.js b/frontend/src/components/TicketOptionsMenu/index.js index 6aa21a6..8087a89 100644 --- a/frontend/src/components/TicketOptionsMenu/index.js +++ b/frontend/src/components/TicketOptionsMenu/index.js @@ -1,7 +1,5 @@ import React, { useEffect, useRef, useState } from "react"; -import { toast } from "react-toastify"; - import MenuItem from "@material-ui/core/MenuItem"; import Menu from "@material-ui/core/Menu"; @@ -9,6 +7,7 @@ import { i18n } from "../../translate/i18n"; import api from "../../services/api"; import ConfirmationModal from "../ConfirmationModal"; import TransferTicketModal from "../TransferTicketModal"; +import toastError from "../../errors/toastError"; const TicketOptionsMenu = ({ ticket, menuOpen, handleClose, anchorEl }) => { const [confirmationOpen, setConfirmationOpen] = useState(false); @@ -25,16 +24,7 @@ const TicketOptionsMenu = ({ ticket, menuOpen, handleClose, anchorEl }) => { try { await api.delete(`/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); } }; diff --git a/frontend/src/components/TransferTicketModal/index.js b/frontend/src/components/TransferTicketModal/index.js index 192a910..024cec1 100644 --- a/frontend/src/components/TransferTicketModal/index.js +++ b/frontend/src/components/TransferTicketModal/index.js @@ -1,6 +1,5 @@ import React, { useState, useEffect } from "react"; import { useHistory } from "react-router-dom"; -import { toast } from "react-toastify"; import Button from "@material-ui/core/Button"; import TextField from "@material-ui/core/TextField"; @@ -17,6 +16,7 @@ import CircularProgress from "@material-ui/core/CircularProgress"; import { i18n } from "../../translate/i18n"; import api from "../../services/api"; import ButtonWithSpinner from "../ButtonWithSpinner"; +import toastError from "../../errors/toastError"; const filterOptions = createFilterOptions({ trim: true, @@ -45,16 +45,7 @@ const TransferTicketModal = ({ modalOpen, onClose, ticketid }) => { setLoading(false); } catch (err) { setLoading(false); - 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); } }; @@ -82,16 +73,7 @@ const TransferTicketModal = ({ modalOpen, onClose, ticketid }) => { history.push(`/tickets`); } catch (err) { setLoading(false); - 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); } }; diff --git a/frontend/src/components/UserModal/index.js b/frontend/src/components/UserModal/index.js index cff9516..dffdd6e 100644 --- a/frontend/src/components/UserModal/index.js +++ b/frontend/src/components/UserModal/index.js @@ -21,6 +21,7 @@ import FormControl from "@material-ui/core/FormControl"; import { i18n } from "../../translate/i18n"; import api from "../../services/api"; +import toastError from "../../errors/toastError"; const useStyles = makeStyles(theme => ({ root: { @@ -28,14 +29,11 @@ const useStyles = makeStyles(theme => ({ flexWrap: "wrap", }, textField: { - // marginLeft: theme.spacing(1), marginRight: theme.spacing(1), - // width: "25ch", flex: 1, }, btnWrapper: { - // margin: theme.spacing(1), position: "relative", }, @@ -83,16 +81,7 @@ const UserModal = ({ open, onClose, userId }) => { return { ...prevState, ...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); } }; @@ -113,16 +102,7 @@ const UserModal = ({ open, onClose, userId }) => { } toast.success(i18n.t("userModal.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); } handleClose(); }; diff --git a/frontend/src/components/WhatsAppModal/index.js b/frontend/src/components/WhatsAppModal/index.js index bb842bd..49e42df 100644 --- a/frontend/src/components/WhatsAppModal/index.js +++ b/frontend/src/components/WhatsAppModal/index.js @@ -18,9 +18,9 @@ import { FormControlLabel, } from "@material-ui/core"; -// import { i18n } from "../../translate/i18n"; import api from "../../services/api"; import { i18n } from "../../translate/i18n"; +import toastError from "../../errors/toastError"; const useStyles = makeStyles(theme => ({ form: { @@ -73,16 +73,7 @@ const WhatsAppModal = ({ open, onClose, whatsAppId }) => { const { data } = await api.get(`whatsapp/${whatsAppId}`); setWhatsApp(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(); @@ -100,16 +91,7 @@ const WhatsAppModal = ({ open, onClose, whatsAppId }) => { } toast.success(i18n.t("whatsappModal.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); } handleClose(); }; diff --git a/frontend/src/context/Auth/useAuth.js b/frontend/src/context/Auth/useAuth.js index b607954..b081bc0 100644 --- a/frontend/src/context/Auth/useAuth.js +++ b/frontend/src/context/Auth/useAuth.js @@ -5,6 +5,7 @@ import { toast } from "react-toastify"; import { i18n } from "../../translate/i18n"; import api from "../../services/api"; +import toastError from "../../errors/toastError"; const useAuth = () => { const history = useHistory(); @@ -78,16 +79,7 @@ const useAuth = () => { toast.success(i18n.t("auth.toasts.success")); history.push("/tickets"); } 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); diff --git a/frontend/src/context/WhatsApp/useWhatsApps.js b/frontend/src/context/WhatsApp/useWhatsApps.js index 4a81951..c4c24b9 100644 --- a/frontend/src/context/WhatsApp/useWhatsApps.js +++ b/frontend/src/context/WhatsApp/useWhatsApps.js @@ -1,9 +1,8 @@ import { useState, useEffect, useReducer } from "react"; -import { toast } from "react-toastify"; import openSocket from "socket.io-client"; +import toastError from "../../errors/toastError"; import api from "../../services/api"; -import { i18n } from "../../translate/i18n"; const reducer = (state, action) => { if (action.type === "LOAD_WHATSAPPS") { @@ -67,16 +66,7 @@ const useWhatsApps = () => { setLoading(false); } catch (err) { setLoading(false); - 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(); diff --git a/frontend/src/errors/toastError.js b/frontend/src/errors/toastError.js new file mode 100644 index 0000000..83fd728 --- /dev/null +++ b/frontend/src/errors/toastError.js @@ -0,0 +1,21 @@ +import { toast } from "react-toastify"; +import { i18n } from "../translate/i18n"; + +const toastError = err => { + const errorMsg = err.response?.data?.error; + if (errorMsg) { + if (i18n.exists(`backendErrors.${errorMsg}`)) { + toast.error(i18n.t(`backendErrors.${errorMsg}`), { + toastId: errorMsg, + }); + } else { + toast.error(errorMsg, { + toastId: errorMsg, + }); + } + } else { + toast.error("An error occurred!"); + } +}; + +export default toastError; diff --git a/frontend/src/hooks/useTickets/index.js b/frontend/src/hooks/useTickets/index.js index c43e779..e6eeed3 100644 --- a/frontend/src/hooks/useTickets/index.js +++ b/frontend/src/hooks/useTickets/index.js @@ -1,8 +1,7 @@ import { useState, useEffect } from "react"; -import { toast } from "react-toastify"; +import toastError from "../../errors/toastError"; import api from "../../services/api"; -import { i18n } from "../../translate/i18n"; const useTickets = ({ searchParam, @@ -36,16 +35,7 @@ const useTickets = ({ setLoading(false); } catch (err) { setLoading(false); - 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); } }; fetchTickets(); diff --git a/frontend/src/pages/Connections/index.js b/frontend/src/pages/Connections/index.js index fc0004e..f1f5fb0 100644 --- a/frontend/src/pages/Connections/index.js +++ b/frontend/src/pages/Connections/index.js @@ -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); } } diff --git a/frontend/src/pages/Contacts/index.js b/frontend/src/pages/Contacts/index.js index da1d546..6bb0f24 100644 --- a/frontend/src/pages/Contacts/index.js +++ b/frontend/src/pages/Contacts/index.js @@ -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); } }; diff --git a/frontend/src/pages/Settings/index.js b/frontend/src/pages/Settings/index.js index 4932c1d..4fd64ab 100644 --- a/frontend/src/pages/Settings/index.js +++ b/frontend/src/pages/Settings/index.js @@ -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); } }; diff --git a/frontend/src/pages/Signup/index.js b/frontend/src/pages/Signup/index.js index ce65758..e7aaf72 100644 --- a/frontend/src/pages/Signup/index.js +++ b/frontend/src/pages/Signup/index.js @@ -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); } }; diff --git a/frontend/src/pages/Users/index.js b/frontend/src/pages/Users/index.js index 140da07..7f4d440 100644 --- a/frontend/src/pages/Users/index.js +++ b/frontend/src/pages/Users/index.js @@ -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("");