From b476da16dbe42007b880f5f703a716f346d110cb Mon Sep 17 00:00:00 2001 From: Joaquin Montes Date: Fri, 3 Jan 2025 12:13:42 -0300 Subject: [PATCH] revert unstable_Form to not depend on EventKeys and parseFormData --- packages/basehub/src/events/primitive.tsx | 2 +- packages/basehub/src/react/form/primitive.tsx | 47 ++++++++++++------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/packages/basehub/src/events/primitive.tsx b/packages/basehub/src/events/primitive.tsx index 0feeeee..dcc1c4c 100644 --- a/packages/basehub/src/events/primitive.tsx +++ b/packages/basehub/src/events/primitive.tsx @@ -12,7 +12,7 @@ import { // eslint-disable-next-line import/no-unresolved } from "../index"; import type { ResolvedRef } from "../common-types"; -import { Field } from "../react/form/primitive"; +import type { Field } from "../react/form/primitive"; /* ------------------------------------------------------------------------------------------------- * Client diff --git a/packages/basehub/src/react/form/primitive.tsx b/packages/basehub/src/react/form/primitive.tsx index 5dab7d6..bd9ac46 100644 --- a/packages/basehub/src/react/form/primitive.tsx +++ b/packages/basehub/src/react/form/primitive.tsx @@ -2,7 +2,7 @@ /* eslint-disable jsx-a11y/alt-text */ /* eslint-disable @next/next/no-img-element */ import { useCallback, type ReactNode } from "react"; -import { EventKeys, parseFormData, sendEvent, updateEvent } from "../../events"; +import { sendEvent, updateEvent } from "../../events"; // this needs to match our BSHBEventSchema scalar type so that it _just works_ export type Field = { @@ -53,7 +53,7 @@ export type HandlerProps = ExtractPropsForHandler< type CustomBlockBase = { readonly __typename: string }; export type CustomBlocksBase = readonly CustomBlockBase[]; -export type FormProps = { +export type FormProps = { schema: Field[]; components?: Partial; disableDefaultComponents?: boolean; @@ -76,7 +76,7 @@ export type FormProps = { "action" | "onSubmit" | "children" >; -export function unstable_Form({ +export function unstable_Form({ schema, components, disableDefaultComponents, @@ -92,28 +92,41 @@ export function unstable_Form({ e.preventDefault(); const form = e.target as HTMLFormElement; const formData = new FormData(form); + const formattedData: Record = {}; - const parserKey = - action.type === "update" ? action.adminKey : action.ingestKey; - const parsedResult = fields - ? parseFormData(parserKey, fields, formData) - : undefined; - if (parsedResult?.success === false) { - throw new Error( - Object.entries(parsedResult.errors) - .map(([key, value]) => `${key}: ${value}`) - .join("\n") - ); - } + fields?.forEach((field) => { + const key = field.name; + const value = formData.get(key); + switch (field.type) { + case "checkbox": + formattedData[key] = value === "on"; + break; + case "select": + formattedData[key] = String(value).split(","); + break; + case "radio": + formattedData[key] = value; + break; + case "date": + case "datetime": + formattedData[key] = new Date(value as string).toISOString(); + break; + case "number": + formattedData[key] = Number(value); + break; + default: + formattedData[key] = value; + } + }); if (action.type === "update") { await updateEvent( action.adminKey as any, action.eventId, - parsedResult?.data as any + formattedData ); } else { - await sendEvent(action.ingestKey as any, parsedResult?.data as any); + await sendEvent(action.ingestKey as any, formattedData); } }, [action, fields]