import toast, { type Toast } from 'react-hot-toast';
import { FiX } from 'react-icons/fi';
import { FaCheck } from "react-icons/fa";
import { IoAlertCircleOutline } from "react-icons/io5";
// Komponen Toast Card dengan Tailwind untuk pesan sukses dan pesan error
function ToastCard({ t, type, title, description }: {
t: Toast;
type: 'success' | 'error';
title: string;
description?: string;
}) {
const isSuccess = type === 'success';
return (
{isSuccess ? : }
{title}
{description &&
{description}
}
);
}
// pemanggilan function ToastCard dengan params
export const customToast = {
success: (title: string, description?: string) =>
toast.custom((t) => ),
error: (title: string, description?: string) =>
toast.custom((t) => ),
promise: async (
promise: Promise,
messages: {
loading: string;
success: (data: T) => { title: string; description?: string };
error: (err: any) => { title: string; description?: string };
}
): Promise => {
const loadingId = toast.loading(messages.loading);
try {
const data = await promise;
const isHookFailure = typeof data === 'object' && data !== null && 'success' in data && (data as { success?: boolean }).success === false;
if (isHookFailure) {
toast.remove(loadingId);
const result = data as { success: boolean; message?: string };
const { title, description } = messages.error(result);
customToast.error(title, description ?? result.message);
throw new Error(result.message ?? 'Operasi gagal.');
}
toast.remove(loadingId);
const { title, description } = messages.success(data);
customToast.success(title, description);
return data;
} catch (err) {
toast.remove(loadingId);
const { title, description } = messages.error(err);
customToast.error(title, description ?? (err as Error)?.message);
throw err;
}
},
};