-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact-form-sdk.js
164 lines (137 loc) · 4.2 KB
/
contact-form-sdk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
*
* Lumify Contact Form SDK
* Author: Lumify Labs
* Website: lumifylabs.com
* Version: 0.1
*
* @param form The target form element
* @param endpoint The endpoint for the contact form
* @param options Override SDK options (optional)
*
*/
class LumifyContactFormSdk {
defaultOptions = {
formMessageClass: 'lumify-form-message',
fieldErrorClass: 'lumify-field-error'
}
constructor(form, endpoint, options = {}) {
this.form = form;
this.endpoint = endpoint;
this.options = {...this.defaultOptions, ...options};
}
errorData = {};
/**
* Submits contact form
*
* @returns Promise<Response>
* @throws Error
*/
async submitForm() {
if (!this.endpoint) {
throw new Error('Configuration error: Please provide a valid API endpoint.')
}
const formData = this.getFormData();
try {
const response = await fetch(this.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify(formData),
});
if (!response.ok) {
this.errorData = await response.json();
if (this.errorData.errors) {
throw new Error('Check the form for validation errors.');
} else {
throw new Error(response.statusText);
}
}
return await response.json();
} catch (error) {
throw new Error(error.message);
}
}
/**
* Display a success or error message on the form
*
* @param {string} message The message to show on the form
* @param state Defaults to 'success' (optional)
*/
showFormMessage(message, state = 'success') {
this.clearFormMessage();
const formMessageElement = this.form.querySelector(`.${this.options.formMessageClass}`);
if (formMessageElement) {
formMessageElement.innerText = message;
formMessageElement.classList.add(`${this.options.formMessageClass}-${state}`);
}
}
/**
* Clear the form message
*
* @returns void
*/
clearFormMessage() {
const formMessageElements = this.form.querySelectorAll(`.${this.options.formMessageClass}`);
formMessageElements.forEach(element => {
element.classList.remove(`${this.options.formMessageClass}-success`);
element.classList.remove(`${this.options.formMessageClass}-error`);
element.innerText = '';
});
}
/**
* Show errors under each field
*
* @param {Object} errors Key-value pairs of errors
* @returns void
*/
showFieldErrors(errors) {
this.clearFieldErrors(this.form);
for (let field in errors) {
const errorElement = this.form.querySelector(`.${this.options.fieldErrorClass}.${this.options.fieldErrorClass}-${field.replace('.', '-')}`);
if (errorElement) {
errorElement.innerText = errors[field].join(', ');
}
}
}
/**
* Clears field errors
*
* @returns void
*/
clearFieldErrors() {
this.errorData = {};
const errorElements = this.form.querySelectorAll(`.${this.options.fieldErrorClass}`);
errorElements.forEach(element => {
element.innerText = '';
});
}
/**
* Collect the values from the form data
*
* @returns void
*/
getFormData() {
const formData = {};
const inputs = this.form.querySelectorAll('input, textarea, select');
inputs.forEach(input => {
formData[input.name] = input.value;
});
return formData;
}
/**
* Reset form values, clear form message, and clear any errors
*
* @returns void
*/
resetForm() {
const inputs = this.form.querySelectorAll('input, textarea, select');
inputs.forEach(input => {
input.value = '';
});
this.clearFormMessage()
this.clearFieldErrors()
}
}