Implemented multi-move possibility in a moves list => better support for multi-moves...
[vchess.git] / client / src / components / UpsertUser.vue
CommitLineData
c66a829b
BA
1<template lang="pug">
2div
910d631b
BA
3 input#modalUser.modal(
4 type="checkbox"
5 @change="trySetEnterTime($event)"
6 )
7 div(
8 role="dialog"
9 data-checkbox="modalUser"
10 )
c66a829b
BA
11 .card
12 label.modal-close(for="modalUser")
9a3049f3 13 h3.section {{ st.tr[stage] }}
725da57f 14 div(@keyup.enter="onSubmit()")
c66a829b
BA
15 div(v-show="stage!='Login'")
16 fieldset
2f258c37 17 label(for="username") {{ st.tr["User name"] }}
910d631b
BA
18 input#username(
19 type="text"
20 v-model="st.user.name"
21 )
c66a829b 22 fieldset
602d6bef 23 label(for="useremail") {{ st.tr["Email"] }}
910d631b
BA
24 input#useremail(
25 type="email"
26 v-model="st.user.email"
27 )
c66a829b 28 fieldset
602d6bef 29 label(for="notifyNew") {{ st.tr["Notifications by email"] }}
910d631b
BA
30 input#notifyNew(
31 type="checkbox"
32 v-model="st.user.notify"
33 )
c66a829b
BA
34 div(v-show="stage=='Login'")
35 fieldset
5fe7e71c 36 label(for="nameOrEmail") {{ st.tr["Name or Email"] }}
910d631b
BA
37 input#nameOrEmail(
38 type="text"
39 v-model="nameOrEmail"
40 )
c66a829b 41 .button-group
9a3049f3 42 button(@click="onSubmit()")
602d6bef 43 span {{ st.tr[submitMessage] }}
910d631b
BA
44 button(
45 v-if="stage!='Update'"
46 type="button"
47 @click="toggleStage()"
48 )
602d6bef 49 span {{ st.tr[stage=="Login" ? "Register" : "Login"] }}
910d631b
BA
50 button(
51 v-else type="button"
52 @click="doLogout()"
53 )
602d6bef 54 span {{ st.tr["Logout"] }}
9a3049f3 55 #dialog.text-center {{ st.tr[infoMsg] }}
c66a829b
BA
56</template>
57
58<script>
59import { store } from "@/store";
f05815d7
BA
60import { checkNameEmail } from "@/data/userCheck";
61import { ajax } from "@/utils/ajax";
c66a829b 62export default {
6808d7a1 63 name: "my-upsert-user",
f05815d7
BA
64 data: function() {
65 return {
f05815d7 66 nameOrEmail: "", //for login
deca03e8 67 logStage: "Login", //or Register
f05815d7
BA
68 infoMsg: "",
69 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
6808d7a1 70 st: store.state
f05815d7
BA
71 };
72 },
dac39588
BA
73 watch: {
74 nameOrEmail: function(newValue) {
6808d7a1 75 if (newValue.indexOf("@") >= 0) {
a3ac374b
BA
76 this.st.user.email = newValue;
77 this.st.user.name = "";
6808d7a1 78 } else {
a3ac374b
BA
79 this.st.user.name = newValue;
80 this.st.user.email = "";
dac39588 81 }
6808d7a1 82 }
dac39588 83 },
f05815d7
BA
84 computed: {
85 submitMessage: function() {
6808d7a1 86 switch (this.stage) {
f05815d7
BA
87 case "Login":
88 return "Go";
89 case "Register":
90 return "Send";
91 case "Update":
92 return "Apply";
93 }
6808d7a1 94 return "Never reached";
f05815d7 95 },
deca03e8 96 stage: function() {
a3ac374b 97 return this.st.user.id > 0 ? "Update" : this.logStage;
6808d7a1 98 }
f05815d7
BA
99 },
100 methods: {
101 trySetEnterTime: function(event) {
6808d7a1 102 if (event.target.checked) {
9a3049f3 103 this.infoMsg = "";
f05815d7 104 this.enterTime = Date.now();
9a3049f3 105 }
f05815d7
BA
106 },
107 toggleStage: function() {
108 // Loop login <--> register (update is for logged-in users)
6808d7a1 109 this.logStage = this.logStage == "Login" ? "Register" : "Login";
f05815d7
BA
110 },
111 ajaxUrl: function() {
6808d7a1 112 switch (this.stage) {
f05815d7
BA
113 case "Login":
114 return "/sendtoken";
115 case "Register":
116 return "/register";
117 case "Update":
118 return "/update";
119 }
6808d7a1 120 return "Never reached";
f05815d7
BA
121 },
122 ajaxMethod: function() {
6808d7a1 123 switch (this.stage) {
f05815d7
BA
124 case "Login":
125 return "GET";
126 case "Register":
127 return "POST";
128 case "Update":
129 return "PUT";
130 }
6808d7a1 131 return "Never reached";
f05815d7
BA
132 },
133 infoMessage: function() {
6808d7a1 134 switch (this.stage) {
f05815d7
BA
135 case "Login":
136 return "Connection token sent. Check your emails!";
137 case "Register":
77c50966 138 return "Registration complete! Please check your emails";
f05815d7
BA
139 case "Update":
140 return "Modifications applied!";
141 }
6808d7a1 142 return "Never reached";
f05815d7
BA
143 },
144 onSubmit: function() {
145 // Basic anti-bot strategy:
146 const exitTime = Date.now();
6808d7a1 147 if (this.stage == "Register" && exitTime - this.enterTime < 5000) return;
f05815d7 148 let error = undefined;
6808d7a1
BA
149 if (this.stage == "Login") {
150 const type = this.nameOrEmail.indexOf("@") >= 0 ? "email" : "name";
151 error = checkNameEmail({ [type]: this.nameOrEmail });
152 } else error = checkNameEmail(this.st.user);
153 if (error) {
866842c3 154 alert(this.st.tr[error]);
6808d7a1 155 return;
f05815d7 156 }
f05815d7 157 this.infoMsg = "Processing... Please wait";
6808d7a1
BA
158 ajax(
159 this.ajaxUrl(),
160 this.ajaxMethod(),
161 this.stage == "Login"
162 ? { nameOrEmail: this.nameOrEmail }
163 : this.st.user,
164 () => {
f05815d7 165 this.infoMsg = this.infoMessage();
6808d7a1 166 if (this.stage != "Update") this.nameOrEmail = "";
f05815d7
BA
167 },
168 err => {
169 this.infoMsg = "";
170 alert(err);
171 }
172 );
173 },
deca03e8 174 doLogout: function() {
a3ac374b
BA
175 document.getElementById("modalUser").checked = false;
176 this.$router.push("/logout");
6808d7a1
BA
177 }
178 }
c66a829b
BA
179};
180</script>
9a3049f3
BA
181
182<style lang="sass" scoped>
183[type="checkbox"].modal+div .card
e71161fb 184 max-width: 450px
9a3049f3 185 max-height: 100%
910d631b 186
9a3049f3
BA
187#dialog
188 padding: 5px
189 color: blue
190</style>