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