Clear when re-opening form

This commit is contained in:
Daniel Hougaard
2023-11-28 00:48:08 +04:00
parent 5ae3b66e2e
commit 33ea019d70

View File

@@ -9,8 +9,6 @@ import { z } from "zod";
import { Button, FormControl, Input, Modal, ModalContent, TextArea } from "@app/components/v2";
export type TReminderFormSchema = z.infer<typeof ReminderFormSchema>;
const ReminderFormSchema = z.object({
note: z.string().optional(),
days: z
@@ -19,6 +17,7 @@ const ReminderFormSchema = z.object({
.max(365, { message: "Must be less than 365 days" }),
cron: z.string().refine(isValidCron, { message: "Invalid cron expression" })
});
export type TReminderFormSchema = z.infer<typeof ReminderFormSchema>;
interface ReminderFormProps {
isOpen: boolean;
@@ -29,6 +28,7 @@ export const CreateReminderForm = ({ isOpen, onOpenChange }: ReminderFormProps)
const {
register,
watch,
reset,
setValue,
handleSubmit,
formState: { errors, isSubmitting }
@@ -46,8 +46,16 @@ export const CreateReminderForm = ({ isOpen, onOpenChange }: ReminderFormProps)
useEffect(() => {
if (!daysWatch) return;
setValue("cron", `0 0 */${daysWatch} * *`);
setValue("days", parseInt(String(daysWatch), 10));
console.count("daysWatch");
}, [daysWatch]);
useEffect(() => {
if (isOpen) {
reset();
}
}, [isOpen]);
return (
<Modal isOpen={isOpen} onOpenChange={onOpenChange}>
<ModalContent
@@ -71,9 +79,9 @@ export const CreateReminderForm = ({ isOpen, onOpenChange }: ReminderFormProps)
errorText={errors?.days?.message || ""}
>
<Input
onChange={(el) => setValue("days", parseInt(el.target.value, 10))}
type="number"
placeholder="every 5 days"
onChange={(el) => setValue("days", parseInt(el.target.value, 10))}
/>
</FormControl>
{!!daysWatch && cronWatch && isValidCron(cronWatch) && (