mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 20:54:07 -08:00
b72040aa54
Signed-off-by: yoshino-s <cy-cui@outlook.com>
67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import type { IDataObject } from 'n8n-workflow';
|
|
import type { FormField, FormTriggerData, FormTriggerInput } from './interfaces';
|
|
|
|
export const prepareFormData = (
|
|
formTitle: string,
|
|
formDescription: string,
|
|
formSubmittedText: string | undefined,
|
|
formFields: FormField[],
|
|
testRun: boolean,
|
|
instanceId?: string,
|
|
) => {
|
|
const validForm = formFields.length > 0;
|
|
const utm_campaign = instanceId ? `&utm_campaign=${instanceId}` : '';
|
|
const n8nWebsiteLink = `https://n8n.io/?utm_source=n8n-internal&utm_medium=form-trigger${utm_campaign}`;
|
|
|
|
if (formSubmittedText === undefined) {
|
|
formSubmittedText = 'Your response has been recorded';
|
|
}
|
|
|
|
const formData: FormTriggerData = {
|
|
testRun,
|
|
validForm,
|
|
formTitle,
|
|
formDescription,
|
|
formSubmittedText,
|
|
n8nWebsiteLink,
|
|
formFields: [],
|
|
};
|
|
|
|
if (!validForm) {
|
|
return formData;
|
|
}
|
|
|
|
for (const [index, field] of formFields.entries()) {
|
|
const { fieldType, requiredField, multiselect } = field;
|
|
|
|
const input: IDataObject = {
|
|
id: `field-${index}`,
|
|
errorId: `error-field-${index}`,
|
|
label: field.fieldLabel,
|
|
inputRequired: requiredField ? 'form-required' : '',
|
|
};
|
|
|
|
if (multiselect) {
|
|
input.isMultiSelect = true;
|
|
input.multiSelectOptions =
|
|
field.fieldOptions?.values.map((e, i) => ({
|
|
id: `option${i}`,
|
|
label: e.option,
|
|
})) ?? [];
|
|
} else if (fieldType === 'dropdown') {
|
|
input.isSelect = true;
|
|
const fieldOptions = field.fieldOptions?.values ?? [];
|
|
input.selectOptions = fieldOptions.map((e) => e.option);
|
|
} else if (fieldType === 'textarea') {
|
|
input.isTextarea = true;
|
|
} else {
|
|
input.isInput = true;
|
|
input.type = fieldType as 'text' | 'number' | 'date';
|
|
}
|
|
|
|
formData.formFields.push(input as FormTriggerInput);
|
|
}
|
|
|
|
return formData;
|
|
};
|