Add Diamond Chess
[vchess.git] / client / src / components / UpsertUser.vue
1 <template lang="pug">
2 div
3 input#modalUser.modal(
4 type="checkbox"
5 @change="trySetEnterTime($event)"
6 )
7 div#upsertDiv(
8 role="dialog"
9 data-checkbox="modalUser"
10 )
11 .card
12 label.modal-close(for="modalUser")
13 h3.section
14 span.title {{ st.tr[stage] }}
15 | (
16 span.link(
17 v-if="stage!='Update'"
18 @click="toggleStage()"
19 )
20 | {{ st.tr[stage=="Login" ? "Register" : "Login"] }}
21 span.link(
22 v-else
23 @click="doLogout()"
24 )
25 | {{ st.tr["Logout"] }}
26 img(src="/images/icons/rightArrow.svg")
27 | )
28 div(@keyup.enter="onSubmit()")
29 div(v-show="stage!='Login'")
30 fieldset
31 label(for="u_username") {{ st.tr["User name"] }}
32 input#u_username(
33 type="text"
34 v-model="user.name"
35 )
36 fieldset
37 label(for="u_useremail") {{ st.tr["Email"] }}
38 input#u_useremail(
39 type="email"
40 v-model="user.email"
41 )
42 fieldset
43 label(for="notifyNew") {{ st.tr["Notifications by email"] }}
44 input#notifyNew(
45 type="checkbox"
46 v-model="user.notify"
47 )
48 div(v-show="stage=='Login'")
49 fieldset
50 label(for="nameOrEmail") {{ st.tr["Name or Email"] }}
51 input#nameOrEmail(
52 type="text"
53 v-model="nameOrEmail"
54 )
55 button#submitBtn(@click="onSubmit()")
56 | {{ st.tr[submitMessage] }}
57 #dialog.text-center {{ st.tr[infoMsg] }}
58 </template>
59
60 <script>
61 import { store } from "@/store";
62 import { checkNameEmail } from "@/data/userCheck";
63 import { ajax } from "@/utils/ajax";
64 import { processModalClick } from "@/utils/modalClick.js";
65 export default {
66 name: "my-upsert-user",
67 data: function() {
68 return {
69 nameOrEmail: "", //for login
70 logStage: "Login", //or Register
71 infoMsg: "",
72 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
73 st: store.state,
74 user: {}
75 };
76 },
77 mounted: function() {
78 document.getElementById("upsertDiv")
79 .addEventListener("click", processModalClick);
80 },
81 watch: {
82 nameOrEmail: function(newValue) {
83 if (newValue.indexOf("@") >= 0) {
84 this.user.email = newValue;
85 this.user.name = "";
86 } else {
87 this.user.name = newValue;
88 this.user.email = "";
89 }
90 }
91 },
92 computed: {
93 submitMessage: function() {
94 switch (this.stage) {
95 case "Login":
96 return "Go";
97 case "Register":
98 return "Send";
99 case "Update":
100 return "Apply";
101 }
102 return "Never reached";
103 },
104 stage: function() {
105 return this.st.user.id > 0 ? "Update" : this.logStage;
106 }
107 },
108 methods: {
109 trySetEnterTime: function(event) {
110 if (event.target.checked) {
111 this.infoMsg = "";
112 this.enterTime = Date.now();
113 document.getElementById("u_username").focus();
114 this.user = {
115 name: this.st.user.name,
116 email: this.st.user.email,
117 notify: this.st.user.notify
118 };
119 }
120 },
121 toggleStage: function() {
122 // Loop login <--> register (update is for logged-in users)
123 this.logStage = this.logStage == "Login" ? "Register" : "Login";
124 },
125 ajaxUrl: function() {
126 switch (this.stage) {
127 case "Login":
128 return "/sendtoken";
129 case "Register":
130 return "/register";
131 case "Update":
132 return "/update";
133 }
134 return "Never reached";
135 },
136 ajaxMethod: function() {
137 switch (this.stage) {
138 case "Login":
139 return "GET";
140 case "Register":
141 return "POST";
142 case "Update":
143 return "PUT";
144 }
145 return "Never reached";
146 },
147 infoMessage: function() {
148 switch (this.stage) {
149 case "Login":
150 return "Connection token sent. Check your emails!";
151 case "Register":
152 return "Registration complete! Please check your emails now";
153 case "Update":
154 return "Modifications applied!";
155 }
156 return "Never reached";
157 },
158 onSubmit: function() {
159 // Basic anti-bot strategy:
160 const exitTime = Date.now();
161 if (this.stage == "Register" && exitTime - this.enterTime < 5000) return;
162 let error = undefined;
163 if (this.stage == "Login") {
164 const type = this.nameOrEmail.indexOf("@") >= 0 ? "email" : "name";
165 error = checkNameEmail({ [type]: this.nameOrEmail });
166 } else error = checkNameEmail(this.user);
167 if (error) {
168 alert(this.st.tr[error]);
169 return;
170 }
171 this.infoMsg = "Processing... Please wait";
172 ajax(
173 this.ajaxUrl(),
174 this.ajaxMethod(),
175 {
176 credentials: true,
177 data: (
178 this.stage == "Login"
179 ? { nameOrEmail: this.nameOrEmail }
180 : this.user
181 ),
182 success: () => {
183 this.infoMsg = this.infoMessage();
184 if (this.stage != "Update") this.nameOrEmail = "";
185 else {
186 this.st.user.name = this.user.name;
187 this.st.user.email = this.user.email;
188 this.st.user.notify = this.user.notify;
189 }
190 },
191 error: (err) => {
192 this.infoMsg = "";
193 alert(err);
194 }
195 }
196 );
197 },
198 doLogout: function() {
199 document.getElementById("modalUser").checked = false;
200 this.$router.push("/logout");
201 }
202 }
203 };
204 </script>
205
206 <style lang="sass" scoped>
207 [type="checkbox"].modal+div .card
208 max-width: 450px
209 max-height: 100%
210
211 h3.section
212 span.title
213 padding-right: 10px
214 span.link
215 color: darkred
216 font-size: 0.8em
217 text-decoration: underline
218 cursor: pointer
219 img
220 height: 1em
221 padding-left: 5px
222
223 #submitBtn
224 width: 50%
225 margin: 0 auto
226
227 #dialog
228 padding: 5px
229 color: blue
230 </style>