-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
168 lines (143 loc) · 6.71 KB
/
app.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
document.addEventListener('DOMContentLoaded', () => {
const carDataDiv = document.getElementById('carData');
const advancedCarDataDiv = document.getElementById('advancedCarData');
const tokenInput = document.getElementById('tokenInput');
const getCarDataButton = document.getElementById('getCarDataButton');
const getAdvancedDataButton = document.getElementById('getAdvancedDataButton');
const getRewardsHistoryButton = document.getElementById('getRewardsHistoryButton');
const shareTwitterButton = document.getElementById('shareTwitterButton');
let carData = null;
const fetchCarData = async (tokenId) => {
try {
console.log('Fetching data for Token ID:', tokenId);
const response = await fetch('/fetchCarData', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ tokenId }),
});
if (!response.ok) {
const errorText = await response.text();
carDataDiv.innerHTML = `<p>Error: ${errorText}</p>`;
return;
}
carData = await response.json();
carDataDiv.innerHTML = `
<h2>Car Data</h2>
<p><strong>ID:</strong> ${carData.id}</p>
<p><strong>Name:</strong> ${carData.name}</p>
<p><strong>Owner:</strong> ${carData.owner}</p>
<a href="https://polygonscan.com/address/${carData.owner}" class="icon-link" target="_blank"><i class="fa-solid fa-link"></i></a>
<p><strong>Minted At:</strong> ${carData.mintedAt ? carData.mintedAt : 'N/A'}</p>
<p><strong>DCN Name:</strong> ${carData.dcn ? carData.dcn.name : 'N/A'}</p>
<p><strong>Total Tokens:</strong> ${carData.earnings ? carData.earnings.totalTokens : 'N/A'}</p>
<p><strong>Make:</strong> ${carData.definition ? carData.definition.make : 'N/A'}</p>
<p><strong>Model:</strong> ${carData.definition ? carData.definition.model : 'N/A'}</p>
<p><strong>Year:</strong> ${carData.definition ? carData.definition.year : 'N/A'}</p>
<p><strong>Definition ID:</strong> ${carData.definition ? carData.definition.id : 'N/A'}</p>
`;
shareTwitterButton.style.display = 'block';
getRewardsHistoryButton.style.display = 'block';
if (carData.definition && carData.definition.id) {
getAdvancedDataButton.style.display = 'block';
} else {
getAdvancedDataButton.style.display = 'none';
}
advancedCarDataDiv.style.display = 'none';
} catch (error) {
carDataDiv.innerHTML = `<p>Error fetching data: ${error.message}</p>`;
}
};
const fetchAdvancedCarData = async (definitionId) => {
try {
console.log('Fetching advanced data for Definition ID:', definitionId);
const response = await fetch('/fetchAdvancedCarData', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ definitionId: String(definitionId) }),
});
if (!response.ok) {
const errorText = await response.text();
advancedCarDataDiv.innerHTML = `<p>Error: ${errorText}</p>`;
return;
}
const advancedData = await response.json();
advancedCarDataDiv.innerHTML = `
<h2>Vehicle Attributes</h2>
${advancedData.attributes.map(attr => `<p><strong>${attr.name}:</strong> ${attr.value}</p>`).join('')}
`;
advancedCarDataDiv.style.display = 'block';
} catch (error) {
advancedCarDataDiv.innerHTML = `<p>Error fetching advanced data: ${error.message}</p>`;
}
};
const fetchRewardsHistory = async (tokenId) => {
try {
console.log('Fetching rewards history for Token ID:', tokenId);
const response = await fetch('/fetchRewardsHistory', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ tokenId }),
});
if (!response.ok) {
const errorText = await response.text();
carDataDiv.innerHTML = `<p>Error: ${errorText}</p>`;
return;
}
const rewardsHistory = await response.json();
advancedCarDataDiv.innerHTML = `
<h2>Rewards History</h2>
${rewardsHistory.edges.map(edge => `
<p><strong>Week:</strong> ${edge.node.week}</p>
<p><strong>Connection Streak:</strong> ${edge.node.connectionStreak}</p>
<p><strong>Streak Tokens:</strong> ${edge.node.streakTokens}</p>
<p><strong>Aftermarket Device Tokens:</strong> ${edge.node.aftermarketDeviceTokens}</p>
`).join('')}
`;
advancedCarDataDiv.style.display = 'block';
} catch (error) {
advancedCarDataDiv.innerHTML = `<p>Error fetching rewards history: ${error.message}</p>`;
}
};
const shareOnTwitter = () => {
if (!carData) {
console.error("No car data available to share.");
return;
}
const url = window.location.href;
const text = encodeURIComponent(`Check out this car data: ID ${carData.id}, Make ${carData.definition.make}, Model ${carData.definition.model}, Year ${carData.definition.year}`);
const twitterUrl = `https://twitter.com/intent/tweet?url=${url}&text=${text}`;
window.open(twitterUrl, '_blank');
};
getCarDataButton.addEventListener('click', () => {
const tokenId = tokenInput.value;
if (tokenId) {
fetchCarData(tokenId);
window.history.pushState({}, '', `/token/${tokenId}`);
}
});
getAdvancedDataButton.addEventListener('click', () => {
if (carData && carData.definition && carData.definition.id) {
fetchAdvancedCarData(carData.definition.id);
}
});
getRewardsHistoryButton.addEventListener('click', () => {
const tokenId = tokenInput.value;
if (tokenId) {
fetchRewardsHistory(tokenId);
}
});
shareTwitterButton.addEventListener('click', () => {
shareOnTwitter();
});
const tokenId = window.location.pathname.split('/').pop();
if (tokenId && tokenId !== 'token') {
tokenInput.value = tokenId;
fetchCarData(tokenId);
}
});