Skip to content

Commit c98c8fd

Browse files
committed
refactor
1 parent 72874ce commit c98c8fd

File tree

1 file changed

+68
-65
lines changed

1 file changed

+68
-65
lines changed

services/static-webserver/client/source/class/osparc/study/BillingSettings.js

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -34,82 +34,85 @@ qx.Class.define("osparc.study.BillingSettings", {
3434
members: {
3535
__studyData: null,
3636

37+
_createChildControlImpl: function(id) {
38+
let control;
39+
switch (id) {
40+
case "credit-account-layout":
41+
control = osparc.study.StudyOptions.createGroupBox(this.tr("Credit Account"));
42+
this._add(control);
43+
}
44+
return control || this.base(arguments, id);
45+
},
46+
3747
__buildLayout: function() {
38-
this.__buildWalletGroup();
48+
this.__populateWallets();
3949
this.__buildPricingUnitsGroup();
4050
},
4151

42-
__buildWalletGroup: function() {
43-
const pricingUnitsLayout = osparc.study.StudyOptions.createGroupBox(this.tr("Credit Account"));
52+
__populateWallets: function() {
53+
const creditAccountLayout = this.getChildControl("credit-account-layout");
54+
creditAccountLayout.removeAll();
4455

45-
const populateCreditAccountBox = () => {
46-
pricingUnitsLayout.removeAll();
47-
48-
const hBox = new qx.ui.container.Composite(new qx.ui.layout.HBox(10)).set({
49-
alignY: "middle"
50-
});
56+
const boxContent = new qx.ui.container.Composite(new qx.ui.layout.HBox(10)).set({
57+
alignY: "middle"
58+
});
59+
creditAccountLayout.add(boxContent);
5160

52-
const walletSelector = osparc.desktop.credits.Utils.createWalletSelector("read");
53-
hBox.add(walletSelector);
61+
const walletSelector = osparc.desktop.credits.Utils.createWalletSelector("read");
62+
boxContent.add(walletSelector);
5463

55-
pricingUnitsLayout.add(hBox);
56-
57-
const paramsGet = {
58-
url: {
59-
studyId: this.__studyData["uuid"]
64+
const paramsGet = {
65+
url: {
66+
studyId: this.__studyData["uuid"]
67+
}
68+
};
69+
osparc.data.Resources.fetch("studies", "getWallet", paramsGet)
70+
.then(wallet => {
71+
if (wallet) {
72+
const walletFound = walletSelector.getSelectables().find(selectables => selectables.walletId === wallet["walletId"]);
73+
if (walletFound) {
74+
walletSelector.setSelection([walletFound]);
75+
} else {
76+
const emptyItem = new qx.ui.form.ListItem("");
77+
emptyItem.walletId = null;
78+
walletSelector.add(emptyItem);
79+
walletSelector.setSelection([emptyItem]);
80+
const label = new qx.ui.basic.Label(this.tr("You don't have access to the last used Credit Account"));
81+
boxContent.add(label);
82+
}
6083
}
61-
};
62-
osparc.data.Resources.fetch("studies", "getWallet", paramsGet)
63-
.then(wallet => {
64-
if (wallet) {
65-
const walletFound = walletSelector.getSelectables().find(selectables => selectables.walletId === wallet["walletId"]);
66-
if (walletFound) {
67-
walletSelector.setSelection([walletFound]);
68-
} else {
69-
const emptyItem = new qx.ui.form.ListItem("");
70-
emptyItem.walletId = null;
71-
walletSelector.add(emptyItem);
72-
walletSelector.setSelection([emptyItem]);
73-
const label = new qx.ui.basic.Label(this.tr("You don't have access to the last used Credit Account"));
74-
hBox.add(label);
84+
})
85+
.finally(() => {
86+
walletSelector.addListener("changeSelection", e => {
87+
const selection = e.getData();
88+
if (selection.length) {
89+
const walletId = selection[0].walletId;
90+
if (walletId === null) {
91+
return;
7592
}
76-
}
77-
})
78-
.finally(() => {
79-
walletSelector.addListener("changeSelection", e => {
80-
const selection = e.getData();
81-
if (selection.length) {
82-
const walletId = selection[0].walletId;
83-
if (walletId === null) {
84-
return;
93+
boxContent.setEnabled(false);
94+
const paramsPut = {
95+
url: {
96+
studyId: this.__studyData["uuid"],
97+
walletId
8598
}
86-
hBox.setEnabled(false);
87-
const paramsPut = {
88-
url: {
89-
studyId: this.__studyData["uuid"],
90-
walletId
91-
}
92-
};
93-
osparc.data.Resources.fetch("studies", "selectWallet", paramsPut)
94-
.then(() => {
95-
const msg = this.tr("Credit Account saved");
96-
osparc.FlashMessenger.getInstance().logAs(msg, "INFO");
97-
})
98-
.catch(err => {
99-
console.error(err);
100-
osparc.FlashMessenger.logAs(err.message, "ERROR");
101-
})
102-
.finally(() => {
103-
hBox.setEnabled(true);
104-
populateCreditAccountBox();
105-
});
106-
}
107-
});
99+
};
100+
osparc.data.Resources.fetch("studies", "selectWallet", paramsPut)
101+
.then(() => {
102+
const msg = this.tr("Credit Account saved");
103+
osparc.FlashMessenger.getInstance().logAs(msg, "INFO");
104+
})
105+
.catch(err => {
106+
console.error(err);
107+
osparc.FlashMessenger.logAs(err.message, "ERROR");
108+
})
109+
.finally(() => {
110+
boxContent.setEnabled(true);
111+
this.__populateWallets();
112+
});
113+
}
108114
});
109-
};
110-
populateCreditAccountBox();
111-
112-
this._add(pricingUnitsLayout);
115+
});
113116
},
114117

115118
__buildPricingUnitsGroup: function() {

0 commit comments

Comments
 (0)