Hopefully Eightpieces is less buggish now
[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")
b0a0468a
BA
13 a#discordLink(href="https://discord.gg/a9ZFKBe")
14 span {{ st.tr["Discord invitation"] }}
15 img(src="/images/icons/discord.svg")
725da57f
BA
16 fieldset
17 label(for="userEmail") {{ st.tr["Email"] }}
4404e58c 18 input#userEmail(type="email" :value="st.user.email")
725da57f
BA
19 fieldset
20 label(for="mailSubject") {{ st.tr["Subject"] }}
21 input#mailSubject(type="text")
22 fieldset
23 textarea#mailContent(:placeholder="st.tr['Your message']")
9a3049f3
BA
24 button(@click="trySendMessage()") {{ st.tr["Send"] }}
25 #dialog.text-center {{ st.tr[infoMsg] }}
98db2082
BA
26</template>
27
28<script>
e57c4de4 29import { ajax } from "@/utils/ajax";
c66a829b 30import { store } from "@/store";
603b8a8b 31import { checkNameEmail } from "@/data/userCheck";
42a92848 32import { processModalClick } from "@/utils/modalClick.js";
98db2082 33export default {
c66a829b
BA
34 name: "my-contact-form",
35 data: function() {
36 return {
9a3049f3 37 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
c66a829b 38 st: store.state,
6808d7a1 39 infoMsg: ""
c66a829b
BA
40 };
41 },
42a92848
BA
42 mounted: function() {
43 document.getElementById("contactDiv")
44 .addEventListener("click", processModalClick);
45 },
ccd4a2b7 46 methods: {
9a3049f3 47 trySetEnterTime: function(event) {
6808d7a1 48 if (event.target.checked) {
9a3049f3
BA
49 this.enterTime = Date.now();
50 this.infoMsg = "";
51 }
52 },
98db2082 53 trySendMessage: function() {
9a3049f3
BA
54 // Basic anti-bot strategy:
55 const exitTime = Date.now();
6808d7a1 56 if (exitTime - this.enterTime < 5000) return;
98db2082
BA
57 let email = document.getElementById("userEmail");
58 let subject = document.getElementById("mailSubject");
59 let content = document.getElementById("mailContent");
6808d7a1
BA
60 let error = checkNameEmail({ email: email });
61 if (!error && content.value.trim().length == 0)
62 error = this.st.tr["Empty message"];
63 if (error) {
64 alert(error);
65 return;
66 }
67 if (
68 subject.value.trim().length == 0 &&
69 !confirm(this.st.tr["No subject. Send anyway?"])
70 )
98db2082 71 return;
98db2082
BA
72 // Message sending:
73 ajax(
74 "/messages",
75 "POST",
76 {
e57c4de4
BA
77 nocredentials: true,
78 data: {
79 email: email.value,
80 subject: subject.value,
81 content: content.value
82 },
83 success: () => {
84 this.infoMsg = "Email sent!";
85 subject.value = "";
86 content.value = "";
87 }
98db2082
BA
88 }
89 );
6808d7a1
BA
90 }
91 }
98db2082
BA
92};
93</script>
4f887105
BA
94
95<style lang="sass" scoped>
9a3049f3
BA
96[type="checkbox"].modal+div .card
97 max-width: 767px
98 max-height: 100%
910d631b 99
9a3049f3
BA
100textarea#mailContent
101 width: 100%
102 min-height: 100px
910d631b 103
b0a0468a
BA
104#discordLink
105 display: block
106 margin-top: 10px
107 text-align: center
108 font-size: 1.3em
109 & > img
110 height: 1.5em
111 display: inline-block
112 margin-left: 5px
113
9a3049f3
BA
114#dialog
115 padding: 5px
dcd68c41 116 color: blue
4f887105 117</style>