import React, { Fragment } from "react"; // next import { useRouter } from "next/router"; // react hook form import { useForm, Controller } from "react-hook-form"; import { Transition, Dialog, Listbox } from "@headlessui/react"; // date helper import { getDatesAfterCurrentDate, getTimestampAfterCurrentTime } from "helpers/date-time.helper"; // hooks import useToast from "hooks/use-toast"; // components import { PrimaryButton, SecondaryButton, Icon } from "components/ui"; // types import type { IUserNotification } from "types"; type SnoozeModalProps = { isOpen: boolean; onClose: () => void; onSuccess: () => void; notification: IUserNotification | null; onSubmit: (notificationId: string, dateTime?: Date | undefined) => Promise; }; const dates = getDatesAfterCurrentDate(); const timeStamps = getTimestampAfterCurrentTime(); const defaultValues = { time: null, date: null, }; export const SnoozeNotificationModal: React.FC = (props) => { const { isOpen, onClose, notification, onSuccess, onSubmit: handleSubmitSnooze } = props; const router = useRouter(); const { workspaceSlug } = router.query; const { setToastAlert } = useToast(); const { formState: { isSubmitting }, reset, handleSubmit, control, } = useForm({ defaultValues, }); const onSubmit = async (formData: any) => { if (!workspaceSlug || !notification) return; const dateTime = new Date( `${formData.date.toLocaleDateString()} ${formData.time.toLocaleTimeString()}` ); await handleSubmitSnooze(notification.id, dateTime).then(() => { onClose(); onSuccess(); setToastAlert({ title: "Notification snoozed", message: "Notification snoozed successfully", type: "success", }); }); }; const handleClose = () => { onClose(); const timeout = setTimeout(() => { reset(); clearTimeout(timeout); }, 500); }; return (
Customize Snooze Time
( {({ open }) => ( <>
{value ? new Date(value)?.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", }) : "Select Time"} {timeStamps.map((time, index) => ( `relative cursor-default select-none py-2 pl-3 pr-9 ${ active ? "bg-custom-primary-100/80 text-custom-text-100" : "text-custom-text-700" }` } value={time.value} > {({ selected, active }) => ( <>
{time.label}
{selected ? ( ) : null} )}
))}
)}
)} />
( {({ open }) => ( <>
{value ? new Date(value)?.toLocaleDateString([], { day: "numeric", month: "long", year: "numeric", }) : "Select Date"} {dates.map((date, index) => ( `relative cursor-default select-none py-2 pl-3 pr-9 ${ active ? "bg-custom-primary-100/80 text-custom-text-100" : "text-custom-text-700" }` } value={date.value} > {({ selected, active }) => ( <>
{date.label}
{selected ? ( ) : null} )}
))}
)}
)} />
Cancel {isSubmitting ? "Submitting..." : "Submit"}
); };