Skip to content

Commit

Permalink
feat: Delete message
Browse files Browse the repository at this point in the history
  • Loading branch information
data-miner00 committed Apr 20, 2024
1 parent 3b38ece commit fb73f74
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
15 changes: 15 additions & 0 deletions src/Linker.Mvc/Hubs/ChatHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ public Task EditMessage(EditChatMessage edit)
return Task.WhenAll(tasks);
}

public Task DeleteMessage(string workspaceId, string chatId)
{
if (!this.connectionManager.Connections.TryGetValue(this.Context.ConnectionId, out var connection))
{
return Task.CompletedTask;
}

IEnumerable<Task> tasks = [
this.repository.SoftDeleteChatMessageAsync(chatId, default),
this.Clients.Group(workspaceId).SendAsync("DeleteMessage", chatId),
];

return Task.WhenAll(tasks);
}

private void KeepConnection(ChatConnection connection)
{
this.connectionManager.Connections[this.Context.ConnectionId] = connection;
Expand Down
38 changes: 29 additions & 9 deletions src/Linker.Mvc/Views/Workspace/Details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,25 @@
<article class="border-b border-solid border-black p-2 bg-gray-50">
<div class="flex justify-between">
<p class="text-xs">@username - @message.CreatedAt</p>
<div class="flex gap-4">
<button class="block" onclick="return toggleChatEdit('@message.Id')"><i class="bi bi-pencil"></i></button>
<button class="block"><i class="bi bi-trash"></i></button>
</div>
@if (!message.IsDeleted)
{
<div class="flex gap-4">
<button class="block" onclick="return toggleChatEdit('@message.Id')"><i class="bi bi-pencil"></i></button>
<button class="block" onclick="return deleteChat('@message.Id')"><i class="bi bi-trash"></i></button>
</div>
}
</div>
<p id="p-@message.Id" class="bg-pink-50">@message.Message</p>
<form id="f-@message.Id" class="hidden" onsubmit="return editChatMessage(event, '@message.Id')">
<input class="bg-purple-50" name="editMessage" value="@message.Message" />
</form>
@if (!message.IsDeleted)
{
<p id="p-@message.Id" class="bg-pink-50">@message.Message</p>
<form id="f-@message.Id" class="hidden" onsubmit="return editChatMessage(event, '@message.Id')">
<input class="bg-purple-50" name="editMessage" value="@message.Message" />
</form>
}
else
{
<p class="bg-pink-50 italic">The message has been deleted.</p>
}
</article>
}
</div>
Expand Down Expand Up @@ -239,7 +249,7 @@
<p class="text-xs">${usr} - ${msg.createdAt}</p>
<div class="flex gap-4">
<button class="block" onclick="return toggleChatEdit('${msg.id}')"><i class="bi bi-pencil"></i></button>
<button class="block"><i class="bi bi-trash"></i></button>
<button class="block" onclick="return deleteChat('${msg.id}')"><i class="bi bi-trash"></i></button>
</div>
</div>
<p id="p-${msg.id}" class="bg-pink-50">${msg.message}</p>
Expand All @@ -256,6 +266,10 @@
console.log(msg);
});
connection.on("DeleteMessage", (chatId) => {
$(`#p-${chatId}`).text("The message has been deleted.").addClass("italic");
})
connection.start().then(() => {
connection.invoke("JoinSpecificChatRoom", {
username: "@User.FindFirstValue(ClaimTypes.Name)",
Expand Down Expand Up @@ -330,5 +344,11 @@
function openAllLinksInNewTab(links) {
links.forEach((link) => window.open(link, "_blank"));
}
function deleteChat(chatId) {
connection
.invoke("DeleteMessage", "@Model.WorkspaceId", chatId)
.catch(console.error);
}
</script>
}

0 comments on commit fb73f74

Please sign in to comment.