-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
189 lines (152 loc) · 4.8 KB
/
build.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
const logo = document.querySelector(".logo");
const main = document.getElementById("main");
const title = document.createElement("h1");
const category = document.querySelectorAll(".category");
const container = document.querySelector(".content-container");
const navLinks = document.querySelectorAll(".nav-link");
const dataArray = [];
let totalCost = 0;
let merged = [];
let order = [];
let cpus = JSON.parse( sessionStorage.getItem("cpus"));
let cases = JSON.parse( sessionStorage.getItem("cases"));
let rams = JSON.parse( sessionStorage.getItem("rams"));
let hdds = JSON.parse( sessionStorage.getItem("harddrives"));
let motherboards = JSON.parse( sessionStorage.getItem("motherboards"));
let powerSupplys = JSON.parse( sessionStorage.getItem("power_supplys"));
let gpus = JSON.parse( sessionStorage.getItem("gpus"));
const windowName = sessionStorage.getItem("name");
console.log(windowName);
const render = () => {
configureTitle();
displayAllData();
goToCart();
};
const mapData = (name) => {
const filteredCpus = [];
const filteredMotherbaords = [];
cpus.forEach((cpu) => {
cpu.name[0].toUpperCase() === name[0].toUpperCase() &&
filteredCpus.push(cpu);
});
motherboards.forEach((board) => {
board.characteristics.model[0].toUpperCase() === name[0].toUpperCase() &&
filteredMotherbaords.push(board);
});
dataArray.push(filteredCpus);
dataArray.push(filteredMotherbaords);
dataArray.push(cases);
dataArray.push(powerSupplys);
dataArray.push(rams);
dataArray.push(hdds);
dataArray.push(gpus);
merged = [].concat.apply([], dataArray);
};
const toggleCategory = () => {
category.forEach((categ) => {
categ.addEventListener("click", (e) => {
if (e.target.classList[0] === "category-title") {
e.target.classList.toggle("active");
categ.children[1] && categ.children[1].classList.toggle("active");
}
});
});
};
const configureTitle = () => {
title.innerText = windowName.toUpperCase();
title.classList.add("title");
main.insertBefore(title, main.firstChild);
};
const createContent = (list, index) => {
const contentContainer = document.createElement("div");
contentContainer.classList.add("content-container");
list.forEach((item) => {
createCard(item, contentContainer, index);
});
};
const createCard = (item, contentContainer, index) => {
const card = document.createElement("div");
card.classList.add("card");
const div = document.createElement("div");
div.innerHTML = `<img class = "card-img" src= "${item.poster_path}" alt= "pc part" />`;
const details = document.createElement("div");
details.classList.add("details");
const name = document.createElement("p");
name.classList.add("name");
name.innerText = `Name : ${item.name}`;
const ul = document.createElement("ul");
ul.innerText = "Characteristics";
for (key in item.characteristics) {
const li = document.createElement("li");
li.innerText = `${key} : ${item.characteristics[key]}`;
ul.appendChild(li);
}
const input = document.createElement("input");
input.type = "radio";
input.classList.add("input");
input.innerHTML = item.price;
input.name = item.type;
input.attributes.required = "required";
input.addEventListener("click", ()=>{
const product = findItem(item.name);
order = order.filter((orderItem) => {
return orderItem.type !== product.type;
})
order.push(item)
calculateCost();
})
const label = document.createElement("label");
label.setAttribute("for", item.type);
label.classList.add("price-label");
label.innerText = `Price : ${item.price}€`;
details.appendChild(name);
details.appendChild(ul);
details.appendChild(input);
details.appendChild(label);
card.appendChild(div);
card.appendChild(details);
contentContainer.appendChild(card);
category[index].appendChild(contentContainer);
};
const displayAllData = () => {
mapData(windowName);
for (let i = 0; i < dataArray.length; i++) {
createContent(dataArray[i], i);
}
toggleCategory();
};
const calculateCost = () => {
totalCost = 0;
order.forEach(item => {
totalCost += item.price;
})
console.log(totalCost);
console.log(order);
}
const findItem = (name) => {
return merged.filter(item => {
return name === item.name;
})[0];
}
const checkRequired = () => {
if(order.length < 7){
alert("YOU HAVE TO CHOOSE ONE PRODUCT FROM EACH CATEGORY");
return false;
}
return true;
}
const goToCart = () => {
let flag = false;
const form = document.querySelector(".form");
form.addEventListener("submit", (e) => {
e.preventDefault();
console.log(flag);
flag = checkRequired();
if(flag){
sessionStorage.setItem("orderJSON", JSON.stringify(order));
sessionStorage.setItem("cost", totalCost);
location.href = "cart.html";
}
})
}
render();