import get from "lodash.get"; import { AlertTriangle, Check, Loader2 } from "lucide-react"; import { toast } from "sonner"; export const actionToast = async (data: { task: () => Promise; before?: () => any; success?: () => any; after?: () => any; failed?: () => any; msg_succes?: string; msg_error?: string; msg_load?: string; hidden_icon?: boolean; }) => { const { task, before, after, success, msg_succes, msg_error, msg_load, failed, hidden_icon, } = data; try { if (typeof before === "function") before(); toast.info( <> {msg_load ? msg_load : " Load..."} ); if (typeof task === "function") await task(); setTimeout(() => { toast.dismiss(); toast.success(
{ toast.dismiss(); }} >
{msg_succes ? msg_succes : " Success"}
); if (typeof after === "function") after(); if (typeof success === "function") success(); }, 100); } catch (ex: any) { setTimeout(() => { if (typeof failed === "function") failed(); toast.dismiss(); toast.error(
{hidden_icon !== true ? ( ) : ( <> )} {msg_error ? msg_error : " Failed"}{" "} {get(ex, "response.data.meta.message") || ex.message}.
, { dismissible: true, className: css` background: #ffecec; border: 2px solid red; `, } ); }, 100); } };