-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
190 lines (185 loc) · 7.79 KB
/
main.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
let displayName = document.querySelector("#displayName");
let nameInput = document.querySelector("#name");
let btnAccept = document.querySelector("#accept");
let btnAdd = document.querySelector("#btnAdd");
let productTable = document.querySelector("#productTable");
let spanLogout = document.querySelector("#logout");
let enterName = document.querySelector("#enterName");
let header = document.querySelector("#header");
let btnLogout = document.querySelector("#btnLogout");
let shoppingListCounter = 0;
class ShoppingLists {
static getShoppingLists() {
const stringData = Cookies.get("shoppingLists");
return objectData = JSON.parse(stringData);
}
static getShoppingList(personName) {
if (typeof (personName) !== "string") { throw "personName needs to be String" }
const stringData = Cookies.get(personName + "shoppingList");
if (stringData === undefined) {
return undefined;
}
console.log(`stringData=${stringData}, type=${typeof (stringData)}`);
let objectData = JSON.parse(stringData);
let shoppingList = new ShoppingList(objectData);
return shoppingList;
}
static saveShoppingList(shoppingList) {
if (!Object.getOwnPropertyNames(shoppingList).includes("personName")) { throw "shoppingList needs to have a person name property!" }
Cookies.set(Cookies.get("name") + "shoppingList", JSON.stringify(shoppingList));
}
}
class ShoppingList {
constructor(param) {
if (typeof (param) === "string") {
this.shoppingListCounter = 0;
this.personName = param;
this.rows = [];
} else {
Object.assign(this, param);
}
}
addRow(name, quantity) {
if (typeof (name) !== "string" || typeof (quantity) !== "number") {
throw "illegal parameters";
}
this.rows.push(new ListRow(this.shoppingListCounter, name, quantity));
this.shoppingListCounter++;
}
removeRow(id) {
if (typeof (id) !== "number"){ throw "id is not a number"}
this.rows = this.rows.filter(row => row.id !== id);
this.shoppingListCounter--;
this.rows = this.rows.map((row, index) => {
row.id = index;
console.log(Object.getOwnPropertyNames(row));
return row;
});
}
}
class ListRow {
constructor(id, name, quantity) {
this.id = id;
this.name = name;
this.quantity = quantity;
}
}
class Application {
static showShoppingList(shoppingList) {
if (shoppingList === undefined) { throw "Shoppinglist is undefined" };
if (!Object.getOwnPropertyNames(shoppingList).includes("personName")) { throw "shoppingList needs to have a person name property!" }
console.log(Object.getOwnPropertyNames(shoppingList));
let table = document.querySelector("#productTable");
console.log(shoppingList.rows);
for (let row of shoppingList.rows) {
let domRow = document.createElement("tr");
domRow.classList.add("data");
table.appendChild(domRow);
for (let column of ['id', 'name', 'quantity']) {
let domColumn = document.createElement("td");
domRow.appendChild(domColumn);
let domColumnText = document.createTextNode(row[column]);
domColumn.appendChild(domColumnText);
if (column === 'name') {
let btnRemove = document.createElement("button");
domColumn.appendChild(btnRemove);
let btnRemoveText = document.createTextNode("Remove");
btnRemove.appendChild(btnRemoveText);
btnRemove.addEventListener("click", function () {
console.log("remove button clicked");
console.log(`name = ${Cookies.get("name")}`);
let sl = ShoppingLists.getShoppingList(Cookies.get('name'));
if (sl === undefined) { throw "Shopping list doesn't exist although it is displayed." }
if (row === undefined || row.id === undefined) { throw "row is not accessible here" };
console.log(`row.id = ${row.id}`)
sl.removeRow(row.id);
Application.clearShoppingList();
Application.showShoppingList(sl);
ShoppingLists.saveShoppingList(sl);
})
}
}
}
}
static clearShoppingList() {
let table = document.querySelector("#productTable");
if (table === null) { throw "There is no #productTable"; }
let rows = table.querySelectorAll("tr");
for (let i = 0; i < rows.length; i++) {
if (rows[i].classList.contains("data")) {
table.removeChild(rows[i]);
}
}
}
static logIn() {
let name = nameInput.value;
Cookies.set('name', name, { expires: 600 });
localStorage.setItem("name", name);
header.style["flex-direction"] = "row";
displayName.style.display = "flex"
productTable.style.display = "table";
spanLogout.style.display = "flex";
enterName.style.display = "none";
Application.changeDisplayName(name);
Application.clearShoppingList();
let shoppingList = ShoppingLists.getShoppingList(name);
if (shoppingList === undefined) {
console.log("Creating a new shopping list for the user.");
shoppingList = new ShoppingList(name);
ShoppingLists.saveShoppingList(shoppingList);
}
Application.showShoppingList(shoppingList);
}
static logout() {
name = "";
Cookies.set('name', "", { expires: 600 });
header.style["flex-direction"] = "column";
localStorage.setItem("name", "");
productTable.style.display = "none";
spanLogout.style.display = "none"
displayName.style.display = "none"
enterName.style.display = "flex";
}
static changeDisplayName(name) {
displayName.textContent = `${name}'s shopping list`;
}
static startUp() {
let currentUser = Cookies.get('name');
if (currentUser) {
Application.changeDisplayName(currentUser);
}
btnAccept.addEventListener("click", function () {
Application.logIn();
});
btnAdd.addEventListener("click", function () {
let productName = document.querySelector("#productName").value;
let productQuantity = Number(document.querySelector("#productQuantity").value);
if (typeof (productName) !== "string") {
throw "typeof (productName) !== \"string\""
}
if (typeof (productQuantity) !== "number") {
throw "typeof (productQuantity) !== \"number\""
}
console.log(`add name: ${productName}, ${productQuantity}`)
let sl = ShoppingLists.getShoppingList(Cookies.get('name'));
if (sl === undefined) { throw `no shopping list found for person ${name}.` }
sl.addRow(productName, productQuantity);
ShoppingLists.saveShoppingList(sl);
Application.clearShoppingList();
Application.showShoppingList(sl);
});
btnLogout.addEventListener("click", function () {
Application.logout();
})
nameInput.addEventListener("keyup", function (event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
Application.logIn();
}
});
}
}
Application.startUp();