-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.js
92 lines (78 loc) · 2.47 KB
/
view.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const clipboard = () => {
const curl = document.getElementById("curl");
navigator.clipboard.writeText(curl.innerText);
const copy = document.getElementById("copy");
const text = copy.innerText;
copy.innerText = text + " ✅";
setTimeout(() => {
copy.innerText = text;
}, 3000);
};
addEventListener("DOMContentLoaded", () => {
const once = { title: false, desc: false };
const title = document.getElementById("title");
const desc = document.getElementById("desc");
const start = document.getElementById("start");
const end = document.getElementById("end");
const offsetBy = (start, hour) => {
const origin = new Date(start);
// https://stackoverflow.com/a/66558369/15287885
const offset = origin.getTime() -
(origin.getTimezoneOffset() + (60 * hour * -1)) * 60 * 1000;
const fmt = new Date(offset).toISOString().slice(0, 16);
return fmt;
};
const updateCurl = () => {
const form = document.querySelector("form");
const data = new FormData(form);
const fmt = new URLSearchParams(data).toString();
const curl = document.getElementById("curl");
curl.innerHTML = `curl -X POST ${window.location.origin}/event \\<br>
-H "Content-Type: application/x-www-form-urlencoded" \\<br>
-d "${fmt}"`;
};
document.querySelectorAll("input[type=text]").forEach((input) => {
input.addEventListener("keyup", updateCurl);
});
document.querySelectorAll("input, select").forEach((input) => {
input.addEventListener("change", updateCurl);
});
start.value = offsetBy(new Date(), 0);
end.value = offsetBy(new Date(), 1);
title.addEventListener("focus", () => {
if (!once.title) {
once.title = true;
title.value = "";
}
});
title.addEventListener("blur", () => {
if (title.value === "") {
title.value = "Meetup";
once.title = false;
}
});
desc.addEventListener("focus", () => {
if (!once.desc) {
once.desc = true;
desc.value = "";
}
});
desc.addEventListener("blur", () => {
if (desc.value === "") {
desc.value = "At the park";
once.desc = false;
}
});
start.addEventListener("change", () => {
const offset = offsetBy(start.value, 1);
end.value = offset;
updateCurl();
});
end.addEventListener("change", () => {
const s = new Date(start.value);
const e = new Date(end.value);
if (!start.value || s > e) start.value = offsetBy(end.value, -1);
updateCurl();
});
updateCurl();
});