2023-01-09 13:57:12 +00:00
|
|
|
import classNames from "classnames";
|
|
|
|
import toast from "react-hot-toast";
|
|
|
|
|
2023-04-12 15:26:31 +00:00
|
|
|
import { Check, Info } from "../icon";
|
2023-01-09 13:57:12 +00:00
|
|
|
|
|
|
|
type IToast = {
|
|
|
|
message: string;
|
|
|
|
toastVisible: boolean;
|
2023-07-25 22:27:14 +00:00
|
|
|
toastId: string;
|
|
|
|
onClose: (toastId: string) => void;
|
2023-01-09 13:57:12 +00:00
|
|
|
};
|
|
|
|
|
2023-07-25 22:27:14 +00:00
|
|
|
export const SuccessToast = ({ message, toastVisible, onClose, toastId }: IToast) => (
|
|
|
|
<button
|
2023-01-09 13:57:12 +00:00
|
|
|
className={classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"data-testid-toast-success bg-brand-default text-inverted mb-2 flex h-auto items-center space-x-2 rounded-md p-3 text-sm font-semibold shadow-md rtl:space-x-reverse md:max-w-sm",
|
2023-06-28 16:21:53 +00:00
|
|
|
toastVisible && "animate-fade-in-up cursor-pointer"
|
|
|
|
)}
|
2023-07-25 22:27:14 +00:00
|
|
|
onClick={() => onClose(toastId)}>
|
2023-01-17 21:15:04 +00:00
|
|
|
<span>
|
2023-04-12 15:26:31 +00:00
|
|
|
<Check className="h-4 w-4" />
|
2023-01-17 21:15:04 +00:00
|
|
|
</span>
|
2023-04-05 15:42:22 +00:00
|
|
|
<p data-testid="toast-success">{message}</p>
|
2023-07-25 22:27:14 +00:00
|
|
|
</button>
|
2023-01-09 13:57:12 +00:00
|
|
|
);
|
|
|
|
|
2023-07-25 22:27:14 +00:00
|
|
|
export const ErrorToast = ({ message, toastVisible, onClose, toastId }: IToast) => (
|
|
|
|
<button
|
2023-01-09 13:57:12 +00:00
|
|
|
className={classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"animate-fade-in-up bg-error text-error mb-2 flex h-auto items-center space-x-2 rounded-md p-3 text-sm font-semibold shadow-md rtl:space-x-reverse md:max-w-sm",
|
2023-06-28 16:21:53 +00:00
|
|
|
toastVisible && "animate-fade-in-up cursor-pointer"
|
|
|
|
)}
|
2023-07-25 22:27:14 +00:00
|
|
|
onClick={() => onClose(toastId)}>
|
2023-01-17 21:15:04 +00:00
|
|
|
<span>
|
2023-04-12 15:26:31 +00:00
|
|
|
<Info className="h-4 w-4" />
|
2023-01-17 21:15:04 +00:00
|
|
|
</span>
|
2023-04-05 15:42:22 +00:00
|
|
|
<p data-testid="toast-error">{message}</p>
|
2023-07-25 22:27:14 +00:00
|
|
|
</button>
|
2023-01-09 13:57:12 +00:00
|
|
|
);
|
|
|
|
|
2023-07-25 22:27:14 +00:00
|
|
|
export const WarningToast = ({ message, toastVisible, onClose, toastId }: IToast) => (
|
|
|
|
<button
|
2023-01-09 13:57:12 +00:00
|
|
|
className={classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"animate-fade-in-up bg-brand-default text-brand mb-2 flex h-auto items-center space-x-2 rounded-md p-3 text-sm font-semibold shadow-md rtl:space-x-reverse md:max-w-sm",
|
2023-06-28 16:21:53 +00:00
|
|
|
toastVisible && "animate-fade-in-up cursor-pointer"
|
|
|
|
)}
|
2023-07-25 22:27:14 +00:00
|
|
|
onClick={() => onClose(toastId)}>
|
2023-01-17 21:15:04 +00:00
|
|
|
<span>
|
2023-04-12 15:26:31 +00:00
|
|
|
<Info className="h-4 w-4" />
|
2023-01-17 21:15:04 +00:00
|
|
|
</span>
|
2023-04-05 15:42:22 +00:00
|
|
|
<p data-testid="toast-warning">{message}</p>
|
2023-07-25 22:27:14 +00:00
|
|
|
</button>
|
2023-01-09 13:57:12 +00:00
|
|
|
);
|
|
|
|
|
2023-07-25 22:27:14 +00:00
|
|
|
export const DefaultToast = ({ message, toastVisible, onClose, toastId }: IToast) => (
|
|
|
|
<button
|
2023-01-09 13:57:12 +00:00
|
|
|
className={classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"animate-fade-in-up bg-brand-default text-inverted mb-2 flex h-auto items-center space-x-2 rounded-md p-3 text-sm font-semibold shadow-md rtl:space-x-reverse md:max-w-sm",
|
2023-06-28 16:21:53 +00:00
|
|
|
toastVisible && "animate-fade-in-up cursor-pointer"
|
|
|
|
)}
|
2023-07-25 22:27:14 +00:00
|
|
|
onClick={() => onClose(toastId)}>
|
2023-01-17 21:15:04 +00:00
|
|
|
<span>
|
2023-04-12 15:26:31 +00:00
|
|
|
<Check className="h-4 w-4" />
|
2023-01-17 21:15:04 +00:00
|
|
|
</span>
|
2023-01-09 13:57:12 +00:00
|
|
|
<p>{message}</p>
|
2023-07-25 22:27:14 +00:00
|
|
|
</button>
|
2023-01-09 13:57:12 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const TOAST_VISIBLE_DURATION = 6000;
|
|
|
|
|
|
|
|
export function showToast(
|
|
|
|
message: string,
|
|
|
|
variant: "success" | "warning" | "error",
|
|
|
|
duration = TOAST_VISIBLE_DURATION
|
|
|
|
) {
|
2023-07-25 22:27:14 +00:00
|
|
|
const onClose = (toastId: string) => {
|
|
|
|
toast.remove(toastId);
|
2023-06-28 16:21:53 +00:00
|
|
|
};
|
2023-01-09 13:57:12 +00:00
|
|
|
switch (variant) {
|
|
|
|
case "success":
|
2023-06-28 16:21:53 +00:00
|
|
|
return toast.custom(
|
2023-07-25 22:27:14 +00:00
|
|
|
(t) => <SuccessToast message={message} toastVisible={t.visible} onClose={onClose} toastId={t.id} />,
|
2023-06-28 16:21:53 +00:00
|
|
|
{ duration }
|
|
|
|
);
|
2023-01-09 13:57:12 +00:00
|
|
|
case "error":
|
2023-06-28 16:21:53 +00:00
|
|
|
return toast.custom(
|
2023-07-25 22:27:14 +00:00
|
|
|
(t) => <ErrorToast message={message} toastVisible={t.visible} onClose={onClose} toastId={t.id} />,
|
2023-06-28 16:21:53 +00:00
|
|
|
{ duration }
|
|
|
|
);
|
2023-01-09 13:57:12 +00:00
|
|
|
case "warning":
|
2023-06-28 16:21:53 +00:00
|
|
|
return toast.custom(
|
2023-07-25 22:27:14 +00:00
|
|
|
(t) => <WarningToast message={message} toastVisible={t.visible} onClose={onClose} toastId={t.id} />,
|
2023-06-28 16:21:53 +00:00
|
|
|
{ duration }
|
|
|
|
);
|
2023-01-09 13:57:12 +00:00
|
|
|
default:
|
2023-06-28 16:21:53 +00:00
|
|
|
return toast.custom(
|
2023-07-25 22:27:14 +00:00
|
|
|
(t) => <DefaultToast message={message} toastVisible={t.visible} onClose={onClose} toastId={t.id} />,
|
2023-06-28 16:21:53 +00:00
|
|
|
{ duration }
|
|
|
|
);
|
2023-01-09 13:57:12 +00:00
|
|
|
}
|
|
|
|
}
|