-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProduct_Offers.html
More file actions
118 lines (111 loc) · 5.2 KB
/
Product_Offers.html
File metadata and controls
118 lines (111 loc) · 5.2 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Zinc API Examples</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<link href="./loading.css" rel="stylesheet">
<style>
html,
body {
height: 100%;
padding: 0;
margin: 0;
font-size: 16px;
}
</style>
</head>
<body>
<div class="loading">Loading…</div>
<div id="explore" class="container-fluid p-0 d-flex">
<div class="container content">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-4 align-items-center d-flex">
<img id="image" class="d-block w-100" src="#">
</div>
<div class="col-8 align-items-center d-flex">
<h5 id="title"></h5>
</div>
</div>
<div class="row mt-2">
<div id="offers" class="list-group">
</div>
</div>
</div>
</div>
</div>
</div>
<script>
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
return null;
}
async function getProductDetails(productId) {
const headers = new Headers();
if(localStorage.getItem('ct')){
const clientToken = localStorage.getItem('ct')
headers.set('Authorization', 'Basic ' + btoa(clientToken + ":")); //this is how you authenticate using your client token
} else {
headers.set('Authorization', 'Basic ' + btoa("demouser:superdupersecret")); //demo auth
}
const response = await fetch(`https://api.zinc.io/v1/products/${productId}?retailer=amazon&max_age=3600`, { headers });
const json = await response.json();
console.log('product details', json);
return json;
}
async function getProductOffers(productId) {
const headers = new Headers();
if (localStorage.getItem('ct')) {
const clientToken = localStorage.getItem('ct')
headers.set('Authorization', 'Basic ' + btoa(clientToken + ":")); //this is how you authenticate using your client token
} else {
headers.set('Authorization', 'Basic ' + btoa("demouser:superdupersecret")); //demo auth
}
const response = await fetch(`https://api.zinc.io/v1/products/${productId}/offers?retailer=amazon&max_age=3600`, { headers });
const json = await response.json();
console.log('product offers', json);
return json;
}
function updateProductDetailsUI(details, offers) {
if(details.status === "failed"){
alert(`${details.code}: ${details.message}`);
return;
}
$(".loading").fadeOut(500)
$("#image").attr('src', details.main_image)
$("#title").text(details.title)
$("#brand").text(details.brand)
for (const offer of offers.offers) {
$('#offers').append(`
<div id="offers" class="list-group">
<a href="https://www.amazon.com/sp?seller=${offer.seller.id}" rel="noreferrer noopener" target="_blank" class="list-group-item list-group-item-action" aria-current="true">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">${offer.seller.name} - ${offer.condition}</h5>
<small>$${(offer.price/100).toFixed(2)}</small>
</div>
<small>${offer.greytext}</small>
</a>
</div>
`)
}
}
//get data and update ui
const productDetailsPromise = getProductDetails(getQueryVariable('pid')||'B07VQ69WP5');
const productOffersPromise = getProductOffers(getQueryVariable('pid')||'B07VQ69WP5');
document.addEventListener("DOMContentLoaded", async () => updateProductDetailsUI(await productDetailsPromise, await productOffersPromise))
</script>
</body>
</html>