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