-
Notifications
You must be signed in to change notification settings - Fork 0
/
showMessaheExtensions.js
87 lines (71 loc) · 2.43 KB
/
showMessaheExtensions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
sap.ui.define([
"sap/ui/integration/Extension",
"sap/ui/integration/library"
], function (Extension, integrationLibrary) {
"use strict";
const CardActionType = integrationLibrary.CardActionType;
const CardMessageType = integrationLibrary.CardMessageType;
const ShowMessageExtension = Extension.extend("card.explorer.extension.showMessage.ShowMessageExtension");
ShowMessageExtension.prototype.init = function () {
Extension.prototype.init.apply(this, arguments);
this.attachAction(this._handleAction.bind(this));
};
ShowMessageExtension.prototype._handleAction = function (oEvent) {
if (oEvent.getParameter("type") !== CardActionType.Custom) {
return;
}
const oActionParams = oEvent.getParameter("parameters");
let pExecuteAction;
if (oActionParams.method === "addToFavorites") {
pExecuteAction = this._addItemToFavorites(oActionParams.id);
} else if (oActionParams.method === "remove") {
pExecuteAction = this._removeItem(oActionParams.id);
} else {
return;
}
const oActionSource = oEvent.getParameter("actionSource");
const oCard = this.getCard();
oActionSource.setEnabled(false); // temporary disable the button
// Send request to the backend and show the result to the user
pExecuteAction
.then(function(sResponseText) {
oCard.showMessage(sResponseText, CardMessageType.Success);
if (oActionParams.method === "addToFavorites") {
oActionSource.setVisible(false);
} else if (oActionParams.method === "remove") {
oCard.refreshData();
}
})
.catch(function (aResponse) {
const oResponse = aResponse[1];
oResponse.text().then(function (sText) {
oCard.showMessage(sText, CardMessageType.Error);
});
oActionSource.setEnabled(true);
});
};
ShowMessageExtension.prototype._addItemToFavorites = function (sProductId) {
// add the item to favorites of the current user
return this.getCard().request({
url: "/user/" + this._getCurrentUserId() + "/favorites",
method: "POST",
headers: {
"Content-Type": "application/json"
},
parameters: {
productId: sProductId
}
});
};
ShowMessageExtension.prototype._removeItem = function (sProductId) {
// remove the item from the database and refresh the card data
return this.getCard().request({
url: "/products/" + sProductId,
method: "DELETE"
});
};
ShowMessageExtension.prototype._getCurrentUserId = function () {
return "CardExplorerUser";
};
return ShowMessageExtension;
});