Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Some fixes in form-handlers (#1616)

Roman Pominovbfd69c89471f
diff --git a/packages/form-handlers/package.json b/packages/form-handlers/package.json
index d3883e3ed8..90f48ae8ab 100644
--- a/packages/form-handlers/package.json
+++ b/packages/form-handlers/package.json
@@ -14,7 +14,9 @@
"dts": "tsc --declarationDir lib/types",
"lint": "eslint ./src --ext .ts,.tsx --max-warnings 0"
},
- "dependencies": {},
+ "dependencies": {
+ "@webstudio-is/project-build": "workspace:^"
+ },
"devDependencies": {
"@jest/globals": "^29.3.1",
"@webstudio-is/jest-config": "workspace:^",
diff --git a/packages/form-handlers/src/index.ts b/packages/form-handlers/src/index.ts
index 15f395324a..9addf56192 100644
--- a/packages/form-handlers/src/index.ts
+++ b/packages/form-handlers/src/index.ts
@@ -1,6 +1,8 @@
export {
formToEmail,
formIdFieldName,
+ getFormId,
+ hasMatchingForm,
type EmailInfo,
type FormInfo,
} from "./shared";
diff --git a/packages/form-handlers/src/shared.ts b/packages/form-handlers/src/shared.ts
index edd8120f21..5b7c5fb265 100644
--- a/packages/form-handlers/src/shared.ts
+++ b/packages/form-handlers/src/shared.ts
@@ -1,3 +1,5 @@
+import type { Build } from "@webstudio-is/project-build";
+
export const formIdFieldName = "ws--form-id";
// Input data common for all handlers
@@ -20,12 +22,15 @@ export type EmailInfo = {
export type Result = { success: true } | { success: false; errors: string[] };
-export const getFormEntries = (formData: FormData) =>
- [...formData.entries()].filter(([key]) => key !== formIdFieldName);
+/** Returns form entries that should be send in email: removes `File` entries and `formId` */
+export const getFormEntries = (formData: FormData): [string, string][] =>
+ [...formData.entries()].flatMap(([key, value]) =>
+ key !== formIdFieldName && typeof value === "string" ? [[key, value]] : []
+ );
export const getFormId = (formData: FormData) => {
for (const [key, value] of formData.entries()) {
- if (key === formIdFieldName) {
+ if (key === formIdFieldName && typeof value === "string") {
return value;
}
}
@@ -99,3 +104,23 @@ export const getErrors = (
return json.errors;
}
};
+
+/** Checks that `formData` corresponds to a form in the `instances` tree */
+export const hasMatchingForm = (
+ formData: FormData,
+ instances: Build["instances"]
+) => {
+ const formId = getFormId(formData);
+
+ if (formId === undefined) {
+ return false;
+ }
+
+ return instances.some(
+ ([, instance]) => instance.id === formId && instance.component === "Form"
+ );
+
+ // @todo:
+ // We could also check that each entry in formData has a corresponding input control in the tree,
+ // but that seem like an overkill for now.
+};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index e29a0b0b50..a339418607 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -836,6 +836,10 @@ importers:
version: 3.19.1
packages/form-handlers:
+ dependencies:
+ '@webstudio-is/project-build':
+ specifier: workspace:^
+ version: link:../project-build
devDependencies:
'@jest/globals':
specifier: ^29.3.1