fix issue #45: enhance error messages for user feedback

updated error toast notifications to display specific user feedback based on backend response
This commit is contained in:
Pranav Bhat 2024-09-30 22:30:42 +05:30
parent d47deeb899
commit 2e3cc0b8e6
10 changed files with 55 additions and 45 deletions

View File

@ -35,8 +35,9 @@ export const CategoryDialog: FC<CategoryDialogProps> = ({
const { t } = useTranslation(); const { t } = useTranslation();
const { toast } = useToast(); const { toast } = useToast();
const { mutateAsync: createCategory } = useCreate(EntityType.CATEGORY, { const { mutateAsync: createCategory } = useCreate(EntityType.CATEGORY, {
onError: () => { onError: (error) => {
toast.error(t("message.internal_server_error")); console.log(error);
toast.error(error);
}, },
onSuccess: () => { onSuccess: () => {
closeDialog(); closeDialog();

View File

@ -50,8 +50,8 @@ export const ContentTypeDialog: FC<ContentTypeDialogProps> = ({
const { mutateAsync: createContentType } = useCreate( const { mutateAsync: createContentType } = useCreate(
EntityType.CONTENT_TYPE, EntityType.CONTENT_TYPE,
{ {
onError: () => { onError: (error) => {
toast.error(t("message.internal_server_error")); toast.error(error);
}, },
onSuccess: () => { onSuccess: () => {
closeDialog(); closeDialog();

View File

@ -178,8 +178,8 @@ export const ContentDialog: FC<ContentDialogProps> = ({
createContent( createContent(
{ ...params, entity: contentType.id }, { ...params, entity: contentType.id },
{ {
onError: () => { onError: (error) => {
toast.error(t("message.internal_server_error")); toast.error(error);
}, },
onSuccess: () => { onSuccess: () => {
closeDialog(); closeDialog();

View File

@ -35,8 +35,8 @@ export const ContextVarDialog: FC<ContextVarDialogProps> = ({
const { t } = useTranslation(); const { t } = useTranslation();
const { toast } = useToast(); const { toast } = useToast();
const { mutateAsync: createContextVar } = useCreate(EntityType.CONTEXT_VAR, { const { mutateAsync: createContextVar } = useCreate(EntityType.CONTEXT_VAR, {
onError: () => { onError: (error) => {
toast.error(t("message.internal_server_error")); toast.error(error);
}, },
onSuccess() { onSuccess() {
closeDialog(); closeDialog();

View File

@ -34,8 +34,8 @@ export const RoleDialog: FC<RoleDialogProps> = ({
const { t } = useTranslation(); const { t } = useTranslation();
const { toast } = useToast(); const { toast } = useToast();
const { mutateAsync: createRole } = useCreate(EntityType.ROLE, { const { mutateAsync: createRole } = useCreate(EntityType.ROLE, {
onError: () => { onError: (error) => {
toast.error(t("message.internal_server_error")); toast.error(error);
}, },
onSuccess() { onSuccess() {
closeDialog(); closeDialog();

View File

@ -57,8 +57,8 @@ export const Roles = () => {
}, },
); );
const { mutateAsync: deleteRole } = useDelete(EntityType.ROLE, { const { mutateAsync: deleteRole } = useDelete(EntityType.ROLE, {
onError: () => { onError: (error) => {
toast.error(t("message.internal_server_error")); toast.error(error);
}, },
onSuccess() { onSuccess() {
deleteDialogCtl.closeDialog(); deleteDialogCtl.closeDialog();

View File

@ -56,8 +56,8 @@ export const Translations = () => {
}, },
); );
const { mutateAsync: deleteTranslation } = useDelete(EntityType.TRANSLATION, { const { mutateAsync: deleteTranslation } = useDelete(EntityType.TRANSLATION, {
onError: () => { onError: (error) => {
toast.error(t("message.internal_server_error")); toast.error(error);
}, },
onSuccess() { onSuccess() {
deleteDialogCtl.closeDialog(); deleteDialogCtl.closeDialog();

View File

@ -1,18 +1,11 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
* 3. SaaS Restriction: This software, or any derivative of it, may not be used to offer a competing product or service (SaaS) without prior written consent from Hexastack. Offering the software as a service or using it in a commercial cloud environment without express permission is strictly prohibited.
*/
import { import {
OptionsObject, OptionsObject,
enqueueSnackbar, enqueueSnackbar,
SnackbarProvider as ToastProvider, SnackbarProvider as ToastProvider,
} from "notistack"; } from "notistack";
import { useTranslation } from "react-i18next";
export { ToastProvider }; export { ToastProvider };
const TOAST_COMMON_STYLE = { const TOAST_COMMON_STYLE = {
@ -39,25 +32,39 @@ const TOAST_WARNING_STYLE = {
backgroundColor: "#fdf6ec", backgroundColor: "#fdf6ec",
}; };
export const useToast = () => ({ export const useToast = () => {
toast: { const { t } = useTranslation();
error: (message: string, options?: OptionsObject<"error">) =>
enqueueSnackbar(message, { const extractErrorMessage = (error: any) => {
variant: "error", if (error?.statusCode == 409) {
...options, return t("message.duplicate_error");
style: { ...TOAST_ERROR_STYLE, ...options?.style }, }
}),
success: (message: string, options?: OptionsObject<"success">) => return error?.message || t("message.internal_server_error");
enqueueSnackbar(message, { };
variant: "success",
...options, return {
style: { ...TOAST_SUCCESS_STYLE, ...options?.style }, toast: {
}), error: (error: any, options?: OptionsObject<"error">) => {
warning: (message: string, options?: OptionsObject<"warning">) => const errorMessage = extractErrorMessage(error);
enqueueSnackbar(message, { enqueueSnackbar(errorMessage, {
variant: "warning", variant: "error",
...options, ...options,
style: { ...TOAST_WARNING_STYLE, ...options?.style }, style: { ...TOAST_ERROR_STYLE, ...options?.style },
}), });
}, },
}); success: (message: string, options?: OptionsObject<"success">) =>
enqueueSnackbar(message, {
variant: "success",
...options,
style: { ...TOAST_SUCCESS_STYLE, ...options?.style },
}),
warning: (message: string, options?: OptionsObject<"warning">) =>
enqueueSnackbar(message, {
variant: "warning",
...options,
style: { ...TOAST_WARNING_STYLE, ...options?.style },
}),
},
};
};

View File

@ -1,5 +1,6 @@
{ {
"message": { "message": {
"duplicate_error": "Duplicate entry. Please choose a unique value.",
"bad_request": "400 BAD REQUEST", "bad_request": "400 BAD REQUEST",
"unable_to_process_request": "Unable to process request", "unable_to_process_request": "Unable to process request",
"not_found": "404 NOT FOUND", "not_found": "404 NOT FOUND",

View File

@ -1,5 +1,6 @@
{ {
"message": { "message": {
"duplicate_error" : "Entrée en double. Veuillez choisir une valeur unique.",
"bad_request": "400 MAUVAISE REQUÊTE", "bad_request": "400 MAUVAISE REQUÊTE",
"unable_to_process_request": "Impossible de traiter la requête", "unable_to_process_request": "Impossible de traiter la requête",
"not_found": "404 RESSOURCE INTROUVABLE", "not_found": "404 RESSOURCE INTROUVABLE",