Simplified toast function call

This commit is contained in:
Daniel Cojocea committed 2024-07-18 10:43:46 -04:00
1 parent 783d745181
commit 5150179193
8 files changed
+9 -61

No files matched your search

@@ -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 + "." });
}
@@ -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,
});
}
}
-14
View File
@@ -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,
});
}
}
-6
View File
@@ -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,
});
}
}
-8
View File
@@ -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,
});
}
}
-8
View File
@@ -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,
});
}
}
-8
View File
@@ -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,
});
}
}
+9 -3
View File
@@ -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,
};