mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 14:44:05 -08:00
3ddc176dfa
Github issue / Community forum post (link here to close automatically): based on https://github.com/joffcom/n8n-nodes-form-trigger --------- Co-authored-by: Giulio Andreini <g.andreini@gmail.com>
65 lines
1.7 KiB
TypeScript
65 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 {
|
|
input.isInput = true;
|
|
input.type = fieldType as 'text' | 'number' | 'date';
|
|
}
|
|
|
|
formData.formFields.push(input as FormTriggerInput);
|
|
}
|
|
|
|
return formData;
|
|
};
|