From a769feaed24d844e92d4ac08995e881152818f6f Mon Sep 17 00:00:00 2001 From: Peter Thiessen Date: Mon, 21 Oct 2024 17:59:33 -0400 Subject: [PATCH] Updates closingDate to use null as closed or date as open --- .../[id]/settings/manage/close/SetClosingDate.tsx | 15 +++++++++++++-- .../(form administration)/form-builder/actions.ts | 2 +- lib/templates.ts | 12 +++--------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/app/(gcforms)/[locale]/(form administration)/form-builder/[id]/settings/manage/close/SetClosingDate.tsx b/app/(gcforms)/[locale]/(form administration)/form-builder/[id]/settings/manage/close/SetClosingDate.tsx index 99da9446d2..2e8483df2e 100644 --- a/app/(gcforms)/[locale]/(form administration)/form-builder/[id]/settings/manage/close/SetClosingDate.tsx +++ b/app/(gcforms)/[locale]/(form administration)/form-builder/[id]/settings/manage/close/SetClosingDate.tsx @@ -46,7 +46,17 @@ export const SetClosingDate = ({ return true; }, [closedMessage]); - const [status, setStatus] = useState(closingDate ? "closed" : "open"); + // TODO: not working, needs more work + const isPastDate = useCallback( + (closingDate: string | null | undefined) => { + const date1 = new Date(closingDate || 0).getTime(); + const date2 = Date.now(); + return date1 < date2; + }, + [closingDate] + ); + + const [status, setStatus] = useState(isPastDate(closingDate) ? "closed" : "open"); const [showDateTimeDialog, setShowDateTimeDialog] = useState(false); const handleToggle = (value: boolean) => { @@ -81,7 +91,7 @@ export const SetClosingDate = ({ ); const saveFormStatus = useCallback(async () => { - let closeDate = "open"; // this wil reset the closing date to null; + let closeDate = null; if (status === "closed") { const now = new Date(); // Set date to now to close the form right away @@ -99,6 +109,7 @@ export const SetClosingDate = ({ return; } + // TODO: not working, needs more work // update the local template store setClosingDate(status !== "open" ? closeDate : null); diff --git a/app/(gcforms)/[locale]/(form administration)/form-builder/actions.ts b/app/(gcforms)/[locale]/(form administration)/form-builder/actions.ts index 756579971c..31d6909606 100644 --- a/app/(gcforms)/[locale]/(form administration)/form-builder/actions.ts +++ b/app/(gcforms)/[locale]/(form administration)/form-builder/actions.ts @@ -252,7 +252,7 @@ export const closeForm = async ({ closedDetails, }: { id: string; - closingDate: string; + closingDate: string | null; closedDetails?: ClosedDetails; }): Promise<{ formID: string; diff --git a/lib/templates.ts b/lib/templates.ts index 0515aa6aab..96841a54cf 100644 --- a/lib/templates.ts +++ b/lib/templates.ts @@ -1251,15 +1251,9 @@ export const onlyIncludePublicProperties = (template: FormRecord): PublicFormRec export const updateClosedData = async ( ability: UserAbility, formID: string, - closingDate: string, + closingDate: string | null, details?: ClosedDetails ) => { - let d = null; - - if (closingDate !== "open") { - d = closingDate; - } - let detailsData: ClosedDetails | null = null; // Add the closed details if they exist @@ -1276,7 +1270,7 @@ export const updateClosedData = async ( id: formID, }, data: { - closingDate: d, + closingDate, closedDetails: detailsData !== null ? (detailsData as Prisma.JsonObject) : Prisma.JsonNull, }, @@ -1297,7 +1291,7 @@ export const updateClosedData = async ( ); throw e; } - return { formID, closingDate: d }; + return { formID, closingDate }; }; export const updateSecurityAttribute = async (