74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
|
|
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: '⚠️' });
|
|||
|
|
}
|
|||
|
|
|