Translations
[vchess.git] / client / src / components / UpsertUser.vue
CommitLineData
c66a829b
BA
1<template lang="pug">
2div
3 input#modalUser.modal(type="checkbox" @change="trySetEnterTime")
dcd68c41 4 div(role="dialog" data-checkbox="modalUser")
c66a829b
BA
5 .card
6 label.modal-close(for="modalUser")
f44fd3bf 7 h3 {{ st.tr[stage] }}
3837d4f7 8 form#userForm(@submit.prevent="onSubmit()" @keyup.enter="onSubmit")
c66a829b
BA
9 div(v-show="stage!='Login'")
10 fieldset
602d6bef 11 label(for="username") {{ st.tr["Name"] }}
c66a829b
BA
12 input#username(type="text" v-model="user.name")
13 fieldset
602d6bef 14 label(for="useremail") {{ st.tr["Email"] }}
1aeed627 15 input#useremail(type="email" v-model="user.email")
c66a829b 16 fieldset
602d6bef 17 label(for="notifyNew") {{ st.tr["Notifications by email"] }}
1aeed627 18 input#notifyNew(type="checkbox" v-model="user.notify")
c66a829b
BA
19 div(v-show="stage=='Login'")
20 fieldset
602d6bef 21 label(for="nameOrEmail") {{ st.tr["Name or Email"] }}
1aeed627 22 input#nameOrEmail(type="text" v-model="nameOrEmail")
c66a829b 23 .button-group
1aeed627 24 button#submit(type="button" @click="onSubmit()")
602d6bef 25 span {{ st.tr[submitMessage] }}
c66a829b 26 button(v-if="stage!='Update'" @click="toggleStage()")
602d6bef 27 span {{ st.tr[stage=="Login" ? "Register" : "Login"] }}
5ea8d113 28 button#logoutBtn(v-else @click="doLogout()")
602d6bef
BA
29 span {{ st.tr["Logout"] }}
30 #dialog(:style="{display: displayInfo}") {{ 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 {
deca03e8 41 user: store.state.user,
f05815d7 42 nameOrEmail: "", //for login
deca03e8 43 logStage: "Login", //or Register
f05815d7
BA
44 infoMsg: "",
45 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
602d6bef 46 st: store.state,
f05815d7
BA
47 };
48 },
dac39588
BA
49 watch: {
50 nameOrEmail: function(newValue) {
51 if (newValue.indexOf('@') >= 0)
52 {
53 this.user.email = newValue;
54 this.user.name = "";
55 }
56 else
57 {
58 this.user.name = newValue;
59 this.user.email = "";
60 }
61 },
62 },
f05815d7
BA
63 computed: {
64 submitMessage: function() {
65 switch (this.stage)
66 {
67 case "Login":
68 return "Go";
69 case "Register":
70 return "Send";
71 case "Update":
72 return "Apply";
73 }
74 },
75 displayInfo: function() {
76 return (this.infoMsg.length > 0 ? "block" : "none");
77 },
deca03e8
BA
78 stage: function() {
79 return this.user.id > 0 ? "Update" : this.logStage;
80 },
f05815d7
BA
81 },
82 methods: {
83 trySetEnterTime: function(event) {
84 if (!!event.target.checked)
85 this.enterTime = Date.now();
86 },
87 toggleStage: function() {
88 // Loop login <--> register (update is for logged-in users)
deca03e8 89 this.logStage = (this.logStage == "Login" ? "Register" : "Login");
f05815d7
BA
90 },
91 ajaxUrl: function() {
92 switch (this.stage)
93 {
94 case "Login":
95 return "/sendtoken";
96 case "Register":
97 return "/register";
98 case "Update":
99 return "/update";
100 }
101 },
102 ajaxMethod: function() {
103 switch (this.stage)
104 {
105 case "Login":
106 return "GET";
107 case "Register":
108 return "POST";
109 case "Update":
110 return "PUT";
111 }
112 },
113 infoMessage: function() {
114 switch (this.stage)
115 {
116 case "Login":
117 return "Connection token sent. Check your emails!";
118 case "Register":
77c50966 119 return "Registration complete! Please check your emails";
f05815d7
BA
120 case "Update":
121 return "Modifications applied!";
122 }
123 },
124 onSubmit: function() {
125 // Basic anti-bot strategy:
126 const exitTime = Date.now();
127 if (this.stage == "Register" && exitTime - this.enterTime < 5000)
128 return; //silently return, in (curious) case of it was legitimate
129 let error = undefined;
130 if (this.stage == 'Login')
131 {
132 const type = (this.nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
133 error = checkNameEmail({[type]: this.nameOrEmail});
134 }
135 else
136 error = checkNameEmail(this.user);
137 if (!!error)
138 return alert(error);
139 this.infoMsg = "Processing... Please wait";
140 ajax(this.ajaxUrl(), this.ajaxMethod(),
141 this.stage == "Login" ? { nameOrEmail: this.nameOrEmail } : this.user,
142 res => {
f05815d7
BA
143 this.infoMsg = this.infoMessage();
144 if (this.stage != "Update")
f05815d7 145 this.nameOrEmail = "";
f05815d7
BA
146 setTimeout(() => {
147 this.infoMsg = "";
f05815d7
BA
148 document.getElementById("modalUser").checked = false;
149 }, 2000);
150 },
151 err => {
152 this.infoMsg = "";
153 alert(err);
154 }
155 );
156 },
deca03e8 157 doLogout: function() {
5ea8d113
BA
158 let logoutBtn = document.getElementById("logoutBtn");
159 logoutBtn.disabled = true;
160 // NOTE: this local cleaning would logically happen when we're sure
161 // that token is erased. But in the case a user clear the cookies,
162 // it would lead to situations where he cannot ("locally") log out.
163 // At worst, if token deletion fails the user can erase cookie manually.
164 this.user.id = 0;
165 this.user.name = "";
166 this.user.email = "";
167 this.user.notify = false;
168 localStorage.removeItem("myid");
169 localStorage.removeItem("myname");
170 ajax("/logout", "GET", () => {
171 logoutBtn.disabled = false; //for symmetry, but not very useful...
172 document.getElementById("modalUser").checked = false;
173 // this.$router.push("/") will fail if logout from Hall, so:
174 document.location.reload(true);
175 });
deca03e8 176 },
f05815d7 177 },
c66a829b
BA
178};
179</script>