From e2cf91c91c834feea049bf00a189e39382944ea7 Mon Sep 17 00:00:00 2001 From: freds-dev Date: Wed, 18 Dec 2024 11:18:37 +0100 Subject: [PATCH] feat(device): modify temporary expiration date handling in device creation --- app/components/device/new/general-info.tsx | 49 +++++++++++-------- .../device/new/new-device-stepper.tsx | 2 +- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/app/components/device/new/general-info.tsx b/app/components/device/new/general-info.tsx index deaddba9..ce4e2247 100644 --- a/app/components/device/new/general-info.tsx +++ b/app/components/device/new/general-info.tsx @@ -1,3 +1,4 @@ +import React, { useState } from "react"; import { useFormContext, useFieldArray } from "react-hook-form"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; @@ -22,7 +23,30 @@ export function GeneralInfoStep() { }); const currentExposure = watch("exposure"); // Watch exposure value - const temporaryExpirationDate = watch("temporaryExpirationDate"); // Watch expiration date + + // State for temporary expiration date + const [temporaryExpirationDate, setTemporaryExpirationDate] = useState< + string | null + >(watch("temporaryExpirationDate") || null); + + const maxExpirationDate = new Date(); + maxExpirationDate.setMonth(maxExpirationDate.getMonth() + 1); + + const handleTemporaryChange = (checked: boolean) => { + if (checked) { + const newDate = maxExpirationDate.toISOString().split("T")[0]; + setTemporaryExpirationDate(newDate); // Update local state + setValue("temporaryExpirationDate", newDate); // Update form value + } else { + setTemporaryExpirationDate(null); // Clear local state + setValue("temporaryExpirationDate", ""); // Clear form value + } + }; + + const handleExpirationDateChange = (date: string) => { + setTemporaryExpirationDate(date); // Update local state + setValue("temporaryExpirationDate", date); // Update form value + }; const addTag = (event: React.FormEvent) => { event.preventDefault(); @@ -52,17 +76,6 @@ export function GeneralInfoStep() { }, ]; - const maxExpirationDate = new Date(); - maxExpirationDate.setMonth(maxExpirationDate.getMonth() + 1); - - const handleTemporaryChange = (checked: boolean) => { - if (checked) { - setValue("temporaryExpirationDate", maxExpirationDate); - } else { - setValue("temporaryExpirationDate", ""); - } - }; - return (
@@ -100,7 +113,7 @@ export function GeneralInfoStep() {