Skip to content

Commit 03b4f6a

Browse files
committed
get data for warehouse client from backend
1 parent 4fadb5b commit 03b4f6a

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

warehouse-client/main.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
import { items as rows } from "./mock.js";
1+
const BASE_URL = "http://localhost:8082/";
2+
async function fetchData() {
3+
const res = await fetch(`${BASE_URL}/api/shipment`);
4+
const shipments = await res.json();
5+
return shipments;
6+
}
27

3-
function main() {
4-
console.log(JSON.stringify(rows));
5-
document.getElementById("root").innerHTML = `<div class="grid-container">
8+
async function main() {
9+
try {
10+
document.getElementById("root").innerHTML = `<h4>Loading...</h4>`;
11+
const rows = await fetchData();
12+
console.log(JSON.stringify(rows));
13+
document.getElementById("root").innerHTML = `<div class="grid-container">
614
${rows
715
.map((row) => {
816
const {
9-
cart: { _id, items, address, email, phone },
17+
cart: { cartId: _id, items, address, email, phone },
1018
shipped,
1119
paid,
1220
} = row;
@@ -36,6 +44,10 @@ function main() {
3644
})
3745
.join("")}
3846
</div>`;
47+
} catch (err) {
48+
console.log(err);
49+
document.getElementById("root").innerHTML = `<h4>Failed to load data!</h4>`;
50+
}
3951
}
4052

4153
main();

warehouse-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"http-server": "^0.12.3"
44
},
55
"scripts": {
6-
"start" : "npx http-server ."
6+
"start" : "npx http-server . -p 3001"
77
}
88
}

0 commit comments

Comments
 (0)