X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fcomponents%2FUpsertUser.vue;h=2be637c678d0c52331779dcde795f4d40587ca32;hb=f0c68a04e31bb6a4b2f8b94a532ef3ca2eebbe3e;hp=f5a90443e0f001b9b9d1a51e8406e216307124f9;hpb=2f258c37c19c5be20ec68695ddfaec2c21f7f0ae;p=vchess.git diff --git a/client/src/components/UpsertUser.vue b/client/src/components/UpsertUser.vue index f5a90443..2be637c6 100644 --- a/client/src/components/UpsertUser.vue +++ b/client/src/components/UpsertUser.vue @@ -1,31 +1,56 @@ @@ -35,7 +60,7 @@ import { store } from "@/store"; import { checkNameEmail } from "@/data/userCheck"; import { ajax } from "@/utils/ajax"; export default { - name: 'my-upsert-user', + name: "my-upsert-user", data: function() { return { nameOrEmail: "", //for login @@ -43,26 +68,23 @@ export default { infoMsg: "", enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy st: store.state, + user: {} }; }, watch: { nameOrEmail: function(newValue) { - if (newValue.indexOf('@') >= 0) - { - this.st.user.email = newValue; - this.st.user.name = ""; + if (newValue.indexOf("@") >= 0) { + this.user.email = newValue; + this.user.name = ""; + } else { + this.user.name = newValue; + this.user.email = ""; } - else - { - this.st.user.name = newValue; - this.st.user.email = ""; - } - }, + } }, computed: { submitMessage: function() { - switch (this.stage) - { + switch (this.stage) { case "Login": return "Go"; case "Register": @@ -70,26 +92,31 @@ export default { case "Update": return "Apply"; } + return "Never reached"; }, stage: function() { return this.st.user.id > 0 ? "Update" : this.logStage; - }, + } }, methods: { trySetEnterTime: function(event) { - if (!!event.target.checked) - { + if (event.target.checked) { this.infoMsg = ""; this.enterTime = Date.now(); + document.getElementById("u_username").focus(); + this.user = { + name: this.st.user.name, + email: this.st.user.email, + notify: this.st.user.notify + }; } }, toggleStage: function() { // Loop login <--> register (update is for logged-in users) - this.logStage = (this.logStage == "Login" ? "Register" : "Login"); + this.logStage = this.logStage == "Login" ? "Register" : "Login"; }, ajaxUrl: function() { - switch (this.stage) - { + switch (this.stage) { case "Login": return "/sendtoken"; case "Register": @@ -97,10 +124,10 @@ export default { case "Update": return "/update"; } + return "Never reached"; }, ajaxMethod: function() { - switch (this.stage) - { + switch (this.stage) { case "Login": return "GET"; case "Register": @@ -108,40 +135,47 @@ export default { case "Update": return "PUT"; } + return "Never reached"; }, infoMessage: function() { - switch (this.stage) - { + switch (this.stage) { case "Login": return "Connection token sent. Check your emails!"; case "Register": - return "Registration complete! Please check your emails"; + return "Registration complete! Please check your emails now"; case "Update": return "Modifications applied!"; } + return "Never reached"; }, onSubmit: function() { // Basic anti-bot strategy: const exitTime = Date.now(); - if (this.stage == "Register" && exitTime - this.enterTime < 5000) - return; + if (this.stage == "Register" && exitTime - this.enterTime < 5000) return; let error = undefined; - if (this.stage == 'Login') - { - const type = (this.nameOrEmail.indexOf('@') >= 0 ? "email" : "name"); - error = checkNameEmail({[type]: this.nameOrEmail}); + if (this.stage == "Login") { + const type = this.nameOrEmail.indexOf("@") >= 0 ? "email" : "name"; + error = checkNameEmail({ [type]: this.nameOrEmail }); + } else error = checkNameEmail(this.user); + if (error) { + alert(this.st.tr[error]); + return; } - else - error = checkNameEmail(this.st.user); - if (!!error) - return alert(error); this.infoMsg = "Processing... Please wait"; - ajax(this.ajaxUrl(), this.ajaxMethod(), - this.stage == "Login" ? { nameOrEmail: this.nameOrEmail } : this.st.user, - res => { + ajax( + this.ajaxUrl(), + this.ajaxMethod(), + this.stage == "Login" + ? { nameOrEmail: this.nameOrEmail } + : this.user, + () => { this.infoMsg = this.infoMessage(); - if (this.stage != "Update") - this.nameOrEmail = ""; + if (this.stage != "Update") this.nameOrEmail = ""; + else { + this.st.user.name = this.user.name; + this.st.user.email = this.user.email; + this.st.user.notify = this.user.notify; + } }, err => { this.infoMsg = ""; @@ -152,15 +186,16 @@ export default { doLogout: function() { document.getElementById("modalUser").checked = false; this.$router.push("/logout"); - }, - }, + } + } };