137 lines
5.7 KiB
TypeScript
137 lines
5.7 KiB
TypeScript
import { Fragment, useEffect, useState } from "react";
|
|
import { Dialog, DialogBackdrop, DialogPanel, DialogTitle, Transition, TransitionChild } from "@headlessui/react";
|
|
import clsx from "clsx";
|
|
import { useTranslation } from "react-i18next";
|
|
import { CheckCircleIcon } from "~/components/ui/icons/vendor";
|
|
|
|
export type ActionResultDto = { success?: ResultMessageDto; error?: ResultMessageDto };
|
|
type ResultMessageDto = { title?: string; description: string; closeText?: string };
|
|
interface Props {
|
|
actionResult?: ActionResultDto;
|
|
className?: string;
|
|
actionData?: { error?: string; success?: string } | null;
|
|
showSuccess?: boolean;
|
|
showError?: boolean;
|
|
onClosed?: () => void;
|
|
}
|
|
|
|
export default function ActionResultModal({ actionResult, className, actionData, showSuccess = true, showError = true, onClosed }: Props) {
|
|
const { t } = useTranslation();
|
|
const [data, setData] = useState<ResultMessageDto>();
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const [status, setStatus] = useState(actionResult);
|
|
|
|
useEffect(() => {
|
|
setStatus(actionResult);
|
|
}, [actionResult]);
|
|
|
|
useEffect(() => {
|
|
if (actionData?.error && showError) {
|
|
setStatus({ error: { description: actionData.error } });
|
|
} else if (actionData?.success && showSuccess) {
|
|
setStatus({ success: { description: actionData.success } });
|
|
}
|
|
}, [actionData, showError, showSuccess]);
|
|
|
|
useEffect(() => {
|
|
if (status?.success) {
|
|
setData({
|
|
title: status.success.title ?? t("shared.success"),
|
|
description: status.success.description,
|
|
closeText: status.success.closeText ?? t("shared.close"),
|
|
});
|
|
setOpen(true);
|
|
} else if (status?.error) {
|
|
setData({
|
|
title: status.error.title ?? t("shared.error"),
|
|
description: status.error.description,
|
|
closeText: status.error.closeText ?? t("shared.close"),
|
|
});
|
|
setOpen(true);
|
|
}
|
|
}, [status, t]);
|
|
|
|
return (
|
|
<Transition show={open} as={Fragment}>
|
|
<Dialog
|
|
as="div"
|
|
className={clsx(className, "fixed inset-0 z-50 overflow-y-auto transition duration-300 ease-out data-[closed]:opacity-0")}
|
|
transition
|
|
onClose={() => setOpen(false)}
|
|
>
|
|
<div className="flex min-h-screen items-end justify-center px-4 pt-4 pb-20 text-center sm:block sm:p-0">
|
|
<TransitionChild
|
|
as={Fragment}
|
|
enter="ease-out duration-300"
|
|
enterFrom="opacity-0"
|
|
enterTo="opacity-100"
|
|
leave="ease-in duration-200"
|
|
leaveFrom="opacity-100"
|
|
leaveTo="opacity-0"
|
|
>
|
|
<DialogBackdrop className="fixed inset-0 bg-gray-500/75 transition-opacity" />
|
|
</TransitionChild>
|
|
|
|
{/* This element is to trick the browser into centering the modal contents. */}
|
|
<span className="hidden sm:inline-block sm:h-screen sm:align-middle" aria-hidden="true">
|
|
​
|
|
</span>
|
|
<div className="fixed inset-0 flex w-screen items-center justify-center p-4">
|
|
{/* Subframe ResultDialog layout: flex-col items-center gap-6, rounded-lg, px-6 py-8, icon badge h-14 w-14 rounded-round, content gap-2, full-width button. */}
|
|
<DialogPanel className="bg-surface-neutral-modal flex w-full max-w-full transform flex-col items-center gap-6 overflow-hidden rounded-lg px-6 py-8 text-left align-bottom shadow-xl transition-all sm:my-8 sm:w-96 sm:align-middle">
|
|
{status?.success && (
|
|
<div className="flex h-14 w-14 flex-none items-center justify-center rounded-round bg-green-100">
|
|
<CheckCircleIcon className="h-6 w-6 text-green-600" />
|
|
</div>
|
|
)}
|
|
{status?.error && (
|
|
<div className="flex h-14 w-14 flex-none items-center justify-center rounded-round bg-red-100">
|
|
<svg
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
className="h-6 w-6 text-red-600"
|
|
fill="none"
|
|
viewBox="0 0 24 24"
|
|
stroke="currentColor"
|
|
strokeWidth="2"
|
|
>
|
|
<path
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
|
|
/>
|
|
</svg>
|
|
</div>
|
|
)}
|
|
<div className="flex w-full flex-col items-center gap-2 text-center">
|
|
<DialogTitle as="h3" className="text-text-neutral-primary text-lg leading-6 font-medium">
|
|
{t(data?.title ?? "")}
|
|
</DialogTitle>
|
|
<p className="text-text-neutral-tertiary text-sm">{t(data?.description ?? "")}</p>
|
|
</div>
|
|
<div className="w-full">
|
|
<button
|
|
type="button"
|
|
className={clsx(
|
|
"inline-flex w-full justify-center rounded-md border border-transparent px-4 py-2 text-base font-medium text-white shadow-2xs focus:ring-2 focus:ring-offset-2 focus:outline-hidden sm:text-sm",
|
|
status?.success && "bg-teal-600 hover:bg-teal-700 focus:ring-teal-500",
|
|
status?.error && "bg-gray-600 hover:bg-gray-700 focus:ring-gray-500"
|
|
)}
|
|
onClick={() => {
|
|
setOpen(false);
|
|
if (onClosed) {
|
|
onClosed();
|
|
}
|
|
}}
|
|
>
|
|
{data?.closeText ?? t("shared.close")}
|
|
</button>
|
|
</div>
|
|
</DialogPanel>
|
|
</div>
|
|
</div>
|
|
</Dialog>
|
|
</Transition>
|
|
);
|
|
}
|