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