notify-me-wl 1.3.1 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +360 -360
- package/index.html +21 -21
- package/index.js +324 -296
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,360 +1,360 @@
|
|
|
1
|
-
### Coming Soon/Notify Me Widget
|
|
2
|
-
|
|
3
|
-
This JavaScript widget allows users to register their interest in a product that is either "coming soon" or "out of stock". Depending on the `data-type` attribute provided, the widget will display appropriate messages and functionality.
|
|
4
|
-
|
|
5
|
-
#### Features:
|
|
6
|
-
- Dynamically display a form to collect user information (mobile, first name, last name).
|
|
7
|
-
- Adjust form and button text based on the widget type (`notify-me` or `coming-soon`).
|
|
8
|
-
- Submit user data to a specified API endpoint with email or mobile as a path parameter and appropriate query parameters.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
<
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
- `data-
|
|
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
|
-
background:
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
#popup-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
#popup-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
#popup-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
font-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
const
|
|
222
|
-
const
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
},
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
popupBody.style.visibility = "visible";
|
|
284
|
-
popupBody.style.opacity = 1;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
function hidePopup() {
|
|
288
|
-
popupBody.style.visibility = "hidden";
|
|
289
|
-
popupBody.style.opacity = 0;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
popupOpenButton.addEventListener("click", showPopup);
|
|
293
|
-
popupClose.addEventListener("click", hidePopup);
|
|
294
|
-
|
|
295
|
-
// Form submission handler
|
|
296
|
-
if (form) {
|
|
297
|
-
form.addEventListener("submit", function (event) {
|
|
298
|
-
event.preventDefault();
|
|
299
|
-
|
|
300
|
-
const selectedSize = sizeSelect.value;
|
|
301
|
-
const selectedVariant = productData.variants.find((variant) => variant.title === selectedSize);
|
|
302
|
-
|
|
303
|
-
if (selectedVariant) {
|
|
304
|
-
const formData = {
|
|
305
|
-
variantRef: selectedVariant.id,
|
|
306
|
-
email: form.querySelector("input[name='email']").value,
|
|
307
|
-
subscribe: form.querySelector("input[name='mailList']").checked,
|
|
308
|
-
};
|
|
309
|
-
|
|
310
|
-
if (type === "coming-soon") {
|
|
311
|
-
formData.comingSoon = true;
|
|
312
|
-
formData.productRef = productData.id;
|
|
313
|
-
} else {
|
|
314
|
-
formData.notifyMe = true;
|
|
315
|
-
}
|
|
316
|
-
if (fields.includes("firstName")) {
|
|
317
|
-
formData.firstName = form.querySelector("input[name='firstName']").value;
|
|
318
|
-
}
|
|
319
|
-
if (fields.includes("lastName")) {
|
|
320
|
-
formData.lastName = form.querySelector("input[name='lastName']").value;
|
|
321
|
-
}
|
|
322
|
-
if (fields.includes("mobile")) {
|
|
323
|
-
formData.mobile = form.querySelector("input[name='mobile']").value;
|
|
324
|
-
formData.phone = form.querySelector("input[name='mobile']").value;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
let url = `https://api.au-
|
|
328
|
-
|
|
329
|
-
fetch(url, {
|
|
330
|
-
method: "POST",
|
|
331
|
-
headers: {
|
|
332
|
-
"Content-Type": "application/json",
|
|
333
|
-
Authorization: ACCESS_TOKEN,
|
|
334
|
-
"X-Twc-Tenant": TENANT_ID,
|
|
335
|
-
},
|
|
336
|
-
body: JSON.stringify(formData),
|
|
337
|
-
})
|
|
338
|
-
.then((response) => {
|
|
339
|
-
if (response.ok) {
|
|
340
|
-
alert("Form submitted");
|
|
341
|
-
hidePopup();
|
|
342
|
-
} else {
|
|
343
|
-
alert("Form submission failed");
|
|
344
|
-
}
|
|
345
|
-
})
|
|
346
|
-
.catch((error) => {
|
|
347
|
-
console.error("Error submitting form:", error);
|
|
348
|
-
alert("Form submission failed");
|
|
349
|
-
});
|
|
350
|
-
} else {
|
|
351
|
-
alert("Selected variant not found");
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
```
|
|
358
|
-
|
|
359
|
-
Replace `ACCESS_TOKEN` with your actual access token to authenticate API requests.
|
|
360
|
-
Replace `TENANT_ID` with your actual tenant to authenticate API requests.
|
|
1
|
+
### Coming Soon/Notify Me Widget
|
|
2
|
+
|
|
3
|
+
This JavaScript widget allows users to register their interest in a product that is either "coming soon" or "out of stock" (aka "notify me"). Depending on the `data-type` attribute provided, the widget will display appropriate messages and functionality.
|
|
4
|
+
|
|
5
|
+
#### Features:
|
|
6
|
+
- Dynamically display a form to collect user information (mobile, first name, last name).
|
|
7
|
+
- Adjust form and button text based on the widget type (`notify-me` or `coming-soon`).
|
|
8
|
+
- Submit user data to a specified API endpoint with email or mobile as a path parameter and appropriate query parameters.
|
|
9
|
+
- An option for the user to subscribe to the database (opt-in)
|
|
10
|
+
|
|
11
|
+
### Installation
|
|
12
|
+
|
|
13
|
+
Add the following script to your HTML to include the widget:
|
|
14
|
+
|
|
15
|
+
```html
|
|
16
|
+
<script src="https://cdn.jsdelivr.net/npm/notify-me-wl/index.js"></script>
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Usage
|
|
20
|
+
|
|
21
|
+
Add a button to your HTML where you want the widget to appear. The button should have an `id` of `popup-open`, a `data-fields` attribute with the fields you want to include in the form (as a JSON string), and a `data-type` attribute to specify the widget type (`notify-me` or `coming-soon`).
|
|
22
|
+
|
|
23
|
+
```html
|
|
24
|
+
<div id="notification-widget"></div>
|
|
25
|
+
<button id="popup-open" data-fields='["mobile", "firstName", "lastName"]' data-type="notify-me">Open Popup</button>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Configuration
|
|
29
|
+
|
|
30
|
+
- `data-fields`: A JSON array specifying the fields to include in the form. Possible values are `"mobile"`, `"firstName"`, and `"lastName"`.
|
|
31
|
+
- `data-type`: Specifies the type of the widget. Possible values are `"notify-me"` and `"coming-soon"`.
|
|
32
|
+
|
|
33
|
+
### Example
|
|
34
|
+
|
|
35
|
+
```html
|
|
36
|
+
<div id="notification-widget"></div>
|
|
37
|
+
<button id="popup-open" data-fields='["mobile"]' data-type="notify-me">Open Popup</button>
|
|
38
|
+
|
|
39
|
+
<script>
|
|
40
|
+
// The script provided will be included here
|
|
41
|
+
</script>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### How It Works
|
|
45
|
+
|
|
46
|
+
1. **Initialization**: The script listens for the `DOMContentLoaded` event to initialize the widget.
|
|
47
|
+
2. **Styles Injection**: It injects necessary styles for the modal and form elements.
|
|
48
|
+
3. **Modal Creation**: Creates the modal structure and appends it to the `notification-widget` div.
|
|
49
|
+
4. **Dynamic Content**: Based on the `data-type`, it sets the appropriate messages and button text.
|
|
50
|
+
5. **Form Submission**: Collects form data and submits it to the API with the mandatory email field and other possible fields.
|
|
51
|
+
|
|
52
|
+
### API Request Example
|
|
53
|
+
|
|
54
|
+
# For sandbox environment
|
|
55
|
+
```
|
|
56
|
+
https://api.au-sandbox.thewishlist.io/services/wsservice/api/wishlist/items/customerInterest
|
|
57
|
+
```
|
|
58
|
+
# For production environment
|
|
59
|
+
```
|
|
60
|
+
https://api.au-aws.thewishlist.io/services/wsservice/api/wishlist/items/customerInterest
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
### Full Script
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
// Access token for the API
|
|
68
|
+
const ACCESS_TOKEN = "123"; // Replace `ACCESS_TOKEN` with your actual access token to authenticate API requests.
|
|
69
|
+
const TENANT_ID = "victoria-woods"; // Replace `TENANT_ID` with your actual tenant ID.
|
|
70
|
+
|
|
71
|
+
document.addEventListener("DOMContentLoaded", function () {
|
|
72
|
+
const wrapper = document.getElementById("notification-widget");
|
|
73
|
+
if (!wrapper) return;
|
|
74
|
+
|
|
75
|
+
// Define and apply styles for the popup and button
|
|
76
|
+
const styles = `
|
|
77
|
+
.notification-btn {
|
|
78
|
+
background-color: #fff;
|
|
79
|
+
border: 1px solid black;
|
|
80
|
+
color: black;
|
|
81
|
+
padding: 12px 24px;
|
|
82
|
+
text-align: center;
|
|
83
|
+
width: 100%;
|
|
84
|
+
cursor: pointer;
|
|
85
|
+
text-transform: uppercase;
|
|
86
|
+
}
|
|
87
|
+
#popup-body.overlay {
|
|
88
|
+
position: fixed;
|
|
89
|
+
z-index: 9998;
|
|
90
|
+
top: 0;
|
|
91
|
+
bottom: 0;
|
|
92
|
+
left: 0;
|
|
93
|
+
right: 0;
|
|
94
|
+
background: rgba(0, 0, 0, 0.7);
|
|
95
|
+
transition: opacity 200ms;
|
|
96
|
+
visibility: hidden;
|
|
97
|
+
opacity: 0;
|
|
98
|
+
}
|
|
99
|
+
#popup-wrapper {
|
|
100
|
+
margin: 70px auto;
|
|
101
|
+
z-index: 9999;
|
|
102
|
+
padding: 15px;
|
|
103
|
+
background: #fff;
|
|
104
|
+
border-radius: 5px;
|
|
105
|
+
max-width: 360px;
|
|
106
|
+
position: relative;
|
|
107
|
+
}
|
|
108
|
+
#popup-close {
|
|
109
|
+
position: absolute;
|
|
110
|
+
top: 5px;
|
|
111
|
+
right: 15px;
|
|
112
|
+
transition: all 200ms;
|
|
113
|
+
font-size: 30px;
|
|
114
|
+
font-weight: bold;
|
|
115
|
+
text-decoration: none;
|
|
116
|
+
color: #333;
|
|
117
|
+
cursor: pointer;
|
|
118
|
+
}
|
|
119
|
+
#popup-close:hover {
|
|
120
|
+
color: #d80606;
|
|
121
|
+
}
|
|
122
|
+
#popup-title {
|
|
123
|
+
margin: 0;
|
|
124
|
+
font-size: 20px;
|
|
125
|
+
max-width: 350px;
|
|
126
|
+
text-transform: uppercase;
|
|
127
|
+
}
|
|
128
|
+
#popup-text {
|
|
129
|
+
margin: 10px 0;
|
|
130
|
+
font-size: 14px;
|
|
131
|
+
}
|
|
132
|
+
#popup-form {
|
|
133
|
+
display: flex;
|
|
134
|
+
flex-direction: column;
|
|
135
|
+
gap: 10px;
|
|
136
|
+
}
|
|
137
|
+
#popup-form input {
|
|
138
|
+
padding: 0.475em 1em;
|
|
139
|
+
border: 1px solid #caced1;
|
|
140
|
+
border-radius: 0.25rem;
|
|
141
|
+
font-size: 1.15rem;
|
|
142
|
+
max-width: 100%;
|
|
143
|
+
}
|
|
144
|
+
#popup-form input::placeholder {
|
|
145
|
+
font-size: 1rem;
|
|
146
|
+
}
|
|
147
|
+
#popup-form button {
|
|
148
|
+
font-size: 0.85rem;
|
|
149
|
+
padding: 0.975em 1em;
|
|
150
|
+
font-weight: 600;
|
|
151
|
+
border: none;
|
|
152
|
+
text-transform: uppercase;
|
|
153
|
+
background: #aca475;
|
|
154
|
+
color: #fff;
|
|
155
|
+
cursor: pointer;
|
|
156
|
+
}
|
|
157
|
+
.custom-select {
|
|
158
|
+
position: relative;
|
|
159
|
+
}
|
|
160
|
+
.custom-select select {
|
|
161
|
+
appearance: none;
|
|
162
|
+
width: 100%;
|
|
163
|
+
font-size: 1.15rem;
|
|
164
|
+
padding: 0.475em 1em;
|
|
165
|
+
background-color: #fff;
|
|
166
|
+
border: 1px solid #caced1;
|
|
167
|
+
border-radius: 0.25rem;
|
|
168
|
+
color: #000;
|
|
169
|
+
cursor: pointer;
|
|
170
|
+
}
|
|
171
|
+
.custom-select::before,
|
|
172
|
+
.custom-select::after {
|
|
173
|
+
--size: 0.3rem;
|
|
174
|
+
content: "";
|
|
175
|
+
position: absolute;
|
|
176
|
+
right: 1rem;
|
|
177
|
+
pointer-events: none;
|
|
178
|
+
}
|
|
179
|
+
.custom-select::before {
|
|
180
|
+
border-left: var(--size) solid transparent;
|
|
181
|
+
border-right: var(--size) solid transparent;
|
|
182
|
+
border-bottom: var(--size) solid black;
|
|
183
|
+
top: 40%;
|
|
184
|
+
}
|
|
185
|
+
.custom-select::after {
|
|
186
|
+
border-left: var(--size) solid transparent;
|
|
187
|
+
border-right: var(--size) solid transparent;
|
|
188
|
+
border-top: var(--size) solid black;
|
|
189
|
+
top: 55%;
|
|
190
|
+
}
|
|
191
|
+
`;
|
|
192
|
+
|
|
193
|
+
const styleSheet = document.createElement("style");
|
|
194
|
+
styleSheet.type = "text/css";
|
|
195
|
+
styleSheet.innerText = styles;
|
|
196
|
+
document.head.appendChild(styleSheet);
|
|
197
|
+
|
|
198
|
+
// Create and append the overlay for the popup
|
|
199
|
+
const overlay = document.createElement("div");
|
|
200
|
+
overlay.innerHTML = `
|
|
201
|
+
<div id="popup-body" class="overlay">
|
|
202
|
+
<div id="popup-wrapper">
|
|
203
|
+
<h3 id="popup-title"></h3>
|
|
204
|
+
<span id="popup-close">×</span>
|
|
205
|
+
<div id="popup-text"></div>
|
|
206
|
+
<form id="popup-form">
|
|
207
|
+
<div class="custom-select">
|
|
208
|
+
<select name="select-size" required>
|
|
209
|
+
</select>
|
|
210
|
+
</div>
|
|
211
|
+
<input name="email" placeholder="Email" type="email" required />
|
|
212
|
+
</form>
|
|
213
|
+
</div>
|
|
214
|
+
</div>
|
|
215
|
+
`;
|
|
216
|
+
|
|
217
|
+
wrapper.appendChild(overlay);
|
|
218
|
+
|
|
219
|
+
const popupBody = document.getElementById("popup-body");
|
|
220
|
+
const popupClose = document.getElementById("popup-close");
|
|
221
|
+
const popupOpenButton = document.getElementById("popup-open");
|
|
222
|
+
const popupTitle = document.getElementById("popup-title");
|
|
223
|
+
const popupText = document.getElementById("popup-text");
|
|
224
|
+
const sizeSelect = document.querySelector("select[name='select-size']");
|
|
225
|
+
const form = overlay.querySelector("#popup-form");
|
|
226
|
+
|
|
227
|
+
if (!popupBody || !popupClose || !popupOpenButton || !popupTitle || !sizeSelect) return;
|
|
228
|
+
|
|
229
|
+
// Parse fields and type to be included in the form from data attribute
|
|
230
|
+
const fields = JSON.parse(popupOpenButton.getAttribute("data-fields") || '["email"]');
|
|
231
|
+
const type = popupOpenButton.getAttribute("data-type") || "notify-me";
|
|
232
|
+
const typeConfig = {
|
|
233
|
+
"notify-me": {
|
|
234
|
+
text: "Register to receive a notification as soon as this item is back in stock",
|
|
235
|
+
buttonText: "Notify me",
|
|
236
|
+
},
|
|
237
|
+
"coming-soon": {
|
|
238
|
+
text: "Register your interest to hear more about this item",
|
|
239
|
+
buttonText: "Register Interest",
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// Get product data from Shopify's global variable
|
|
244
|
+
const productData = window?.currentProduct;
|
|
245
|
+
|
|
246
|
+
// Map for form fields
|
|
247
|
+
popupText.innerText = typeConfig[type].text;
|
|
248
|
+
|
|
249
|
+
const fieldMap = {
|
|
250
|
+
email: `<input name="email" placeholder="Email" type="email" required />`,
|
|
251
|
+
mobile: `<input name="mobile" placeholder="Mobile" type="tel" required />`,
|
|
252
|
+
firstName: `<input name="firstName" placeholder="First name" type="text" required />`,
|
|
253
|
+
lastName: `<input name="lastName" placeholder="Last name" type="text" required />`,
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// Add fields to the form based on the parsed fields
|
|
257
|
+
fields.forEach((field) => {
|
|
258
|
+
if (fieldMap[field]) {
|
|
259
|
+
form.insertAdjacentHTML("beforeend", fieldMap[field]);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
form.insertAdjacentHTML("beforeend", `<div id="checkbox-wrapper"></div>`);
|
|
264
|
+
const checkboxWrapper = document.getElementById("checkbox-wrapper");
|
|
265
|
+
checkboxWrapper.insertAdjacentHTML("beforeend", `<input type="checkbox" name="mailList" id="mailList" />`);
|
|
266
|
+
checkboxWrapper.insertAdjacentHTML("beforeend", `<label for="mailList">Subscribe to our mailing list</label>`);
|
|
267
|
+
form.insertAdjacentHTML("beforeend", `<button type="submit">${typeConfig[type].buttonText}</button>`);
|
|
268
|
+
|
|
269
|
+
// Show/hide the popup
|
|
270
|
+
function showPopup() {
|
|
271
|
+
if (!productData) {
|
|
272
|
+
alert("Product data not found");
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
// Set the popup title and populate the size dropdown
|
|
276
|
+
popupTitle.innerHTML = productData.title;
|
|
277
|
+
productData.variants.forEach((variant) => {
|
|
278
|
+
const option = document.createElement("option");
|
|
279
|
+
option.value = variant.title;
|
|
280
|
+
option.textContent = variant.title;
|
|
281
|
+
sizeSelect.appendChild(option);
|
|
282
|
+
});
|
|
283
|
+
popupBody.style.visibility = "visible";
|
|
284
|
+
popupBody.style.opacity = 1;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function hidePopup() {
|
|
288
|
+
popupBody.style.visibility = "hidden";
|
|
289
|
+
popupBody.style.opacity = 0;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
popupOpenButton.addEventListener("click", showPopup);
|
|
293
|
+
popupClose.addEventListener("click", hidePopup);
|
|
294
|
+
|
|
295
|
+
// Form submission handler
|
|
296
|
+
if (form) {
|
|
297
|
+
form.addEventListener("submit", function (event) {
|
|
298
|
+
event.preventDefault();
|
|
299
|
+
|
|
300
|
+
const selectedSize = sizeSelect.value;
|
|
301
|
+
const selectedVariant = productData.variants.find((variant) => variant.title === selectedSize);
|
|
302
|
+
|
|
303
|
+
if (selectedVariant) {
|
|
304
|
+
const formData = {
|
|
305
|
+
variantRef: selectedVariant.id,
|
|
306
|
+
email: form.querySelector("input[name='email']").value,
|
|
307
|
+
subscribe: form.querySelector("input[name='mailList']").checked,
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
if (type === "coming-soon") {
|
|
311
|
+
formData.comingSoon = true;
|
|
312
|
+
// formData.productRef = productData.id;
|
|
313
|
+
} else {
|
|
314
|
+
formData.notifyMe = true;
|
|
315
|
+
}
|
|
316
|
+
if (fields.includes("firstName")) {
|
|
317
|
+
formData.firstName = form.querySelector("input[name='firstName']").value;
|
|
318
|
+
}
|
|
319
|
+
if (fields.includes("lastName")) {
|
|
320
|
+
formData.lastName = form.querySelector("input[name='lastName']").value;
|
|
321
|
+
}
|
|
322
|
+
if (fields.includes("mobile")) {
|
|
323
|
+
formData.mobile = form.querySelector("input[name='mobile']").value;
|
|
324
|
+
// formData.phone = form.querySelector("input[name='mobile']").value;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
let url = `https://api.au-sandbox.thewishlist.io/services/wsservice/api/wishlist/items/customerInterest`;
|
|
328
|
+
|
|
329
|
+
fetch(url, {
|
|
330
|
+
method: "POST",
|
|
331
|
+
headers: {
|
|
332
|
+
"Content-Type": "application/json",
|
|
333
|
+
Authorization: ACCESS_TOKEN,
|
|
334
|
+
"X-Twc-Tenant": TENANT_ID,
|
|
335
|
+
},
|
|
336
|
+
body: JSON.stringify(formData),
|
|
337
|
+
})
|
|
338
|
+
.then((response) => {
|
|
339
|
+
if (response.ok) {
|
|
340
|
+
alert("Form submitted");
|
|
341
|
+
hidePopup();
|
|
342
|
+
} else {
|
|
343
|
+
alert("Form submission failed");
|
|
344
|
+
}
|
|
345
|
+
})
|
|
346
|
+
.catch((error) => {
|
|
347
|
+
console.error("Error submitting form:", error);
|
|
348
|
+
alert("Form submission failed");
|
|
349
|
+
});
|
|
350
|
+
} else {
|
|
351
|
+
alert("Selected variant not found");
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
Replace `ACCESS_TOKEN` with your actual access token to authenticate API requests.
|
|
360
|
+
Replace `TENANT_ID` with your actual tenant to authenticate API requests.
|