This commit is contained in:
tuffahni 2024-12-31 00:12:59 +00:00
parent c6d68334be
commit ecd7b650bd
4 changed files with 151 additions and 115 deletions

View File

@ -192,7 +192,8 @@ ol li {
border-radius: 20px; border-radius: 20px;
text-align: left; text-align: left;
font-family: "Cutive Mono", monospace; font-family: "Cutive Mono", monospace;
min-width: 250px; min-width: 200px;
max-width: 300px;
display: flex; display: flex;
flex-direction: row; flex-direction: row;
justify-content: space-between; justify-content: space-between;
@ -209,7 +210,7 @@ ol li button {
.decor { .decor {
position: absolute; position: absolute;
top: 50px; top: 20px;
left: -50px; left: -50px;
} }
.decor img { .decor img {

View File

@ -0,0 +1,24 @@
// Function to delete a todo item
function deleteTodo(todo) {
fetch("/todo", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ todo }),
})
.then((response) => {
if (response.ok) {
// Remove the list item and button from the DOM
const listItem = [...document.querySelectorAll("li")].find((li) =>
li.textContent.trim().startsWith(todo)
);
if (listItem) {
listItem.remove();
}
} else {
console.error("Failed to delete the todo item.");
}
})
.catch((error) => console.error("Error deleting todo:", error));
}

View File

@ -20,21 +20,21 @@ app.set("view engine", "ejs");
app.use(express.static(`${__dirname}/../public`)); app.use(express.static(`${__dirname}/../public`));
//check and send index ejs //check and send index ejs
//Query postgres database to get all todos (async await)
app.get("/", (_req: Request, res: Response): void => { app.get("/", (_req: Request, res: Response): void => {
let todos: string[] = []; pg.query("SELECT * FROM todos", (err: Error, result: any) => {
//Query postgres database to get all todos (async await)
pg.query("SELECT * FROM todos", async (err: Error, result: any) => {
if (err) { if (err) {
console.log("Error querying todos", err); console.log("Error querying todos", err);
res.status(500).send("Internal Server Error");
return; return;
} }
todos = result.rows.map((row: any) => row.todo); // Extract all rows from the database
} const todos = result.rows.map((row: { todo: string }) => row.todo);
res.render("index",{ res.render("index", {
todos, todos, // Pass todos to the template
});
}); });
}); });
@ -54,9 +54,18 @@ app.post("/form-action", (req: Request, res: Response): void => {
res.redirect("/"); res.redirect("/");
}); });
// app.delete("/delete-todo/:todo", (req: Request, res: Response): void => { app.delete("/todo", (req: Request, res: Response): void => {
// //delete todo from postgres const { todo } = req.body;
// });
pg.query("DELETE FROM todos WHERE todo = $1", [todo], (err: Error) => {
if (err) {
console.log("Error deleting todo", err);
return;
}
res.status(200).send({ message: "Todo deleted successfully" });
});
});
app.listen(port, (): void => { app.listen(port, (): void => {
console.log(`Server is running on port ${port}`); console.log(`Server is running on port ${port}`);

View File

@ -51,11 +51,13 @@
<h1>To do list</h1> <h1>To do list</h1>
<ol> <ol>
<!-- rendering the todos --> <!-- rendering the todos -->
<!-- <% todos.forEach(todo => { %> <li><%= todo %></li> <% }); %> --> <% todos.forEach(todo => { %>
<!-- click button, go to app.delete, then delete something --> <li>
<!-- <li>Learn from Ronald</li> <%= todo %>
<li>Watch movies</li> <button onclick="deleteTodo('<%= todo %>')" class="delete-button">
<li>Sleep</li> --> <i class="fa-solid fa-delete-left"></i>
</li>
<% }); %>
</ol> </ol>
<div class="decor"> <div class="decor">
<img src="images/cryinggirl.gif" /> <img src="images/cryinggirl.gif" />
@ -69,5 +71,5 @@
</section> </section>
</body> </body>
<!-- <script src="js/todo.js"></script> --> <script src="js/index.js"></script>
</html> </html>