Skip to content

Commit

Permalink
Merge pull request #86 from priyanshuverma-dev/feat-more-actions
Browse files Browse the repository at this point in the history
fixes #77: Feature copy and delete actions
  • Loading branch information
kom-senapati authored Oct 12, 2024
2 parents 9d18c12 + 0339eff commit 749b3ac
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
18 changes: 18 additions & 0 deletions app/api_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,24 @@ def api_anonymous_chatbot() -> Union[Response, tuple[Response, int]]:
)


@api_bp.route("/api/chat/<int:chat_id>/delete", methods=["POST"])
@login_required
def api_chat_delete(chat_id: int) -> Union[Response, tuple[Response, int]]:
chat: Chat = Chat.query.get_or_404(chat_id)
if chat.user_id != current_user.uid:
return (
jsonify({"error": "Unauthorized access."}),
403,
)
chat.query.filter_by(id=chat_id).delete()
return (
jsonify(
{"success": True, "message": "Message deleted.", "chat": chat.chatbot_id}
),
200,
)


@api_bp.route("/api/chatbot/<int:chatbot_id>/clear", methods=["POST"])
@login_required
def api_clear_chats(chatbot_id: int) -> Union[Response, tuple[Response, int]]:
Expand Down
51 changes: 51 additions & 0 deletions app/static/js/chatbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,57 @@ if (window.location.pathname.includes("update")) {

messageForm.addEventListener("submit", sendMessage);

// COPY BUTTON
var copyButtons = document.querySelectorAll(".copy-btn");

copyButtons.forEach(function (btn) {
btn.addEventListener("click", function () {
var parentContainer = btn.closest(".flex.flex-col");

// Select the chat-response element inside the parent container
var responseElement = parentContainer.querySelector(".chat-response");
var responseText = responseElement.getAttribute("data-response");

if (responseText) {
console.log("Copied To clipboard");
navigator.clipboard.writeText(responseText);
}
});
});

// DELETE CHAT
var deleteButtons = document.querySelectorAll(".delete-btn");

deleteButtons.forEach(function (btn) {
btn.addEventListener("click", async function () {
var chatId = btn.getAttribute("data-id");

console.log(chatId);
try {
const response = await fetch(`/api/chat/${chatId}/delete`, {
method: "POST",
});

const data = await response.json(); // Assuming the server responds with JSON

if (!response.ok) {
throw new Error(data.message);
}

if (data.success) {
loadContent(`/chatbot/${data.chat}`);
setTimeout(scrollToBottom, 100);
} else {
// Handle errors returned by the server
document.getElementById("error").textContent =
data.message || "An error occurred.";
}
} catch (error) {
console.error("failed:", error);
document.getElementById("error").textContent = error.message;
}
});
});
// CLEAR CHATS
async function deleteChats(event) {
event.preventDefault(); // Prevent the default form submission
Expand Down
12 changes: 11 additions & 1 deletion app/templates/chatbot.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ <h1 class="text-4xl font-extrabold dark:text-dark text-center">{{ chatbot.name }
{% endautoescape %}
</p>
<!-- Speak button aligned at the bottom -->
<div class="flex justify-end mt-2">
<div class="flex justify-end mt-2 space-x-2">
<button
class="bg-red-500 hover:bg-red-600 text-white rounded-full p-2 drop-shadow transition duration-200 flex items-center justify-center delete-btn"
data-id="{{ chat.id }}" title="Delete">
<i class="fa-solid fa-trash-alt"></i>
</button>
<button type="button"
class="bg-blue-500 hover:bg-blue-600 text-white rounded-full p-2 drop-shadow transition duration-200 flex items-center justify-center copy-btn"
title="Copy">
<i class="fa-solid fa-copy"></i>
</button>
<button type="button"
class="bg-blue-500 hover:bg-blue-600 text-white rounded-full p-2 drop-shadow transition duration-200 flex items-center justify-center speak-btn"
title="Speak">
Expand Down

0 comments on commit 749b3ac

Please sign in to comment.