n8n/packages/nodes-base/nodes/Form/utils.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
1.7 KiB
TypeScript
Raw Normal View History

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;
};