Skip to content

Commit

Permalink
Merge branch 'nighthawkcoders:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Darsh220 authored Nov 12, 2024
2 parents 867d310 + 8dfc9c7 commit 2c004c5
Show file tree
Hide file tree
Showing 4 changed files with 846 additions and 129 deletions.
55 changes: 39 additions & 16 deletions navigation/rate_and_relate/instabox/instabox.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,25 +211,26 @@ author: Aadi, Aaditya, Aditya, Kanhay
e.preventDefault();
// Get the current text content of the chat input
const newMessage = chatInput.textContent.trim();
if (localStorage.getItem("messageCount")) {
localStorage.setItem("messageCount", parseInt(localStorage.getItem("messageCount")) + 1);
} else {
localStorage.setItem("messageCount", 1);
}
// if (localStorage.getItem("messageCount")) {
// localStorage.setItem("messageCount", parseInt(localStorage.getItem("messageCount")) + 1);
// } else {
// localStorage.setItem("messageCount", 1);
// }
localStorage.setItem("message" + localStorage.getItem("messageCount"), newMessage);
// Create a new paragraph element for the message
if (newMessage != "") {
const messageElement = document.createElement("p");
messageElement.innerHTML = `<strong>Me:</strong> ${newMessage}`;
// Append the new message to the instabox
instabox.appendChild(messageElement);
// Clear the chat input
chatInput.textContent = "";
// Scroll to the latest message
instabox.scrollTop = instabox.scrollHeight;
// Reset character count to 100
charCount.textContent = "100";
charCount.style.color = "#aaa";
// const messageElement = document.createElement("p");
// messageElement.innerHTML = `<strong>Me:</strong> ${newMessage}`;
// // Append the new message to the instabox
// instabox.appendChild(messageElement);
// // Clear the chat input
// chatInput.textContent = "";
// // Scroll to the latest message
// instabox.scrollTop = instabox.scrollHeight;
// // Reset character count to 100
// charCount.textContent = "100";
// charCount.style.color = "#aaa";
postMessage(newMessage);
}
}
});
Expand Down Expand Up @@ -261,6 +262,28 @@ author: Aadi, Aaditya, Aditya, Kanhay
});
});
}
// Add new message
function postMessage(newMessage) {
fetch(`${pythonURI}/api/messages`, {
...fetchOptions,
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ message: newMessage }) // send the new message as payload
})
.then(response => response.json())
.then(data => {
if (data.success) {
fetchMessages(); // refresh messages to include the new message
} else {
console.error("Error posting message:", data.error);
}
})
.catch(error => {
console.error("Error in postMessage:", error);
});
}


window.onload = fetchMessages; // use and run the script
Expand Down
Loading

0 comments on commit 2c004c5

Please # to comment.