'update'
[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"] }}
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
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 {
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 },
74 displayInfo: function() {
75 return (this.infoMsg.length > 0 ? "block" : "none");
76 },
deca03e8 77 stage: function() {
a3ac374b 78 return this.st.user.id > 0 ? "Update" : this.logStage;
deca03e8 79 },
f05815d7
BA
80 },
81 methods: {
82 trySetEnterTime: function(event) {
83 if (!!event.target.checked)
84 this.enterTime = Date.now();
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)
127 return; //silently return, in (curious) case of it was legitimate
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 setTimeout(() => {
146 this.infoMsg = "";
f05815d7
BA
147 document.getElementById("modalUser").checked = false;
148 }, 2000);
149 },
150 err => {
151 this.infoMsg = "";
152 alert(err);
153 }
154 );
155 },
deca03e8 156 doLogout: function() {
a3ac374b
BA
157 document.getElementById("modalUser").checked = false;
158 this.$router.push("/logout");
deca03e8 159 },
f05815d7 160 },
c66a829b
BA
161};
162</script>