Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
odeimaiz committed Dec 6, 2024
1 parent 9a15e0c commit 72a5d99
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,11 @@ qx.Class.define("osparc.data.Resources", {
getOne: {
method: "GET",
url: statics.API + "/me"
}
},
patch: {
method: "PATCH",
url: statics.API + "/me"
},
}
},
/*
Expand Down Expand Up @@ -1117,7 +1121,11 @@ qx.Class.define("osparc.data.Resources", {
postResetPassword: {
method: "POST",
url: statics.API + "/auth/reset-password/{code}"
}
},
changeEmail: {
method: "POST",
url: statics.API + "/auth/change-email"
},
}
},
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
Authors:
* Pedro Crespo (pcrespov)
* Odei Maiz (odeimaiz)
************************************************************************ */

/**
* User profile in preferences dialog
*
* - user name, surname, email, avatar
* - first name, last name, username, email
*
*/

Expand Down Expand Up @@ -47,6 +48,28 @@ qx.Class.define("osparc.desktop.account.ProfilePage", {
__userProfileData: null,
__userProfileModel: null,

__fetchProfile: function() {
osparc.data.Resources.getOne("profile", {}, null, false)
.then(profile => {
this.__setDataToModel(profile);
})
.catch(err => {
console.error(err);
});
},

__setDataToModel: function(data) {
if (data) {
this.__userProfileData = data;
this.__userProfileModel.set({
"firstName": data["first_name"] || "",
"lastName": data["last_name"] || "",
"email": data["login"],
"expirationDate": data["expirationDate"] || null
});
}
},

__createProfileUser: function() {
// layout
const box = osparc.ui.window.TabbedView.createSectionBox(this.tr("User"));
Expand Down Expand Up @@ -140,55 +163,30 @@ qx.Class.define("osparc.desktop.account.ProfilePage", {
return;
}

const requests = {
email: null,
names: null
};
if (this.__userProfileData["login"] !== model.getEmail()) {
if (emailValidator.validate()) {
const emailReq = new osparc.io.request.ApiRequest("/auth/change-email", "POST");
emailReq.setRequestData({
"email": model.getEmail()
});
requests.email = emailReq;
}
}

if (this.__userProfileData["first_name"] !== model.getFirstName() || this.__userProfileData["last_name"] !== model.getLastName()) {
if (namesValidator.validate()) {
const profileReq = new osparc.io.request.ApiRequest("/me", "PATCH");
profileReq.setRequestData({
"first_name": model.getFirstName(),
"last_name": model.getLastName()
});
requests.names = profileReq;
const params = {
data: {
"first_name": model.getFirstName(),
"last_name": model.getLastName(),
}
};
osparc.data.Resources.fetch("profile", "patch", params)
.then(() => {
this.__setDataToModel(Object.assign(this.__userProfileData, params.data));
osparc.auth.Manager.getInstance().updateProfile(this.__userProfileData);
const msg = this.tr("Profile updated");
osparc.FlashMessenger.getInstance().logAs(msg, "INFO");
})
.catch(err => {
this.__resetDataToModel();
const msg = err.message || this.tr("Failed to update profile");
osparc.FlashMessenger.getInstance().logAs(msg, "ERROR");
console.error(err);
});
}
}

Object.keys(requests).forEach(key => {
const req = requests[key];
if (req === null) {
return;
}

req.addListenerOnce("success", e => {
const reqData = e.getTarget().getRequestData();
this.__setDataToModel(Object.assign(this.__userProfileData, reqData));
osparc.auth.Manager.getInstance().updateProfile(this.__userProfileData);
const res = e.getTarget().getResponse();
const msg = (res && res.data) ? res.data : this.tr("Profile updated");
osparc.FlashMessenger.getInstance().logAs(msg, "INFO");
}, this);

req.addListenerOnce("fail", e => {
this.__resetDataToModel();
const msg = osparc.data.Resources.getErrorMsg(e.getTarget().getResponse()) || this.tr("Failed to update profile");
osparc.FlashMessenger.getInstance().logAs(msg, "ERROR");
}, this);

req.send();
});
}, this);
});

return box;
},
Expand Down Expand Up @@ -261,28 +259,6 @@ qx.Class.define("osparc.desktop.account.ProfilePage", {
return box;
},

__fetchProfile: function() {
osparc.data.Resources.getOne("profile", {}, null, false)
.then(profile => {
this.__setDataToModel(profile);
})
.catch(err => {
console.error(err);
});
},

__setDataToModel: function(data) {
if (data) {
this.__userProfileData = data;
this.__userProfileModel.set({
"firstName": data["first_name"] || "",
"lastName": data["last_name"] || "",
"email": data["login"],
"expirationDate": data["expirationDate"] || null
});
}
},

__resetDataToModel: function() {
this.__setDataToModel(this.__userProfileData);
},
Expand Down

0 comments on commit 72a5d99

Please sign in to comment.