-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
213 lines (171 loc) · 6.74 KB
/
script.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
function addToCard(e) {
// const card_item = document.getElementsByClassName('dm_card_item');
// const card_subtotal = document.getElementsByClassName('dm_card_subtotal');
// const card_total = document.getElementsByClassName('dm_card_total');
// const card_vat = document.getElementsByClassName('dm_card_vat');
//get domain info by click add button
let Dname = e.dataset.name;
let Dprice = e.dataset.price;
let Dvat = e.dataset.vat;
let localIndex = 0;
let data = [];
let card = {
name: Dname,
price: Dprice,
qty: '1',
vat: Dvat,
};
let card2 = [{
name: Dname,
price: Dprice,
qty: '1',
vat: Dvat,
}];
let check = localStorage.getItem('domainCard'); //get localstorage data
// console.log(check = [].localStorage.getItem('card'));
// if (data = "" || data == null || data === 'null') {
// }
// else {
// // let localData = JSON.parse(localStorage.getItem('domainCard'));
// console.log(JSON.parse(localStorage.getItem('domainCard')));
// }
//set array to localStorage
if (check == null) { //if domainCard key not exist in localstorage;
// console.log('null');
localStorage.setItem('domainCard', JSON.stringify(card2));
} else { //if key exist
// console.log('not null');
data = JSON.parse(localStorage.getItem('domainCard'));
data.push(card);
localStorage.setItem('domainCard', JSON.stringify(data));
}
//get all localstorage data after set
if (check != null) {
let datas = JSON.parse(localStorage.getItem('domainCard'));
localIndex = datas.length - 1;
e.setAttribute('data-index', localIndex);
// console.log(localIndex);
}
// if (check != null && check.length > 0) {
// //for each throught localStorage data
// datas.forEach(cardItem => {
// subTotal += parseInt(cardItem.price);
// });
// }
showCheckOutInfo();
displayCardItem();
changeBtnIfInCard();
}
function showCheckOutInfo() {
let datas;
let subTotal = 0;
let vat = 0;
let total = 0;
const card_item = document.getElementsByClassName('dm_card_item');
const card_subtotal = document.getElementsByClassName('dm_card_subtotal');
const card_total = document.getElementsByClassName('dm_card_total');
const card_vat = document.getElementsByClassName('dm_card_vat');
const card_item_input = document.getElementsByClassName('dm_card_item_input');
const card_subtotal_input = document.getElementsByClassName('dm_card_subtotal_input');
const card_vat_input = document.getElementsByClassName('dm_card_vat_input');
const card_total_input = document.getElementsByClassName('dm_card_total_input');
let check = localStorage.getItem('domainCard');
if (check != null && check.length > 0) {
datas = JSON.parse(localStorage.getItem('domainCard'));
//for each throught localStorage data
datas.forEach(cardItem => {
subTotal += parseInt(cardItem.price);
vat += parseInt(cardItem.vat);
});
let withoutVat = subTotal / 100 * vat;
card_item[0].innerHTML = datas.length;
card_subtotal[0].innerHTML = "৳" + subTotal + " TK";
card_vat[0].innerHTML = vat + "% ( " + withoutVat + " TK )";
total = (subTotal - withoutVat);
card_total[0].innerHTML = "৳" + total + " TK";
card_total[1].innerHTML = "৳" + total + " TK";
// console.log(subTotal / 100 * vat);
card_item_input[0].value = datas.length;
card_subtotal_input[0].value = subTotal;
card_vat_input[0].value = vat;
card_total_input[0].value = total;
} else {
card_item[0].innerHTML = 0;
card_subtotal[0].innerHTML = 0;
card_vat[0].innerHTML = 0;
card_total[0].innerHTML = 0;
card_item_input[0].value = 'item';
card_subtotal_input[0].value = 'subtotal';
card_vat_input[0].value = 'vat';
card_total_input[0].value = 'total';
card_total[1].innerHTML = '0';
}
displayCardItem();
}
function changeBtnIfInCard() {
let check = localStorage.getItem('domainCard');
let localData = JSON.parse(check);
let plusBtn = document.getElementsByClassName('addToCard');
if (check != null) { //has data in localstorage
for (let i = 0; i < plusBtn.length; i++) {
plusBtn[i].innerHTML = ` <i class="fa-solid fa-plus"></i>`;
plusBtn[i].setAttribute('onclick', 'addToCard(this)');
// addBtn[i].innerHTML = `<i class="fa-solid fa-shoping-basket"></i>`;
}
localData.forEach(cardItem => {
let name = cardItem.name;
// console.log(document.getElementsByClassName(`'${name}'`));
if (document.getElementsByClassName(name)[0] != undefined) {
document.getElementsByClassName(name)[0].innerHTML = `<a style="color:white; font-size:12px;" href="#"><i class="fa-solid fa-cart-shopping"></i></a>`; //change plus button with shoping icon alongist anchor tag
document.getElementsByClassName(name)[0].removeAttribute('onclick'); //remove onlicck attribute
}
})
} else {
for (let i = 0; i < plusBtn.length; i++) {
plusBtn[i].innerHTML = ` <i class="fa-solid fa-plus"></i>`;
plusBtn[i].setAttribute('onclick', 'addToCard(this)');
// addBtn[i].innerHTML = `<i class="fa-solid fa-shoping-basket"></i>`;
}
}
}
function clearCard() {
localStorage.removeItem('domainCard')
changeBtnIfInCard();
showCheckOutInfo();
}
function displayCardItem() {
let data = [];
let div = '';
data = JSON.parse(localStorage.getItem('domainCard'));
if (localStorage.getItem('domainCard') != null) {
data.forEach((dm, index) => {
div +=
`
<div class='domain_item'>
<div class='name'>${dm.name}</div>
<div class='cross_btn' data-index="${index}" onclick="removeFromCard(this)">-</div>
</div>
`;
});
document.getElementsByClassName('dm_card_domain_show')[0].innerHTML = div;
} else {
document.getElementsByClassName('dm_card_domain_show')[0].innerHTML = '';
}
}
//remove a card item
function removeFromCard(e) {
let localIndex = e.dataset.index;
let localData = [];
localData = JSON.parse(localStorage.getItem('domainCard'));
localData.splice(localIndex, 1);
localStorage.setItem('domainCard', JSON.stringify(localData));
displayCardItem();
showCheckOutInfo();
changeBtnIfInCard();
// console.log(localData);
}
setTimeout(() => {
changeBtnIfInCard();
}, 1000);
displayCardItem();
showCheckOutInfo();