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