-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfeedback.ts
156 lines (147 loc) · 4.33 KB
/
feedback.ts
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
import {customElement, state} from 'lit/decorators.js';
import {html, css} from 'lit';
// @ts-ignore
@customElement('proj-feedback')
export class ProjFeedback extends (window as any).gmfapi.elements.ToolPanelElement {
@state()
private show_send = false;
@state()
private permalink: string = window.location.href;
private email: string = '';
private email_optional: string = '';
private feedback_text: string = '';
private url_: string;
private subscriptions_ = [];
static styles = [
...(window as any).gmfapi.elements.ToolPanelElement.styles,
css`
.modal-footer {
border-top: 0.06rem solid var(--color-light);
}
`,
];
connectedCallback(): void {
super.connectedCallback();
this.subscriptions_.push(
(window as any).gmfapi.store.config.getConfig().subscribe({
next: (configuration) => {
if (configuration) {
this.url_ = new URL(configuration.sitnFeedbackPath, configuration.gmfBase).href;
}
},
}),
);
window.addEventListener('popstate', () => {
this.permalink = window.location.href;
});
}
render() {
return html`${this.getTitle('Signaler un problème')} <label for="email">Votre email</label><br />
<input
input="text"
placeholder="[email protected]"
name="email"
class="form-control"
id="email"
.value="${this.email}"
@input=${(e) => {
this.email = e.target.value;
}}
/>
<br />
<label for="email_optional">Inclure une personne en CC</label><br />
<input
type="text"
placeholder="[email protected]"
name="email_optional"
class="form-control"
id="email_optional"
.value="${this.email_optional}"
@input=${(e) => {
this.email_optional = e.target.value;
}}
/>
<br />
<label for="feedback_text">Votre description du problème concernant la carte *</label><br />
<textarea
rows="4"
cols="40"
class="form-control"
id="feedback_text"
.value="${this.feedback_text}"
@input=${(e) => {
this.feedback_text = e.target.value;
}}
maxlength="1000"
placeholder="Maximum 1000 caractères"
>
</textarea>
<br />
<label for="permalink">L'URL ci-dessous sera envoyée</label>
<input
type="text"
name="permalink"
class="form-control"
id="permalink"
.value="${this.permalink}"
readonly
/>
<br />
<button type="submit" class="btn prime" @click="${this.feedbackSubmit}">Envoyer</button>
${this.show_send
? html`
<div class="fas fa-spinner fa-spin"></div>
En cours d'envoi...
`
: ''}`;
}
private feedbackSubmit() {
if (
(this.email && this.email.indexOf('@') === -1) ||
(this.email_optional && this.email_optional.indexOf('@') === -1)
) {
alert("Une adresse email n'est pas valide");
return;
}
if (!this.feedback_text) {
alert('Veuillez saisir une descritption du problème.');
return;
}
if (this.feedback_text.length > 1000) {
alert('Votre texte est trop long (max 1000 caractères).');
return;
}
this.show_send = true;
let url = new URL(this.url_);
let params = new URLSearchParams(url.search.slice(1));
let formdata = new FormData();
formdata.set('permalink', this.permalink.toString());
formdata.set('ua', navigator.userAgent);
formdata.set('email', this.email);
formdata.set('email_optional', this.email_optional);
formdata.set('feedback', this.feedback_text);
fetch(this.url_, {
method: 'POST',
body: formdata,
})
.then((response) => {
if (!response.ok) {
throw new Error(`${response.statusText} (${response.status})`);
}
this.show_send = false;
alert(
[
'Merci! Votre demande est bien partie.',
'',
'Suivant votre demande, une personne prendra bientôt contact avec vous.',
].join('\n'),
);
(window as any).gmfapi.store.panels.closeToolPanel();
})
.catch((error) => {
console.error(error);
alert('Une erreur est survenue.');
this.show_send = false;
});
}
}