-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
77 lines (71 loc) · 2.25 KB
/
App.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
var editor = grapesjs.init({
fromElement: 1,
container: "#gjs",
allowScripts: 1,
plugins: ['grapesjs-preset-webpage'],
pluginsOpts: {
"gjs-blocks-basic": {},
},
});
editor.DomComponents.addType('custom', {
model: {
defaults: {
tagName: "form",
name: "form",
attributes: {
id: "emailForm",
class: "contact-form"
},
content:
`<div class="form-group row contact-email">
<label for="inputEmail" class="">Enter your email</label>
<div class="">
<input type="email" class="form-control" id="inputEmail" name="contact[email]" placeholder="test@test.com" required />
<button type="submit" class="btn btn-primary btn-block">Submit</button>
</div>
</div>`,
traits: [
'data-url'
],
script: function () {
const form = this;
form.addEventListener('submit', function (event) {
event.preventDefault();
const url = this.attributes['data-url'] ? this.attributes['data-url'].value : '';
if (url.trim() === '') {
alert('Please add post request url');
return;
}
const regex = new RegExp(/[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi);
if (regex.test(url) === false) {
alert('Invaliid post request url');
return;
}
const email = form.querySelector('[name="contact[email]"]').value;
fetch('', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: email,
}),
})
.then(response => response.text())
.then(data => {
alert('API Response: ' + JSON.stringify(data));
})
.catch((error) => {
console.error('Error:', error);
alert('Error: ' + error.message);
});
});
},
},
}
});
editor.BlockManager.add("custom", {
label: "Email",
content: { type: 'custom' },
attributes: { class: 'fa fa-envelope-o' },
});