|
12 | 12 |
|
13 | 13 | Authors: |
14 | 14 | * Pedro Crespo (pcrespov) |
| 15 | + * Odei Maiz (odeimaiz) |
15 | 16 |
|
16 | 17 | ************************************************************************ */ |
17 | 18 |
|
18 | 19 | /** |
19 | 20 | * User profile in preferences dialog |
20 | 21 | * |
21 | | - * - user name, surname, email, avatar |
| 22 | + * - first name, last name, username, email |
22 | 23 | * |
23 | 24 | */ |
24 | 25 |
|
@@ -47,6 +48,28 @@ qx.Class.define("osparc.desktop.account.ProfilePage", { |
47 | 48 | __userProfileData: null, |
48 | 49 | __userProfileModel: null, |
49 | 50 |
|
| 51 | + __fetchProfile: function() { |
| 52 | + osparc.data.Resources.getOne("profile", {}, null, false) |
| 53 | + .then(profile => { |
| 54 | + this.__setDataToModel(profile); |
| 55 | + }) |
| 56 | + .catch(err => { |
| 57 | + console.error(err); |
| 58 | + }); |
| 59 | + }, |
| 60 | + |
| 61 | + __setDataToModel: function(data) { |
| 62 | + if (data) { |
| 63 | + this.__userProfileData = data; |
| 64 | + this.__userProfileModel.set({ |
| 65 | + "firstName": data["first_name"] || "", |
| 66 | + "lastName": data["last_name"] || "", |
| 67 | + "email": data["login"], |
| 68 | + "expirationDate": data["expirationDate"] || null |
| 69 | + }); |
| 70 | + } |
| 71 | + }, |
| 72 | + |
50 | 73 | __createProfileUser: function() { |
51 | 74 | // layout |
52 | 75 | const box = osparc.ui.window.TabbedView.createSectionBox(this.tr("User")); |
@@ -140,55 +163,30 @@ qx.Class.define("osparc.desktop.account.ProfilePage", { |
140 | 163 | return; |
141 | 164 | } |
142 | 165 |
|
143 | | - const requests = { |
144 | | - email: null, |
145 | | - names: null |
146 | | - }; |
147 | | - if (this.__userProfileData["login"] !== model.getEmail()) { |
148 | | - if (emailValidator.validate()) { |
149 | | - const emailReq = new osparc.io.request.ApiRequest("/auth/change-email", "POST"); |
150 | | - emailReq.setRequestData({ |
151 | | - "email": model.getEmail() |
152 | | - }); |
153 | | - requests.email = emailReq; |
154 | | - } |
155 | | - } |
156 | | - |
157 | 166 | if (this.__userProfileData["first_name"] !== model.getFirstName() || this.__userProfileData["last_name"] !== model.getLastName()) { |
158 | 167 | if (namesValidator.validate()) { |
159 | | - const profileReq = new osparc.io.request.ApiRequest("/me", "PATCH"); |
160 | | - profileReq.setRequestData({ |
161 | | - "first_name": model.getFirstName(), |
162 | | - "last_name": model.getLastName() |
163 | | - }); |
164 | | - requests.names = profileReq; |
| 168 | + const params = { |
| 169 | + data: { |
| 170 | + "first_name": model.getFirstName(), |
| 171 | + "last_name": model.getLastName(), |
| 172 | + } |
| 173 | + }; |
| 174 | + osparc.data.Resources.fetch("profile", "patch", params) |
| 175 | + .then(() => { |
| 176 | + this.__setDataToModel(Object.assign(this.__userProfileData, params.data)); |
| 177 | + osparc.auth.Manager.getInstance().updateProfile(this.__userProfileData); |
| 178 | + const msg = this.tr("Profile updated"); |
| 179 | + osparc.FlashMessenger.getInstance().logAs(msg, "INFO"); |
| 180 | + }) |
| 181 | + .catch(err => { |
| 182 | + this.__resetDataToModel(); |
| 183 | + const msg = err.message || this.tr("Failed to update profile"); |
| 184 | + osparc.FlashMessenger.getInstance().logAs(msg, "ERROR"); |
| 185 | + console.error(err); |
| 186 | + }); |
165 | 187 | } |
166 | 188 | } |
167 | | - |
168 | | - Object.keys(requests).forEach(key => { |
169 | | - const req = requests[key]; |
170 | | - if (req === null) { |
171 | | - return; |
172 | | - } |
173 | | - |
174 | | - req.addListenerOnce("success", e => { |
175 | | - const reqData = e.getTarget().getRequestData(); |
176 | | - this.__setDataToModel(Object.assign(this.__userProfileData, reqData)); |
177 | | - osparc.auth.Manager.getInstance().updateProfile(this.__userProfileData); |
178 | | - const res = e.getTarget().getResponse(); |
179 | | - const msg = (res && res.data) ? res.data : this.tr("Profile updated"); |
180 | | - osparc.FlashMessenger.getInstance().logAs(msg, "INFO"); |
181 | | - }, this); |
182 | | - |
183 | | - req.addListenerOnce("fail", e => { |
184 | | - this.__resetDataToModel(); |
185 | | - const msg = osparc.data.Resources.getErrorMsg(e.getTarget().getResponse()) || this.tr("Failed to update profile"); |
186 | | - osparc.FlashMessenger.getInstance().logAs(msg, "ERROR"); |
187 | | - }, this); |
188 | | - |
189 | | - req.send(); |
190 | | - }); |
191 | | - }, this); |
| 189 | + }); |
192 | 190 |
|
193 | 191 | return box; |
194 | 192 | }, |
@@ -261,28 +259,6 @@ qx.Class.define("osparc.desktop.account.ProfilePage", { |
261 | 259 | return box; |
262 | 260 | }, |
263 | 261 |
|
264 | | - __fetchProfile: function() { |
265 | | - osparc.data.Resources.getOne("profile", {}, null, false) |
266 | | - .then(profile => { |
267 | | - this.__setDataToModel(profile); |
268 | | - }) |
269 | | - .catch(err => { |
270 | | - console.error(err); |
271 | | - }); |
272 | | - }, |
273 | | - |
274 | | - __setDataToModel: function(data) { |
275 | | - if (data) { |
276 | | - this.__userProfileData = data; |
277 | | - this.__userProfileModel.set({ |
278 | | - "firstName": data["first_name"] || "", |
279 | | - "lastName": data["last_name"] || "", |
280 | | - "email": data["login"], |
281 | | - "expirationDate": data["expirationDate"] || null |
282 | | - }); |
283 | | - } |
284 | | - }, |
285 | | - |
286 | 262 | __resetDataToModel: function() { |
287 | 263 | this.__setDataToModel(this.__userProfileData); |
288 | 264 | }, |
|
0 commit comments