Started code review + some fixes (unfinished)
[vchess.git] / client / src / components / ContactForm.vue
CommitLineData
98db2082
BA
1<template lang="pug">
2div
9a3049f3
BA
3 input#modalContact.modal(type="checkbox" @change="trySetEnterTime($event)")
4 div(role="dialog" data-checkbox="modalContact")
5 .card
98db2082 6 label.modal-close(for="modalContact")
9a3049f3
BA
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
89021f18 15 textarea#mailContent(:placeholder="st.tr['Your message']")
9a3049f3
BA
16 button(@click="trySendMessage()") {{ st.tr["Send"] }}
17 #dialog.text-center {{ st.tr[infoMsg] }}
98db2082
BA
18</template>
19
20<script>
21import { ajax } from "../utils/ajax";
c66a829b 22import { store } from "@/store";
603b8a8b 23import { checkNameEmail } from "@/data/userCheck";
98db2082 24export default {
c66a829b
BA
25 name: "my-contact-form",
26 data: function() {
27 return {
9a3049f3 28 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
c66a829b 29 st: store.state,
6808d7a1 30 infoMsg: ""
c66a829b
BA
31 };
32 },
ccd4a2b7 33 methods: {
9a3049f3 34 trySetEnterTime: function(event) {
6808d7a1 35 if (event.target.checked) {
9a3049f3
BA
36 this.enterTime = Date.now();
37 this.infoMsg = "";
38 }
39 },
98db2082 40 trySendMessage: function() {
9a3049f3
BA
41 // Basic anti-bot strategy:
42 const exitTime = Date.now();
6808d7a1 43 if (exitTime - this.enterTime < 5000) return;
98db2082
BA
44 let email = document.getElementById("userEmail");
45 let subject = document.getElementById("mailSubject");
46 let content = document.getElementById("mailContent");
6808d7a1
BA
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 )
98db2082
BA
58 return;
59
60 // Message sending:
61 ajax(
62 "/messages",
63 "POST",
64 {
65 email: email.value,
66 subject: subject.value,
6808d7a1 67 content: content.value
98db2082
BA
68 },
69 () => {
9a3049f3 70 this.infoMsg = "Email sent!";
98db2082
BA
71 subject.value = "";
72 content.value = "";
98db2082
BA
73 }
74 );
6808d7a1
BA
75 }
76 }
98db2082
BA
77};
78</script>
4f887105
BA
79
80<style lang="sass" scoped>
9a3049f3
BA
81[type="checkbox"].modal+div .card
82 max-width: 767px
83 max-height: 100%
84textarea#mailContent
85 width: 100%
86 min-height: 100px
87#dialog
88 padding: 5px
dcd68c41 89 color: blue
4f887105 90</style>