-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.js
More file actions
138 lines (117 loc) · 4.02 KB
/
contact.js
File metadata and controls
138 lines (117 loc) · 4.02 KB
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
import { registerCustomElement } from '@shgysk8zer0/kazoo/custom-elements.js';
import { on } from '@shgysk8zer0/kazoo/dom.js';
import { send } from '@shgysk8zer0/kazoo/slack.js';
import { whenIntersecting } from '@shgysk8zer0/kazoo/intersect.js';
import { sanitizer } from '@aegisjsproject/sanitizer/config/base.js';
import { formStyles } from './styles/forms.js';
import template from './contact.html.js';
import styles from './contact.css.js';
import { reset } from '@aegisjsproject/styles/reset.js';
import { componentBase } from '@aegisjsproject/styles/theme.js';
import { layers } from '@aegisjsproject/styles/layers.js';
const ENDPOINT = 'https://contact.kernvalley.us/api/slack';
const symbols = {
shadow: Symbol('shadow'),
internals: Symbol('internals'),
};
registerCustomElement('krv-contact', class HTMLKRVContactElement extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({ mode: 'closed' });
this[symbols.shadow] = shadow;
if (HTMLElement.prototype.attachInternals instanceof Function) {
this[symbols.internals] = this.attachInternals();
this[symbols.internals].states.add('--loading');
}
this.addEventListener('error', console.error);
}
async connectedCallback() {
await whenIntersecting(this);
const shadow = this[symbols.shadow];
shadow.adoptedStyleSheets = await Promise.all([
layers, reset, componentBase, formStyles,
new CSSStyleSheet().replace(styles),
]);
shadow.setHTML(template, { sanitizer });
on(shadow.querySelector('form'), {
reset: () => this.dispatchEvent(new Event('reset')),
submit: async event => {
event.preventDefault();
const target = event.target;
const data = new FormData(target);
try {
const resp = await send(ENDPOINT, {
name: data.get('name'),
email: data.get('email'),
phone: data.get('phone'),
url: data.get('url'),
subject: data.get('subject'),
body: data.get('body'),
});
if (resp.success) {
this.dispatchEvent(new Event('sent'));
if (this.hasOwnProperty(symbols.internals)) {
this[symbols.internals].states.delete('--error');
this[symbols.internals].states.add('--sent');
}
target.reset();
} else {
throw new Error(`<${resp.url}> [${resp.status} ${resp.statusText}]`);
}
} catch(error) {
if (this.hasOwnProperty(symbols.internals)) {
this[symbols.internals].states.delete('--sent');
this[symbols.internals].states.add('--error');
}
this.dispatchEvent(new ErrorEvent('error', {
error,
message: 'Error submitting form',
}));
}
}
});
this.dispatchEvent(new Event('ready'));
if (this.hasOwnProperty(symbols.internals)) {
this[symbols.internals].states.delete('--loading');
this[symbols.internals].states.add('--ready');
}
this.dispatchEvent(new Event('connected'));
}
[Symbol.dispose]() {
this.inert = true;
}
get ready() {
if (this[symbols.shadow].childElementCount > 2) {
return Promise.resolve();
} else {
return new Promise(resolve => this.addEventListener('ready', () => resolve(), { once: true }));
}
}
get whenConnected() {
if (this.isConnected) {
return Promise.resolve();
} else {
return new Promise(resolve => this.addEventListener('connected', () => resolve(), { once: true}));
}
}
set subject(val) {
this.ready.then(() => this[symbols.shadow].getElementById('krv-contact-subject').value = val);
}
set body(val) {
this.ready.then(() => this[symbols.shadow].getElementById('krv-contact-body').value = val);
}
set url(val) {
this.ready.then(() => this[symbols.shadow].getElementById('krv-contact-url').value = val);
}
/**
* Based on https://developer.chrome.com/articles/web-share-target/
*/
static fromSearchParams({ title = 'title', text = 'text', url = 'url' } = {}) {
const contactForm = new HTMLKRVContactElement();
const params = new URLSearchParams(location.search);
contactForm.subject = params.get(title);
contactForm.body = params.get(text);
contactForm.url = params.get(url);
return contactForm;
}
});