M: Improve notification delays
Some checks are pending
Frontend CI / build (22.x) (push) Waiting to run

This commit is contained in:
Ivan 2025-02-26 16:46:34 +03:00
parent e211898cc0
commit 02fffc67e5
2 changed files with 34 additions and 41 deletions

View File

@ -6,6 +6,7 @@ import axios, { type AxiosError, type AxiosRequestConfig } from 'axios';
import { type z, ZodError } from 'zod';
import { buildConstants } from '@/utils/buildConstants';
import { PARAMETER } from '@/utils/constants';
import { errorMsg } from '@/utils/labels';
import { extractErrorMessage } from '@/utils/utils';
@ -62,11 +63,7 @@ export function axiosGet<ResponseData>({ endpoint, options, schema }: IAxiosGetR
.catch((error: Error | AxiosError) => {
// Note: Ignore cancellation errors
if (error.name !== 'CanceledError') {
if (error instanceof ZodError) {
toast.error(errorMsg.invalidResponse);
} else {
toast.error(extractErrorMessage(error));
}
notifyError(error);
console.error(error);
}
throw error;
@ -83,21 +80,11 @@ export function axiosPost<RequestData, ResponseData = void>({
.post<ResponseData>(endpoint, request?.data, options)
.then(response => {
schema?.parse(response.data);
if (request?.successMessage) {
if (typeof request.successMessage === 'string') {
toast.success(request.successMessage);
} else {
toast.success(request.successMessage(response.data));
}
}
notifySuccess(response.data, request?.successMessage);
return response.data;
})
.catch((error: Error | AxiosError | ZodError) => {
if (error instanceof ZodError) {
toast.error(errorMsg.invalidResponse);
} else {
toast.error(extractErrorMessage(error));
}
notifyError(error);
throw error;
});
}
@ -112,21 +99,11 @@ export function axiosDelete<RequestData, ResponseData = void>({
.delete<ResponseData>(endpoint, options)
.then(response => {
schema?.parse(response.data);
if (request?.successMessage) {
if (typeof request.successMessage === 'string') {
toast.success(request.successMessage);
} else {
toast.success(request.successMessage(response.data));
}
}
notifySuccess(response.data, request?.successMessage);
return response.data;
})
.catch((error: Error | AxiosError | ZodError) => {
if (error instanceof ZodError) {
toast.error(errorMsg.invalidResponse);
} else {
toast.error(extractErrorMessage(error));
}
notifyError(error);
throw error;
});
}
@ -141,21 +118,36 @@ export function axiosPatch<RequestData, ResponseData = void>({
.patch<ResponseData>(endpoint, request?.data, options)
.then(response => {
schema?.parse(response.data);
if (request?.successMessage) {
if (typeof request.successMessage === 'string') {
toast.success(request.successMessage);
} else {
toast.success(request.successMessage(response.data));
}
}
notifySuccess(response.data, request?.successMessage);
return response.data;
})
.catch((error: Error | AxiosError | ZodError) => {
notifyError(error);
throw error;
});
}
// ====== Internals =========
function notifySuccess<ResponseData>(
data: ResponseData,
message: string | ((data: ResponseData) => string) | undefined
) {
if (!message) {
return;
}
setTimeout(() => {
if (typeof message === 'string') {
toast.success(message);
} else {
toast.success(message(data));
}
}, PARAMETER.notificationDelay);
}
function notifyError(error: Error | AxiosError | ZodError) {
if (error instanceof ZodError) {
toast.error(errorMsg.invalidResponse);
} else {
toast.error(extractErrorMessage(error));
}
throw error;
});
}

View File

@ -9,6 +9,7 @@ export const PARAMETER = {
smallScreen: 640, // == tailwind:sm
smallTreeNodes: 50, // amount of nodes threshold for size increase for large graphs
refreshTimeout: 100, // milliseconds delay for post-refresh actions
notificationDelay: 300, // milliseconds delay for notifications
minimalTimeout: 10, // milliseconds delay for fast updates
zoomDuration: 500, // milliseconds animation duration
moveDuration: 500, // milliseconds - duration of move animation