Fixes, improvements
[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
2f258c37 21 label(for="nameOrEmail") {{ st.tr["User 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
BA
37export default {
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
602d6bef 45 st: store.state,
f05815d7
BA
46 };
47 },
dac39588
BA
48 watch: {
49 nameOrEmail: function(newValue) {
50 if (newValue.indexOf('@') >= 0)
51 {
a3ac374b
BA
52 this.st.user.email = newValue;
53 this.st.user.name = "";
dac39588
BA
54 }
55 else
56 {
a3ac374b
BA
57 this.st.user.name = newValue;
58 this.st.user.email = "";
dac39588
BA
59 }
60 },
61 },
f05815d7
BA
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 },
deca03e8 74 stage: function() {
a3ac374b 75 return this.st.user.id > 0 ? "Update" : this.logStage;
deca03e8 76 },
f05815d7
BA
77 },
78 methods: {
79 trySetEnterTime: function(event) {
80 if (!!event.target.checked)
9a3049f3
BA
81 {
82 this.infoMsg = "";
f05815d7 83 this.enterTime = Date.now();
9a3049f3 84 }
f05815d7
BA
85 },
86 toggleStage: function() {
87 // Loop login <--> register (update is for logged-in users)
deca03e8 88 this.logStage = (this.logStage == "Login" ? "Register" : "Login");
f05815d7
BA
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":
77c50966 118 return "Registration complete! Please check your emails";
f05815d7
BA
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)
9a3049f3 127 return;
f05815d7
BA
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
a3ac374b 135 error = checkNameEmail(this.st.user);
f05815d7
BA
136 if (!!error)
137 return alert(error);
138 this.infoMsg = "Processing... Please wait";
139 ajax(this.ajaxUrl(), this.ajaxMethod(),
a3ac374b 140 this.stage == "Login" ? { nameOrEmail: this.nameOrEmail } : this.st.user,
f05815d7 141 res => {
f05815d7
BA
142 this.infoMsg = this.infoMessage();
143 if (this.stage != "Update")
f05815d7 144 this.nameOrEmail = "";
f05815d7
BA
145 },
146 err => {
147 this.infoMsg = "";
148 alert(err);
149 }
150 );
151 },
deca03e8 152 doLogout: function() {
a3ac374b
BA
153 document.getElementById("modalUser").checked = false;
154 this.$router.push("/logout");
deca03e8 155 },
f05815d7 156 },
c66a829b
BA
157};
158</script>
9a3049f3
BA
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>