-
-
Notifications
You must be signed in to change notification settings - Fork 444
London10_Jan_Softa_Node.js_Week2_CYF_Chat_Server #289
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| const chatMessageModel = { | ||
| id: 0, | ||
| from: "", | ||
| text: "", | ||
| }; | ||
|
|
||
| module.exports = chatMessageModel; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,125 @@ | ||
| <!-- working on send/add messages, see all messages and delete messages and give error | ||
| error message if not both name and message is filled in--> | ||
|
|
||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>Welcome to CYF Chat</title> | ||
| </head> | ||
|
|
||
| <body> | ||
| <h1> | ||
| CYF Chat | ||
| </h1> | ||
| <h2> | ||
| Send a message | ||
| </h2> | ||
| <h1>CYF Chat</h1> | ||
| <h2>Send a message</h2> | ||
| <form action="/messages" method="post"> | ||
| <p> | ||
| Name: <input type="text" name="from" placeholder="Your Name" /> <br /> | ||
| Message: <input type="text" name="text" placeholder="The message..." /> | ||
| <br /> | ||
| </p> | ||
| <button type="submit"> | ||
| Send | ||
| </button> | ||
| <button type="submit">Send</button> | ||
| </form> | ||
|
|
||
| <br /> | ||
| <a href="/messages">See all messages</a> | ||
|
|
||
| <form> | ||
| <p> | ||
| Delete Message: | ||
| <input | ||
| type="number" | ||
| id="deleteId" | ||
| name="id" | ||
| placeholder="Delete message by ID..." | ||
| /> | ||
| </p> | ||
| <button type="button" onclick="deleteMessage()">Submit</button> | ||
| </form> | ||
|
|
||
| <script> | ||
| function deleteMessage() { | ||
| const idToDelete = document.getElementById("deleteId").value; | ||
| if (!idToDelete || isNaN(idToDelete)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of a guard clause! 🥳 it's very important to have these layers of security throughout our codebase, to minimise the amount of bugs, and stop our program from crashing. Excellent work! |
||
| alert("Please enter a valid ID to delete."); | ||
| return; | ||
| } | ||
|
|
||
| const url = `/messages/delete/${idToDelete}`; | ||
| fetch(url, { | ||
| method: "DELETE", | ||
| }) | ||
| .then((response) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, as a suggestion and going forward with JavaScript, is to get acquainted with ES6+ features and apply them to your JS solutions. One of the ES6+ features (which generally means modern JavaScript) is the use of The idea of using async/await is to make your codebase shorter and thus easier to understand. 😊 |
||
| if (!response.ok) { | ||
| throw new Error("Network response was not ok"); | ||
| } | ||
| return response.json(); | ||
| }) | ||
| .then((data) => { | ||
| // Handle the response data here if needed | ||
| console.log("Message deleted successfully:", data); | ||
| // Refresh the page to see the updated messages list | ||
| window.location.reload(); | ||
| }) | ||
| .catch((error) => { | ||
| console.error("Error deleting message:", error); | ||
| }); | ||
| } | ||
| </script> | ||
| </body> | ||
| </html> | ||
|
|
||
| <!-- code is working to show you added message--> | ||
| <!-- <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>Welcome to CYF Chat</title> | ||
| </head> | ||
| <body> | ||
| <h1>CYF Chat</h1> | ||
| <h2>Send a message</h2> | ||
| <form action="/messages" method="post"> | ||
| <p> | ||
| Name: <input type="text" name="from" placeholder="Your Name" /> <br /> | ||
| Message: <input type="text" name="text" placeholder="The message..." /> | ||
| <br /> | ||
| </p> | ||
| <button type="submit">Send</button> | ||
| </form> | ||
| <br /> | ||
| <a href="/messages/showAll" action="/messages/showAll" method="get" | ||
| >See all messages</a | ||
| > | ||
| <form action="/messages/:id" method="delete"> | ||
| <p> | ||
| Delete Message: | ||
| <input | ||
| type="number" | ||
| name="number" | ||
| placeholder="Delete message by ID,,," | ||
| /> | ||
| </p> | ||
| <button type="submit">Submit</button> | ||
| </form> | ||
| </body> | ||
| </html> --> | ||
|
|
||
| <!-- <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>Welcome to CYF Chat!!!!!!!</title> | ||
| </head> | ||
| <body> | ||
| <h1>CYF Chat</h1> | ||
| <h2>Send a message</h2> | ||
| <form action="/messages" method="post"> | ||
| <p> | ||
| Name: <input type="text" name="from" placeholder="Your Name" /> <br /> | ||
| Message: <input type="text" name="text" placeholder="The message..." /> | ||
| <br /> | ||
| </p> | ||
| <button type="submit">Send</button> | ||
| </form> | ||
| <a href="/messages">See all messages</a> | ||
| </body> | ||
| </html> --> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just as a suggestion, we can create a separate javascript file, for example
script.jsand then refer or include that exact javascript file into the script HTML element. You can read more on that here for example. This way, we can effectively structure our code.