Experimental news notification system + fix Eightpieces variant
[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 )
42a92848 7 div#contactDiv(
910d631b
BA
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"] }}
4404e58c 15 input#userEmail(type="email" :value="st.user.email")
725da57f
BA
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>
e57c4de4 26import { ajax } from "@/utils/ajax";
c66a829b 27import { store } from "@/store";
603b8a8b 28import { checkNameEmail } from "@/data/userCheck";
42a92848 29import { processModalClick } from "@/utils/modalClick.js";
98db2082 30export default {
c66a829b
BA
31 name: "my-contact-form",
32 data: function() {
33 return {
9a3049f3 34 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
c66a829b 35 st: store.state,
6808d7a1 36 infoMsg: ""
c66a829b
BA
37 };
38 },
42a92848
BA
39 mounted: function() {
40 document.getElementById("contactDiv")
41 .addEventListener("click", processModalClick);
42 },
ccd4a2b7 43 methods: {
9a3049f3 44 trySetEnterTime: function(event) {
6808d7a1 45 if (event.target.checked) {
9a3049f3
BA
46 this.enterTime = Date.now();
47 this.infoMsg = "";
48 }
49 },
98db2082 50 trySendMessage: function() {
9a3049f3
BA
51 // Basic anti-bot strategy:
52 const exitTime = Date.now();
6808d7a1 53 if (exitTime - this.enterTime < 5000) return;
98db2082
BA
54 let email = document.getElementById("userEmail");
55 let subject = document.getElementById("mailSubject");
56 let content = document.getElementById("mailContent");
6808d7a1
BA
57 let error = checkNameEmail({ email: email });
58 if (!error && content.value.trim().length == 0)
59 error = this.st.tr["Empty message"];
60 if (error) {
61 alert(error);
62 return;
63 }
64 if (
65 subject.value.trim().length == 0 &&
66 !confirm(this.st.tr["No subject. Send anyway?"])
67 )
98db2082 68 return;
98db2082
BA
69 // Message sending:
70 ajax(
71 "/messages",
72 "POST",
73 {
e57c4de4
BA
74 nocredentials: true,
75 data: {
76 email: email.value,
77 subject: subject.value,
78 content: content.value
79 },
80 success: () => {
81 this.infoMsg = "Email sent!";
82 subject.value = "";
83 content.value = "";
84 }
98db2082
BA
85 }
86 );
6808d7a1
BA
87 }
88 }
98db2082
BA
89};
90</script>
4f887105
BA
91
92<style lang="sass" scoped>
9a3049f3
BA
93[type="checkbox"].modal+div .card
94 max-width: 767px
95 max-height: 100%
910d631b 96
9a3049f3
BA
97textarea#mailContent
98 width: 100%
99 min-height: 100px
910d631b 100
9a3049f3
BA
101#dialog
102 padding: 5px
dcd68c41 103 color: blue
4f887105 104</style>