Skip to content

Commit 80bcf61

Browse files
committed
refactor
1 parent d5bea22 commit 80bcf61

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

warehouse-client/main.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ async function fetchData() {
55
return shipments;
66
}
77

8+
function generateBtnId(cartId) {
9+
return `btn-${cartId}`;
10+
}
11+
812
function renderData(rows) {
9-
const buttonIds = [];
1013
document.getElementById("root").innerHTML = `<div class="grid-container">
1114
${rows
1215
.map((row) => {
@@ -15,12 +18,12 @@ ${rows
1518
shipped,
1619
paid,
1720
} = row;
18-
const curBtnId = `btn-${cartId}`;
19-
buttonIds.push(curBtnId);
21+
2022
// TODO: do this from the backend
2123
if (shipped) {
2224
return "";
2325
}
26+
2427
return `
2528
<div id=${cartId} class="grid-item">
2629
<h2><code>${cartId}</code>\t${paid ? "Paid" : "Pending"}</h2>
@@ -37,16 +40,23 @@ ${rows
3740
)
3841
.join("")}
3942
</ul>
40-
<button ${paid ? "" : "disabled"} id="${curBtnId}">Ship it!</button>
43+
<button ${paid ? "" : "disabled"} id="${generateBtnId(
44+
cartId
45+
)}">Ship it!</button>
4146
</div>
4247
`;
4348
})
4449
.join("")}
4550
</div>`;
46-
return buttonIds;
4751
}
4852

49-
function attachBtnHandlers(buttonIds) {
53+
function attachBtnHandlers(rows) {
54+
const buttonIds = rows.map((row) => {
55+
const {
56+
cart: { cartId },
57+
} = row;
58+
return generateBtnId(cartId);
59+
});
5060
buttonIds.forEach((btnId) => {
5161
document.getElementById(btnId).onclick = () => alert(`${btnId} Clicked`);
5262
});
@@ -57,7 +67,8 @@ async function main() {
5767
document.getElementById("root").innerHTML = `<h4>Loading...</h4>`;
5868
const rows = await fetchData();
5969
console.log(JSON.stringify(rows));
60-
attachBtnHandlers(renderData(rows));
70+
renderData(rows);
71+
attachBtnHandlers(rows);
6172
} catch (err) {
6273
console.log(err);
6374
document.getElementById("root").innerHTML = `<h4>Failed to load data!</h4>`;

0 commit comments

Comments
 (0)