veza/apps/web/src/utils/apiToastHelper.ts

73 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { InternalAxiosRequestConfig } from 'axios';
import toast from 'react-hot-toast';
/**
* FE-COMP-005: Helper utilities for API toast notifications
*/
/**
* Enable success toast for an API request
* @param config Axios request config
* @param message Optional custom success message
* @returns Modified config with toast enabled
*/
export function withSuccessToast(
config: InternalAxiosRequestConfig,
message?: string,
): InternalAxiosRequestConfig {
(config as any)._showSuccessToast = true;
if (message) {
(config as any)._successMessage = message;
}
return config;
}
/**
* Disable automatic error toast for an API request
* Useful when you want to handle errors manually
* @param config Axios request config
* @returns Modified config with toast disabled
*/
export function withoutErrorToast(
config: InternalAxiosRequestConfig,
): InternalAxiosRequestConfig {
(config as any)._disableToast = true;
return config;
}
/**
* Show a success toast manually
* @param message Success message
* @param duration Toast duration in milliseconds
*/
export function showSuccessToast(message: string, duration?: number): void {
toast.success(message, { duration });
}
/**
* Show an error toast manually
* @param message Error message
* @param duration Toast duration in milliseconds
*/
export function showErrorToast(message: string, duration?: number): void {
toast.error(message, { duration });
}
/**
* Show an info toast manually
* @param message Info message
* @param duration Toast duration in milliseconds
*/
export function showInfoToast(message: string, duration?: number): void {
toast(message, { duration, icon: '' });
}
/**
* Show a warning toast manually
* @param message Warning message
* @param duration Toast duration in milliseconds
*/
export function showWarningToast(message: string, duration?: number): void {
toast(message, { duration, icon: '⚠️' });
}