Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion hat/assets/js/apps/Iaso/libs/apiHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type SnackMutationDict<Data, Error, Variables, Context> = {
>
| undefined;
ignoreErrorCodes?: number[];
useApiErrorMessage?: boolean;
showSuccessSnackBar?: boolean;
successSnackBar?: (
msg: IntlMessage,
Expand All @@ -84,6 +85,37 @@ type SnackMutationDict<Data, Error, Variables, Context> = {
};
};

const getErrorFromDetails = (details: any) => {
if (typeof details === 'string') {
return details;
}
if (Array.isArray(details)) {
return details.join('\n');
}
};

const getApiErrorMessage = (error: any) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a DjangoError type no ? @quang-le

let errorMsg = '';

if (typeof error === 'string') {
return error;
}

if (!error.details) {
return error.message;
}

const keys = Object.keys(error.details);
if (keys.length === 1) {
return getErrorFromDetails(error.details[keys[0]]);
} else {
errorMsg = Object.keys(error.details)
.map(key => `${key}: ${getErrorFromDetails(error.details[key])}`)
.join('\n');
}
return errorMsg;
};

const useBaseSnackMutation = <
Data = unknown,
Error = unknown,
Expand All @@ -92,7 +124,7 @@ const useBaseSnackMutation = <
>(
mutationFn: MutationFunction<Data, any>,
snackSuccessMessage: IntlMessage = MESSAGES.defaultMutationApiSuccess,
snackErrorMsg: IntlMessage = MESSAGES.defaultMutationApiError,
snackErrorMsg: IntlMessage | string = MESSAGES.defaultMutationApiError,
invalidateQueryKey: QueryKey | undefined = undefined,
options:
| Omit<
Expand All @@ -113,6 +145,7 @@ const useBaseSnackMutation = <
persist: boolean;
};
} = msg => succesfullSnackBar(undefined, msg),
useApiErrorMessage = false,
): UseMutationResult<Data, Error, Variables, Context> => {
const queryClient = useQueryClient();
const { formatMessage } = useSafeIntl();
Expand All @@ -124,13 +157,17 @@ const useBaseSnackMutation = <
onError: (error, variables, context) => {
if (!ignoreErrorCodes.includes(error.status)) {
let errorMsg = snackErrorMsg;

if (error.status === 403) {
if (error.details.detail) {
errorMsg = error.details.detail;
} else if (!errorMsg) {
errorMsg = formatMessage(MESSAGES.permissionError);
}
} else if (useApiErrorMessage && error.details) {
errorMsg = getApiErrorMessage(error) as any;
}

openSnackBar(errorSnackBar(undefined, errorMsg, error));
}
if (options.onError) {
Expand Down Expand Up @@ -192,6 +229,7 @@ export const useSnackMutation = <
persist: boolean;
};
} = msg => succesfullSnackBar(undefined, msg),
useApiErrorMessage = false,
): UseMutationResult<Data, Error, Variables, Context> => {
let arg1;
let arg2;
Expand All @@ -201,6 +239,7 @@ export const useSnackMutation = <
let arg6;
let arg7;
let arg8;
let arg9;
// Checking if the first argument passed is a dictionary
const keys = Object.keys(mutationArg) ?? [];
if (keys.length > 0 && keys.includes('mutationFn')) {
Expand Down Expand Up @@ -228,6 +267,9 @@ export const useSnackMutation = <
arg8 = (
mutationArg as SnackMutationDict<Data, Error, Variables, Context>
).successSnackBar;
arg9 = (
mutationArg as SnackMutationDict<Data, Error, Variables, Context>
).useApiErrorMessage;
} else {
arg1 = mutationArg;
arg2 = snackSuccessMessage;
Expand All @@ -237,6 +279,7 @@ export const useSnackMutation = <
arg6 = showSuccessSnackBar;
arg7 = ignoreErrorCodes;
arg8 = successSnackBar;
arg9 = useApiErrorMessage;
}
return useBaseSnackMutation<Data, Error, Variables, Context>(
arg1,
Expand All @@ -247,6 +290,7 @@ export const useSnackMutation = <
arg6,
arg7,
arg8,
arg9,
);
};

Expand Down
Loading