Skip to content

Commit 93a901d

Browse files
committed
add prechecks
1 parent 7b1fde9 commit 93a901d

3 files changed

Lines changed: 100 additions & 48 deletions

File tree

‎public/js/menu.js‎

Lines changed: 97 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class PopupMenu {
88
onSubmit = null,
99
onSubmitClicked = null,
1010
showErrors = true,
11+
precheckEndpoint = null,
1112
}) {
1213
this.title = title
1314
this.description = description
@@ -21,6 +22,38 @@ class PopupMenu {
2122
this.showErrors = showErrors
2223
}
2324

25+
async request(endpoint, data) {
26+
try {
27+
const response = await fetch(endpoint, {
28+
method: "POST",
29+
headers: {
30+
"Content-Type": "application/json",
31+
},
32+
body: JSON.stringify(data),
33+
})
34+
35+
const result = await response.json()
36+
37+
if (!response.ok) {
38+
if (this.showErrors)
39+
this.displayError(result?.error ?? "Something went wrong")
40+
41+
return
42+
}
43+
} catch (err) {
44+
this.displayError("Could not connect to server")
45+
console.error(err)
46+
}
47+
}
48+
49+
async validate() {
50+
if (typeof str === "string" && str.trim().length > 0) {
51+
const data = Object.fromEntries(new FormData(e.target).entries())
52+
53+
await this.request(this.precheckEndpoint, data)
54+
}
55+
}
56+
2457
render() {
2558
const wrapper = document.createElement("div")
2659
wrapper.className = "popup-menu"
@@ -41,7 +74,20 @@ class PopupMenu {
4174

4275
wrapper.innerHTML = html
4376

44-
wrapper.querySelector("form").addEventListener("submit", async (e) => {
77+
const form = wrapper.querySelector("form")
78+
79+
const fields = [...form.querySelectorAll("input[required]")]
80+
const lastField = fields.at(-1)
81+
82+
lastField.addEventListener("blur", async (e) => {
83+
const allFilled = fields.every((input) => input.value.trim())
84+
85+
if (allFilled) {
86+
await this.validate()
87+
}
88+
})
89+
90+
form.addEventListener("submit", async (e) => {
4591
e.preventDefault()
4692

4793
this.clearError()
@@ -60,27 +106,7 @@ class PopupMenu {
60106
return
61107
}
62108

63-
try {
64-
const response = await fetch(this.endpoint, {
65-
method: "POST",
66-
headers: {
67-
"Content-Type": "application/json",
68-
},
69-
body: JSON.stringify(data),
70-
})
71-
72-
const result = await response.json()
73-
74-
if (!response.ok) {
75-
if (this.showErrors)
76-
this.displayError(result?.error ?? "Something went wrong")
77-
78-
return
79-
}
80-
} catch (err) {
81-
this.displayError("Could not connect to server")
82-
console.error(err)
83-
}
109+
await this.request(this.endpoint, data)
84110

85111
if (this.onSubmit) {
86112
this.onSubmit(response)
@@ -207,6 +233,7 @@ class Menu {
207233
onSubmit = null,
208234
onSubmitClicked = null,
209235
showErrors = true,
236+
precheckEndpoint = null,
210237
}) {
211238
this.title = title
212239
this.endpoint = endpoint
@@ -217,6 +244,39 @@ class Menu {
217244
this.container = this.render()
218245
this.overlay = null
219246
this.showErrors = showErrors
247+
this.precheckEndpoint = precheckEndpoint
248+
}
249+
250+
async request(data, endpoint) {
251+
try {
252+
const response = await fetch(endpoint, {
253+
method: "POST",
254+
headers: {
255+
"Content-Type": "application/json",
256+
},
257+
body: JSON.stringify(data),
258+
})
259+
260+
const result = await response.json()
261+
262+
if (!response.ok) {
263+
if (this.showErrors)
264+
this.displayError(result?.error ?? "Something went wrong")
265+
266+
return
267+
}
268+
} catch (err) {
269+
this.displayError("Could not connect to server")
270+
console.error(err)
271+
}
272+
}
273+
274+
async validate() {
275+
if (typeof str === "string" && str.trim().length > 0) {
276+
const data = Object.fromEntries(new FormData(e.target).entries())
277+
278+
await this.request(this.precheckEndpoint, data)
279+
}
220280
}
221281

222282
render() {
@@ -238,7 +298,20 @@ class Menu {
238298

239299
wrapper.innerHTML = html
240300

241-
wrapper.querySelector("form").addEventListener("submit", async (e) => {
301+
const form = wrapper.querySelector("form")
302+
303+
const fields = [...form.querySelectorAll("input[required]")]
304+
const lastField = fields.at(-1)
305+
306+
lastField.addEventListener("blur", async (e) => {
307+
const allFilled = fields.every((input) => input.value.trim())
308+
309+
if (allFilled) {
310+
await this.validate()
311+
}
312+
})
313+
314+
form.addEventListener("submit", async (e) => {
242315
e.preventDefault()
243316

244317
const data = Object.fromEntries(new FormData(e.target).entries())
@@ -247,36 +320,12 @@ class Menu {
247320
if (!this.onSubmitClicked(e, data)) return
248321
}
249322

250-
try {
251-
const response = await fetch(this.endpoint, {
252-
method: "POST",
253-
headers: {
254-
"Content-Type": "application/json",
255-
},
256-
body: JSON.stringify(data),
257-
})
258-
259-
const result = await response.json()
260-
261-
if (!response.ok) {
262-
if (this.showErrors)
263-
this.displayError(result?.error ?? "Something went wrong")
264-
265-
return
266-
}
267-
} catch (err) {
268-
this.displayError("Could not connect to server")
269-
console.error(err)
270-
}
323+
await this.request(this.endpoint, data)
271324

272325
if (this.onSubmit) {
273326
this.onSubmit(response)
274327
}
275328

276-
if (this.onSubmit) {
277-
this.onSubmit()
278-
}
279-
280329
this.close()
281330
})
282331

‎src/routes/data.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ router.post("/mailbox/select", async (req, res, next) => {
163163
throw new HttpError(400, "Bad Request")
164164
}
165165

166+
EmailAllowed(email)
167+
166168
await EnsureMailboxOwnershipAsync(user, email)
167169

168170
if (!req.session.mail) {

‎views/home.ejs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
const createMenu = new Menu({
5555
title: "Create Mailbox",
5656
endpoint: "<%= prefix %>/data/mailbox/create",
57+
precheckEndpoint: "<%= prefix %>/data/mailbox/check",
5758
actions: [
5859
{
5960
label: "Create",

0 commit comments

Comments
 (0)