-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
137 lines (112 loc) · 3.73 KB
/
app.js
File metadata and controls
137 lines (112 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import express from "express";
import bodyParser from "body-parser";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine", "ejs");
const port = 3000;
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const months = ["January","February","March","April",
"May","June","July","August",
"September","October","November","December"]
const _month = months[new Date().getMonth()];
const _day = days[new Date().getDay()];
const _year = new Date().getFullYear();
const date = {
year: _year,
month: _month,
dayNumber: new Date().getDay(),
day: _day
}
//let todoActivity = {todoId: 0 , todoItem: "item", completed: false};
const todayTodos = [{todoId: 0, todoItem: "item 1", completed: false}, {todoId: 1, todoItem: "Item 2", completed: false}];
const workTodos = [{todoId: 0, todoItem: "item 1", completed: false}, {todoId: 1, todoItem: "Item 2", completed: false}];
const buyerTodos = [{todoId: 0, todoItem: "item 1", completed: false}, {todoId: 1, todoItem: "Item 2", completed: false}];
app.get("/", (req, res) => {
res.render(__dirname + "/views/today.ejs", {
date,
todayTodos
});
});
app.get("/work", (req, res)=>{
res.render(__dirname + "/views/work.ejs",{
date,
workTodos
})
})
app.get("/buyer", (req, res)=>{
res.render(__dirname + "/views/buyer.ejs",{
date,
buyerTodos
})
})
app.post("/", (req, res) => {
const inputTodoId = todayTodos.length; // Update the calculation here
const inputTodoItem = req.body.todoTask;
todayTodos.push({
todoId: inputTodoId,
todoItem: inputTodoItem,
completed: false // Set the completed value to false for the new item
});
res.redirect("/");
});
app.post("/work", (req, res) => {
const inputTodoId = workTodos.length; // Update the calculation here
const inputTodoItem = req.body.todoTask;
workTodos.push({
todoId: inputTodoId,
todoItem: inputTodoItem,
completed: false // Set the completed value to false for the new item
});
res.redirect("/work");
});
app.post("/buyer", (req, res) => {
const inputTodoId = buyerTodos.length; // Update the calculation here
const inputTodoItem = req.body.todoTask;
buyerTodos.push({
todoId: inputTodoId,
todoItem: inputTodoItem,
completed: false // Set the completed value to false for the new item
});
res.redirect("/buyer");
});
app.post("/toggleCheckbox", (req, res) => {
const listName = req.body.listName;
const itemId = req.body.itemId;
const itemStatus = req.body.completed;
let selectedList = null;
let whereToReturn = null;
switch(listName){
case "todayTodos":
whereToReturn = "/";
selectedList = todayTodos;
console.log(todayTodos);
break;
case "workTodos":
whereToReturn = "/work";
selectedList = workTodos;
break;
case "buyerTodos":
whereToReturn = "/buyer";
selectedList = buyerTodos;
break;
default:
console.log("Invalid list name");
return;
}
const itemToUpdate = selectedList.find(task => task.todoId == itemId);
if (itemToUpdate) {
itemToUpdate.completed = itemStatus;
} else {
console.log("Item not found");
}
res.json({ success: true, selectedList });
})
app.listen(port, ()=>{
console.log(`server runnning on ${port}`);
})