Started code review + some fixes (unfinished)
[vchess.git] / client / src / components / ContactForm.vue
1 <template lang="pug">
2 div
3 input#modalContact.modal(type="checkbox" @change="trySetEnterTime($event)")
4 div(role="dialog" data-checkbox="modalContact")
5 .card
6 label.modal-close(for="modalContact")
7 form(@submit.prevent="trySendMessage()" @keyup.enter="trySendMessage()")
8 fieldset
9 label(for="userEmail") {{ st.tr["Email"] }}
10 input#userEmail(type="email")
11 fieldset
12 label(for="mailSubject") {{ st.tr["Subject"] }}
13 input#mailSubject(type="text")
14 fieldset
15 textarea#mailContent(:placeholder="st.tr['Your message']")
16 button(@click="trySendMessage()") {{ st.tr["Send"] }}
17 #dialog.text-center {{ st.tr[infoMsg] }}
18 </template>
19
20 <script>
21 import { ajax } from "../utils/ajax";
22 import { store } from "@/store";
23 import { checkNameEmail } from "@/data/userCheck";
24 export default {
25 name: "my-contact-form",
26 data: function() {
27 return {
28 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
29 st: store.state,
30 infoMsg: ""
31 };
32 },
33 methods: {
34 trySetEnterTime: function(event) {
35 if (event.target.checked) {
36 this.enterTime = Date.now();
37 this.infoMsg = "";
38 }
39 },
40 trySendMessage: function() {
41 // Basic anti-bot strategy:
42 const exitTime = Date.now();
43 if (exitTime - this.enterTime < 5000) return;
44 let email = document.getElementById("userEmail");
45 let subject = document.getElementById("mailSubject");
46 let content = document.getElementById("mailContent");
47 let error = checkNameEmail({ email: email });
48 if (!error && content.value.trim().length == 0)
49 error = this.st.tr["Empty message"];
50 if (error) {
51 alert(error);
52 return;
53 }
54 if (
55 subject.value.trim().length == 0 &&
56 !confirm(this.st.tr["No subject. Send anyway?"])
57 )
58 return;
59
60 // Message sending:
61 ajax(
62 "/messages",
63 "POST",
64 {
65 email: email.value,
66 subject: subject.value,
67 content: content.value
68 },
69 () => {
70 this.infoMsg = "Email sent!";
71 subject.value = "";
72 content.value = "";
73 }
74 );
75 }
76 }
77 };
78 </script>
79
80 <style lang="sass" scoped>
81 [type="checkbox"].modal+div .card
82 max-width: 767px
83 max-height: 100%
84 textarea#mailContent
85 width: 100%
86 min-height: 100px
87 #dialog
88 padding: 5px
89 color: blue
90 </style>