From 5150179193126052b884d573cbac8fd05eb83b2e Mon Sep 17 00:00:00 2001 From: Daniel Cojocea Date: Thu, 18 Jul 2024 10:43:46 -0400 Subject: [PATCH] Simplified toast function call --- .../Components/TabPanels/Account/PasswordPanel.jsx | 4 ---- .../Components/TabPanels/Account/ProfilePanel.jsx | 10 ---------- Client/src/Pages/Auth/CheckEmail.jsx | 14 -------------- Client/src/Pages/Auth/ForgotPassword.jsx | 6 ------ Client/src/Pages/Auth/Login.jsx | 8 -------- Client/src/Pages/Auth/Register.jsx | 8 -------- Client/src/Pages/Auth/SetNewPassword.jsx | 8 -------- Client/src/Utils/toastUtils.jsx | 12 +++++++++--- 8 files changed, 9 insertions(+), 61 deletions(-) diff --git a/Client/src/Components/TabPanels/Account/PasswordPanel.jsx b/Client/src/Components/TabPanels/Account/PasswordPanel.jsx index 97ec1c087..2b019107b 100644 --- a/Client/src/Components/TabPanels/Account/PasswordPanel.jsx +++ b/Client/src/Components/TabPanels/Account/PasswordPanel.jsx @@ -79,9 +79,7 @@ const PasswordPanel = () => { const action = await dispatch(update({ authToken, localData })); if (action.payload.success) { createToast({ - variant: "info", body: "Your password was changed successfully.", - hasIcon: false, }); setLocalData({ password: "", @@ -91,9 +89,7 @@ const PasswordPanel = () => { } else { // TODO: Check for other errors? createToast({ - variant: "info", body: "Your password input was incorrect.", - hasIcon: false, }); setErrors({ password: "*" + action.payload.msg + "." }); } diff --git a/Client/src/Components/TabPanels/Account/ProfilePanel.jsx b/Client/src/Components/TabPanels/Account/ProfilePanel.jsx index bc5991d2a..d5a286ec5 100644 --- a/Client/src/Components/TabPanels/Account/ProfilePanel.jsx +++ b/Client/src/Components/TabPanels/Account/ProfilePanel.jsx @@ -162,9 +162,7 @@ const ProfilePanel = () => { localData.file === undefined ) { createToast({ - variant: "info", body: "Unable to update profile — no changes detected.", - hasIcon: false, }); setErrors({ unchanged: "unable to update profile" }); return; @@ -173,15 +171,11 @@ const ProfilePanel = () => { const action = await dispatch(update({ authToken, localData })); if (action.payload.success) { createToast({ - variant: "info", body: "Your profile data was changed successfully.", - hasIcon: false, }); } else { createToast({ - variant: "error", body: "There was an error updating your profile data.", - hasIcon: false, }); } }; @@ -205,16 +199,12 @@ const ProfilePanel = () => { if (action.payload) { // dispatch errors createToast({ - variant: "info", body: action.payload.msg, - hasIcon: false, }); } else { // unknown errors createToast({ - variant: "info", body: "Unknown error.", - hasIcon: false, }); } } diff --git a/Client/src/Pages/Auth/CheckEmail.jsx b/Client/src/Pages/Auth/CheckEmail.jsx index 76b640923..d6ff54fef 100644 --- a/Client/src/Pages/Auth/CheckEmail.jsx +++ b/Client/src/Pages/Auth/CheckEmail.jsx @@ -29,24 +29,16 @@ const CheckEmail = () => { const toastFail = [ { - variant: "info", body: "Email not found.", - hasIcon: false, }, { - variant: "info", body: "Redirecting in 3...", - hasIcon: false, }, { - variant: "info", body: "Redirecting in 2...", - hasIcon: false, }, { - variant: "info", body: "Redirecting in 1...", - hasIcon: false, }, ]; @@ -68,25 +60,19 @@ const CheckEmail = () => { const action = await dispatch(forgotPassword(form)); if (action.payload.success) { createToast({ - variant: "info", body: `Instructions sent to ${form.email}.`, - hasIcon: false, }); setDisabled(false); } else { if (action.payload) { // dispatch errors createToast({ - variant: "info", body: action.payload.msg, - hasIcon: false, }); } else { // unknown errors createToast({ - variant: "info", body: "Unknown error.", - hasIcon: false, }); } } diff --git a/Client/src/Pages/Auth/ForgotPassword.jsx b/Client/src/Pages/Auth/ForgotPassword.jsx index 34e99d400..86db72076 100644 --- a/Client/src/Pages/Auth/ForgotPassword.jsx +++ b/Client/src/Pages/Auth/ForgotPassword.jsx @@ -52,24 +52,18 @@ const ForgotPassword = () => { sessionStorage.setItem("email", form.email); navigate("/check-email"); createToast({ - variant: "info", body: `Instructions sent to ${form.email}.`, - hasIcon: false, }); } else { if (action.payload) { // dispatch errors createToast({ - variant: "info", body: action.payload.msg, - hasIcon: false, }); } else { // unknown errors createToast({ - variant: "info", body: "Unknown error.", - hasIcon: false, }); } } diff --git a/Client/src/Pages/Auth/Login.jsx b/Client/src/Pages/Auth/Login.jsx index 464a74394..4d0ae8336 100644 --- a/Client/src/Pages/Auth/Login.jsx +++ b/Client/src/Pages/Auth/Login.jsx @@ -63,36 +63,28 @@ const Login = () => { }); setErrors(newErrors); createToast({ - variant: "info", body: error.details && error.details.length > 0 ? error.details[0].message : "Error validating data.", - hasIcon: false, }); } else { const action = await dispatch(login(form)); if (action.payload.success) { navigate("/monitors"); createToast({ - variant: "info", body: "Welcome back! You're successfully logged in.", - hasIcon: false, }); } else { if (action.payload) { // dispatch errors createToast({ - variant: "info", body: action.payload.msg, - hasIcon: false, }); } else { // unknown errors createToast({ - variant: "info", body: "Unknown error.", - hasIcon: false, }); } } diff --git a/Client/src/Pages/Auth/Register.jsx b/Client/src/Pages/Auth/Register.jsx index c82e944be..30d5184d0 100644 --- a/Client/src/Pages/Auth/Register.jsx +++ b/Client/src/Pages/Auth/Register.jsx @@ -70,12 +70,10 @@ const Register = () => { }); setErrors(newErrors); createToast({ - variant: "info", body: error.details && error.details.length > 0 ? error.details[0].message : "Error validating data.", - hasIcon: false, }); } else { delete adminForm.confirm; @@ -85,24 +83,18 @@ const Register = () => { localStorage.setItem("token", token); navigate("/"); createToast({ - variant: "info", body: "Welcome! Your account was created successfully.", - hasIcon: false, }); } else { if (action.payload) { // dispatch errors createToast({ - variant: "info", body: action.payload.msg, - hasIcon: false, }); } else { // unknown errors createToast({ - variant: "info", body: "Unknown error.", - hasIcon: false, }); } } diff --git a/Client/src/Pages/Auth/SetNewPassword.jsx b/Client/src/Pages/Auth/SetNewPassword.jsx index 96ee553fd..ce95fb7a5 100644 --- a/Client/src/Pages/Auth/SetNewPassword.jsx +++ b/Client/src/Pages/Auth/SetNewPassword.jsx @@ -52,12 +52,10 @@ const SetNewPassword = () => { }); setErrors(newErrors); createToast({ - variant: "info", body: error.details && error.details.length > 0 ? error.details[0].message : "Error validating data.", - hasIcon: false, }); } else { delete passwordForm.confirm; @@ -67,24 +65,18 @@ const SetNewPassword = () => { if (action.payload.success) { navigate("/new-password-confirmed"); createToast({ - variant: "info", body: "Your password was reset successfully.", - hasIcon: false, }); } else { if (action.payload) { // dispatch errors createToast({ - variant: "info", body: action.payload.msg, - hasIcon: false, }); } else { // unknown errors createToast({ - variant: "info", body: "Unknown error.", - hasIcon: false, }); } } diff --git a/Client/src/Utils/toastUtils.jsx b/Client/src/Utils/toastUtils.jsx index 73c21c260..0f272a74e 100644 --- a/Client/src/Utils/toastUtils.jsx +++ b/Client/src/Utils/toastUtils.jsx @@ -11,7 +11,13 @@ import Alert from "../Components/Alert"; * @param {object} [props.config] - Additional configuration props for the toast. */ -export const createToast = ({ variant, title, body, hasIcon, config = {} }) => { +export const createToast = ({ + variant = "info", + title, + body, + hasIcon = false, + config = {}, +}) => { const toastConfig = { position: "bottom-right", autoClose: 3000, @@ -36,9 +42,9 @@ export const createToast = ({ variant, title, body, hasIcon, config = {} }) => { }; createToast.propTypes = { - variant: PropTypes.oneOf(["info", "error", "warning"]).isRequired, + variant: PropTypes.oneOf(["info", "error", "warning"]), title: PropTypes.string, - body: PropTypes.string, + body: PropTypes.string.isRequired, hasIcon: PropTypes.bool, config: PropTypes.object, };