-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
155 lines (128 loc) · 4.31 KB
/
Copy pathapp.js
File metadata and controls
155 lines (128 loc) · 4.31 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const _ = require('lodash');
const cookieSession = require('cookie-session');
////////////// Set Production Environment //////////////
const productionEnvironment = false;
let cookieSessionExpirationHours = 0;
if (productionEnvironment) {
cookieSessionExpirationHours = 60 * 60 * 1000 * 24 * 7;
} else {
cookieSessionExpirationHours = 10000 ; // milliseconds | 60 * 60 * 1000 * 24 is 24 hours
}
////////////////////////////////////////////////////////
// initialize modules
const app = express();
app.set('trust proxy', 1); // trust the first proxy, from cookie-session
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + '/public'));
app.use(cookieSession({
name: 'session',
// keys: new Keygrip(['key1', 'key2'], 'SHA384', 'base64')
secret: process.env.COOKIE_SESSION_SECRET,
maxAge: cookieSessionExpirationHours
}));
function removeItemFromGuestList(req, idToRemove) {
// loop through guest posts list and if the id of the object
// matches the delete button id remove the current item and then stop
for (let i = 0; i < req.session.guestList.length; i++) {
const taskId = req.session.guestList[i]._id.toString();
if (taskId === idToRemove) {
req.session.guestList.splice(i, 1);
break;
}
}
};
function updateTaskChecked (req, checkedId) {
for (let i = 0; i < req.session.guestList.length; i++) {
const taskId = req.session.guestList[i]._id.toString();
if (taskId === checkedId) {
// toggle checked boolean
req.session.guestList[i].checked = !req.session.guestList[i].checked
break;
}
}
};
let startingSessionId = 0;
function initializeSessionList (req) {
req.session.guestList = [];
};
// used later with custom lists
// function formatCustomListName(arr) {
// return _.startCase(_.toLower(_.trim(arr).replace(/\s+/g,' ')));
// };
app.get("/", (req, res) => {
// if the session list doesn't exist start with fresh settings
if (!req.session.guestList) {
initializeSessionList(req);
}
res.render("list", {
listTitle: "Tasks",
listItems: req.session.guestList,
renderDeleteButton: false
});
// this will be used when new lists can be added by logged in user
// let existingLists = [];
// List.find({}, function(err, foundLists) {
// //console.log(foundLists);
// foundLists.forEach( list => {
// existingLists.push(list.name);
// });
// res.render("list", {
// listTitle: "Tasks",
// listItems: req.session.guestList,
// renderDeleteButton: false,
// existingLists: existingLists
// });
// });
});
app.post("/newTask", (req, res) => {
const listName = req.body.listName; // this comes from the '+' push
const itemName = req.body.newTask;
// iterate req.session.guestListId to keep getting unique id's
const newItem = {_id: startingSessionId++, task: itemName , checked: false};
if (listName === "Tasks") {
// if the session times out need to start with fresh settings
if (!req.session.guestList) {
initializeSessionList(req);
}
//req.session.guestList is a list of item objects
req.session.guestList.push(newItem);
res.redirect("/");
} // will add more functionality for other lists from mongodb
});
app.post("/deleteTask", (req, res) => {
const taskId = req.body.taskId;
const listName = req.body.listName; // from the hidden input in list.ejs
if (listName === "Tasks"){
// if the session times out need to start with fresh settings
if (req.session.guestList) {
removeItemFromGuestList(req, taskId);
} else {
initializeSessionList(req);
}
res.redirect("/");
} // will add more functionality for other lists from mongodb
});
app.post("/taskChecked", (req, res) => {
const taskId = req.body.taskId
const listName = req.body.listName
if (listName === "Tasks") {
// if the session times out need to start with fresh settings
if (req.session.guestList) {
updateTaskChecked(req, taskId);
} else {
initializeSessionList(req);
}
res.redirect("/");
}
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function() {
console.log("Server started on port: ", port);
});