Fix bug when updating a move in game.vue
[vchess.git] / client / src / components / UpsertUser.vue
1 <template lang="pug">
2 div
3 input#modalUser.modal(type="checkbox" @change="trySetEnterTime")
4 div(role="dialog")
5 .card
6 label.modal-close(for="modalUser")
7 h3 {{ stage }}
8 form#userForm(@submit.prevent="onSubmit()")
9 div(v-show="stage!='Login'")
10 fieldset
11 label(for="username") Name
12 input#username(type="text" v-model="user.name")
13 fieldset
14 label(for="useremail") Email
15 input#useremail(type="email" v-model="user.email")
16 fieldset
17 label(for="notifyNew") Notify new moves &amp; games
18 input#notifyNew(type="checkbox" v-model="user.notify")
19 div(v-show="stage=='Login'")
20 fieldset
21 label(for="nameOrEmail") Name or Email
22 input#nameOrEmail(type="text" v-model="nameOrEmail")
23 .button-group
24 button#submit(type="button" @click="onSubmit()")
25 span {{ submitMessage }}
26 i.material-icons send
27 button(v-if="stage!='Update'" @click="toggleStage()")
28 span {{ stage=="Login" ? "Register" : "Login" }}
29 button(v-else @click="doLogout()")
30 span Logout
31 #dialog(:style="{display: displayInfo}") {{ infoMsg }}
32 </template>
33
34 <script>
35 import { store } from "@/store";
36 import { checkNameEmail } from "@/data/userCheck";
37 import { ajax } from "@/utils/ajax";
38 export default {
39 name: 'my-upsert-user',
40 data: function() {
41 return {
42 user: store.state.user,
43 nameOrEmail: "", //for login
44 logStage: "Login", //or Register
45 infoMsg: "",
46 enterTime: Number.MAX_SAFE_INTEGER, //for a basic anti-bot strategy
47 };
48 },
49 computed: {
50 submitMessage: function() {
51 switch (this.stage)
52 {
53 case "Login":
54 return "Go";
55 case "Register":
56 return "Send";
57 case "Update":
58 return "Apply";
59 }
60 },
61 displayInfo: function() {
62 return (this.infoMsg.length > 0 ? "block" : "none");
63 },
64 stage: function() {
65 return this.user.id > 0 ? "Update" : this.logStage;
66 },
67 },
68 methods: {
69 trySetEnterTime: function(event) {
70 if (!!event.target.checked)
71 this.enterTime = Date.now();
72 },
73 toggleStage: function() {
74 // Loop login <--> register (update is for logged-in users)
75 this.logStage = (this.logStage == "Login" ? "Register" : "Login");
76 },
77 ajaxUrl: function() {
78 switch (this.stage)
79 {
80 case "Login":
81 return "/sendtoken";
82 case "Register":
83 return "/register";
84 case "Update":
85 return "/update";
86 }
87 },
88 ajaxMethod: function() {
89 switch (this.stage)
90 {
91 case "Login":
92 return "GET";
93 case "Register":
94 return "POST";
95 case "Update":
96 return "PUT";
97 }
98 },
99 infoMessage: function() {
100 switch (this.stage)
101 {
102 case "Login":
103 return "Connection token sent. Check your emails!";
104 case "Register":
105 return "Registration complete! Please check your emails.";
106 case "Update":
107 return "Modifications applied!";
108 }
109 },
110 onSubmit: function() {
111 // Basic anti-bot strategy:
112 const exitTime = Date.now();
113 if (this.stage == "Register" && exitTime - this.enterTime < 5000)
114 return; //silently return, in (curious) case of it was legitimate
115 let error = undefined;
116 if (this.stage == 'Login')
117 {
118 const type = (this.nameOrEmail.indexOf('@') >= 0 ? "email" : "name");
119 error = checkNameEmail({[type]: this.nameOrEmail});
120 }
121 else
122 error = checkNameEmail(this.user);
123 if (!!error)
124 return alert(error);
125 this.infoMsg = "Processing... Please wait";
126 ajax(this.ajaxUrl(), this.ajaxMethod(),
127 this.stage == "Login" ? { nameOrEmail: this.nameOrEmail } : this.user,
128 res => {
129 this.infoMsg = this.infoMessage();
130 if (this.stage != "Update")
131 this.nameOrEmail = "";
132 setTimeout(() => {
133 this.infoMsg = "";
134 document.getElementById("modalUser").checked = false;
135 }, 2000);
136 },
137 err => {
138 this.infoMsg = "";
139 alert(err);
140 }
141 );
142 },
143 doLogout: function() {
144 ajax(
145 "/logout",
146 "GET",
147 () => {
148 this.user.id = 0;
149 this.user.name = "";
150 this.user.email = "";
151 this.user.notify = false;
152 delete localStorage["myid"];
153 delete localStorage["myname"];
154 }
155 );
156 },
157 },
158 };
159 </script>