Skip to content

Commit 904b669

Browse files
authored
✨ [Frontend] Conversations: Edit and Delete messages (#7954)
1 parent 4c6c5df commit 904b669

File tree

5 files changed

+148
-13
lines changed

5 files changed

+148
-13
lines changed

services/static-webserver/client/source/class/osparc/conversation/AddMessage.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,27 @@ qx.Class.define("osparc.conversation.AddMessage", {
2323
* @param studyData {Object} serialized Study Data
2424
* @param conversationId {String} Conversation Id
2525
*/
26-
construct: function(studyData, conversationId = null) {
26+
construct: function(studyData, conversationId = null, message = null) {
2727
this.base(arguments);
2828

2929
this.__studyData = studyData;
3030
this.__conversationId = conversationId;
31+
this.__message = message;
3132

3233
this._setLayout(new qx.ui.layout.VBox(5));
3334

3435
this.__buildLayout();
3536
},
3637

3738
events: {
38-
"commentAdded": "qx.event.type.Event"
39+
"commentAdded": "qx.event.type.Data",
40+
"messageEdited": "qx.event.type.Data",
3941
},
4042

4143
members: {
4244
__studyData: null,
4345
__conversationId: null,
46+
__message: null,
4447

4548
_createChildControlImpl: function(id) {
4649
let control;
@@ -120,13 +123,22 @@ qx.Class.define("osparc.conversation.AddMessage", {
120123

121124
__buildLayout: function() {
122125
this.getChildControl("thumbnail");
123-
this.getChildControl("comment-field");
126+
const commentField = this.getChildControl("comment-field");
124127

125128
const addMessageButton = this.getChildControl("add-comment-button");
126-
addMessageButton.addListener("execute", () => this.__addComment());
129+
if (this.__message) {
130+
// edit mode
131+
addMessageButton.setLabel(this.tr("Edit message"));
132+
addMessageButton.addListener("execute", () => this.__editComment());
133+
134+
commentField.setText(this.__message["content"]);
135+
} else {
136+
// new message
137+
addMessageButton.addListener("execute", () => this.__addComment());
127138

128-
const notifyUserButton = this.getChildControl("notify-user-button");
129-
notifyUserButton.addListener("execute", () => this.__notifyUserTapped());
139+
const notifyUserButton = this.getChildControl("notify-user-button");
140+
notifyUserButton.addListener("execute", () => this.__notifyUserTapped());
141+
}
130142
},
131143

132144
__addComment: function() {
@@ -211,13 +223,24 @@ qx.Class.define("osparc.conversation.AddMessage", {
211223

212224
__postMessage: function() {
213225
const commentField = this.getChildControl("comment-field");
214-
const comment = commentField.getChildControl("text-area").getValue();
215-
if (comment) {
216-
osparc.study.Conversations.addMessage(this.__studyData["uuid"], this.__conversationId, comment)
226+
const content = commentField.getChildControl("text-area").getValue();
227+
if (content) {
228+
osparc.study.Conversations.addMessage(this.__studyData["uuid"], this.__conversationId, content)
217229
.then(data => {
218230
this.fireDataEvent("commentAdded", data);
219231
commentField.getChildControl("text-area").setValue("");
220-
osparc.FlashMessenger.logAs(this.tr("Message added"), "INFO");
232+
});
233+
}
234+
},
235+
236+
__editComment: function() {
237+
const commentField = this.getChildControl("comment-field");
238+
const content = commentField.getChildControl("text-area").getValue();
239+
if (content) {
240+
osparc.study.Conversations.editMessage(this.__studyData["uuid"], this.__conversationId, this.__message["messageId"], content)
241+
.then(data => {
242+
this.fireDataEvent("messageEdited", data);
243+
commentField.getChildControl("text-area").setValue("");
221244
});
222245
}
223246
},

services/static-webserver/client/source/class/osparc/conversation/Conversation.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ qx.Class.define("osparc.conversation.Conversation", {
195195
this.__getNextRequest()
196196
.then(resp => {
197197
const messages = resp["data"];
198+
// it's not provided by the backend
199+
messages.forEach(message => message["studyId"] = this.__studyData["uuid"]);
198200
this.__addMessages(messages);
199201
this.__nextRequestParams = resp["_links"]["next"];
200202
if (this.__nextRequestParams === null) {
@@ -236,7 +238,9 @@ qx.Class.define("osparc.conversation.Conversation", {
236238
let control = null;
237239
switch (message["type"]) {
238240
case "MESSAGE":
239-
control = new osparc.conversation.MessageUI(message);
241+
control = new osparc.conversation.MessageUI(message, this.__studyData);
242+
control.addListener("messageEdited", () => this.fetchMessages());
243+
control.addListener("messageDeleted", () => this.fetchMessages());
240244
break;
241245
case "NOTIFICATION":
242246
control = new osparc.conversation.NotificationUI(message);

services/static-webserver/client/source/class/osparc/conversation/MessageUI.js

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ qx.Class.define("osparc.conversation.MessageUI", {
2121

2222
/**
2323
* @param message {Object} message
24+
* @param studyData {Object?null} serialized Study Data
2425
*/
25-
construct: function(message) {
26+
construct: function(message, studyData = null) {
2627
this.base(arguments);
2728

2829
this.__message = message;
30+
this.__studyData = studyData;
2931

3032
const isMyMessage = this.self().isMyMessage(this.__message);
3133
const layout = new qx.ui.layout.Grid(12, 4);
@@ -43,6 +45,11 @@ qx.Class.define("osparc.conversation.MessageUI", {
4345
}
4446
},
4547

48+
events: {
49+
"messageEdited": "qx.event.type.Event",
50+
"messageDeleted": "qx.event.type.Event",
51+
},
52+
4653
members: {
4754
__message: null,
4855

@@ -109,6 +116,25 @@ qx.Class.define("osparc.conversation.MessageUI", {
109116
column: isMyMessage ? 0 : 2,
110117
});
111118
break;
119+
case "menu-button": {
120+
const buttonSize = 22;
121+
control = new qx.ui.form.MenuButton().set({
122+
width: buttonSize,
123+
height: buttonSize,
124+
allowGrowX: false,
125+
allowGrowY: false,
126+
marginTop: 4,
127+
alignY: "top",
128+
icon: "@FontAwesome5Solid/ellipsis-v/14",
129+
focusable: false
130+
});
131+
this._add(control, {
132+
row: 0,
133+
column: 3,
134+
rowSpan: 2,
135+
});
136+
break;
137+
}
112138
}
113139

114140
return control || this.base(arguments, id);
@@ -143,6 +169,53 @@ qx.Class.define("osparc.conversation.MessageUI", {
143169
});
144170

145171
this.getChildControl("spacer");
146-
}
172+
173+
if (this.self().isMyMessage(this.__message)) {
174+
const menuButton = this.getChildControl("menu-button");
175+
176+
const menu = new qx.ui.menu.Menu().set({
177+
position: "bottom-right",
178+
});
179+
menuButton.setMenu(menu);
180+
181+
const editButton = new qx.ui.menu.Button(this.tr("Edit..."));
182+
editButton.addListener("execute", () => this.__editMessage(), this);
183+
menu.add(editButton);
184+
185+
const deleteButton = new qx.ui.menu.Button(this.tr("Delete..."));
186+
deleteButton.addListener("execute", () => this.__deleteMessage(), this);
187+
menu.add(deleteButton);
188+
}
189+
},
190+
191+
__editMessage: function() {
192+
const addMessage = new osparc.conversation.AddMessage(this.__studyData, this.__message["conversationId"], this.__message);
193+
const title = this.tr("Edit message");
194+
const win = osparc.ui.window.Window.popUpInWindow(addMessage, title, 570, 135).set({
195+
clickAwayClose: false,
196+
resizable: true,
197+
showClose: true,
198+
});
199+
addMessage.addListener("messageEdited", () => {
200+
win.close();
201+
this.fireDataEvent("messageEdited");
202+
});
203+
},
204+
205+
__deleteMessage: function() {
206+
const win = new osparc.ui.window.Confirmation(this.tr("Delete message?")).set({
207+
caption: this.tr("Delete"),
208+
confirmText: this.tr("Delete"),
209+
confirmAction: "delete",
210+
});
211+
win.open();
212+
win.addListener("close", () => {
213+
if (win.getConfirmed()) {
214+
osparc.study.Conversations.deleteMessage(this.__message["studyId"], this.__message["conversationId"], this.__message["messageId"])
215+
.then(() => this.fireEvent("messageDeleted"))
216+
.catch(err => osparc.FlashMessenger.logError(err));
217+
}
218+
});
219+
},
147220
}
148221
});

services/static-webserver/client/source/class/osparc/data/Resources.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ qx.Class.define("osparc.data.Resources", {
332332
method: "POST",
333333
url: statics.API + "/projects/{studyId}/conversations/{conversationId}/messages"
334334
},
335+
editMessage: {
336+
method: "PUT",
337+
url: statics.API + "/projects/{studyId}/conversations/{conversationId}/messages/{messageId}"
338+
},
339+
deleteMessage: {
340+
method: "DELETE",
341+
url: statics.API + "/projects/{studyId}/conversations/{conversationId}/messages/{messageId}"
342+
},
335343
getMessagesPage: {
336344
method: "GET",
337345
url: statics.API + "/projects/{studyId}/conversations/{conversationId}/messages?offset={offset}&limit={limit}"

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,33 @@ qx.Class.define("osparc.study.Conversations", {
9494
.catch(err => osparc.FlashMessenger.logError(err));
9595
},
9696

97+
editMessage: function(studyId, conversationId, messageId, message) {
98+
const params = {
99+
url: {
100+
studyId,
101+
conversationId,
102+
messageId,
103+
},
104+
data: {
105+
"content": message,
106+
},
107+
};
108+
return osparc.data.Resources.fetch("conversations", "editMessage", params)
109+
.catch(err => osparc.FlashMessenger.logError(err));
110+
},
111+
112+
deleteMessage: function(studyId, conversationId, messageId) {
113+
const params = {
114+
url: {
115+
studyId,
116+
conversationId,
117+
messageId,
118+
},
119+
};
120+
return osparc.data.Resources.fetch("conversations", "deleteMessage", params)
121+
.catch(err => osparc.FlashMessenger.logError(err));
122+
},
123+
97124
notifyUser: function(studyId, conversationId, userGroupId) {
98125
const params = {
99126
url: {

0 commit comments

Comments
 (0)