-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauction.js
202 lines (173 loc) · 5.14 KB
/
auction.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
const awaitLoadingCompleted = () => {
const checkLoadingIconVisibility = () => {
let loading = document.getElementById("loader-wrapper").style.display;
if (loading !== "none") {
setTimeout(() => {
loading = document.getElementById("loader-wrapper").style.display;
checkLoadingIconVisibility();
}, 1000);
} else {
console.info("done loading");
setMarkers();
setPaginationListener();
}
};
checkLoadingIconVisibility();
};
const setMarkers = () => {
const getItemStatus = (info) => {
if (info.includes("item is new")) {
return 1;
} else if (info.includes("as-is")) {
return 2;
} else if (info.includes("no obvious damage!")) {
return 3;
} else {
return 0;
}
};
const appendItemMarker = (element, background, textContent) => {
let newItemMarker = document.createElement("div");
const elementToInsertBefore = element.querySelector(".carousel");
const classes = [
"card",
"rounded-pill",
"py-2",
"my-2",
"lead",
"text-center",
"extension-marker",
];
newItemMarker.classList.add(...classes, background);
newItemMarker.textContent = textContent;
elementToInsertBefore.parentElement.insertBefore(
newItemMarker,
elementToInsertBefore
);
};
const auctionItems = document.getElementsByClassName(
"row pb-3 mt-2 border-bottom border auction-item-cardcolor"
);
Array.from(auctionItems).forEach((item) => {
const itemInformation = item
.querySelector(".tooltip-demos")
.innerHTML.toLowerCase();
const itemStatus = getItemStatus(itemInformation);
let background = "",
textContent = "";
switch (itemStatus) {
case 1:
background = "bg-primary";
textContent = "New";
break;
case 2:
background = "bg-secondary";
textContent = "As-Is";
break;
case 3:
background = "bg-warning";
textContent = "No Obvious Damage";
break;
default:
background = "bg-danger";
textContent = "Other";
break;
}
appendItemMarker(item, background, textContent);
});
};
const toggleViewedStyles = (element) => {
const viewedColor = "bg-primary";
const notViewedColor = "bg-secondary";
if (element.classList.contains(viewedColor)) {
element.classList.remove(viewedColor);
element.classList.add(notViewedColor);
element.textContent = "Not Viewed";
} else {
element.classList.remove(notViewedColor);
element.classList.add(viewedColor);
element.textContent = "Viewed";
}
};
const getAuctionId = () => {
const params = new URLSearchParams(window.location.search);
return encodeURIComponent(params.get("AuctionId"))
.substring(0, 10)
.toLowerCase();
};
const updateDbEntryViewedStatus = () => {
const auctionId = getAuctionId();
const req = window.indexedDB.open("capitalCityDb", 1);
req.onsuccess = function (event) {
const db = event.target.result;
const transaction = db.transaction(["auctionStore"], "readwrite");
const auctionStore = transaction.objectStore("auctionStore");
const getReq = auctionStore.get(auctionId);
getReq.onsuccess = (event) => {
let entry = event.target.result;
if (entry) {
entry.viewed = !entry.viewed;
const updateReq = auctionStore.put(entry);
updateReq.onsuccess = function () {
console.log("Updated db key: " + auctionId);
};
}
};
};
};
const addViewedButton = () => {
const parent = document.querySelector("#divAuctionTotalList");
const classes = [
"text-center",
"h2",
"py-2",
"my-2",
"bg-secondary",
"card",
"rounded-pill",
];
const customStyles = `cursor: pointer`;
let viewedBtn = document.createElement("div");
viewedBtn.setAttribute("style", customStyles);
viewedBtn.id = "viewedButton";
viewedBtn.classList.add(...classes);
viewedBtn.innerText = "Not Viewed";
const auctionId = getAuctionId();
const req = window.indexedDB.open("capitalCityDb", 1);
req.onsuccess = function (event) {
const db = event.target.result;
const transaction = db.transaction(["auctionStore"], "readwrite");
const auctionStore = transaction.objectStore("auctionStore");
const getReq = auctionStore.get(auctionId);
getReq.onsuccess = function (event) {
const viewed = event.target.result.viewed;
if (viewed) {
toggleViewedStyles(viewedBtn);
viewedBtn.innerText = "Viewed";
}
};
};
parent.appendChild(viewedBtn);
viewedBtn.addEventListener("click", function () {
updateDbEntryViewedStatus();
toggleViewedStyles(viewedBtn);
});
};
const setPaginationListener = () => {
document.querySelectorAll(".page-item").forEach((element) => {
element.addEventListener("click", () => {
const hasMarkers =
document.getElementsByClassName("extension-marker").length != 0;
if (hasMarkers) {
setTimeout(() => {
setMarkers();
setPaginationListener();
}, 1000);
}
});
});
};
document.addEventListener("DOMContentLoaded", [
awaitLoadingCompleted(),
addViewedButton(),
]);