-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreditstatus.js
41 lines (37 loc) · 1.22 KB
/
creditstatus.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
// creditStatus.js
export class CreditStatus {
constructor(container) {
this.container = container;
this.setup();
}
setup() {
this.statusBar = document.createElement('div');
this.statusBar.className = 'credit-status';
this.statusBar.innerHTML = `
<div class="credit-balance">
<span class="credit-amount"></span> credits
</div>
<div class="credit-warning hidden">
<button class="get-credits-btn">Get More Credits</button>
</div>
`;
this.container.appendChild(this.statusBar);
this.updateCredits();
}
updateCredits(amount) {
fetch('/auth/user')
.then(res => res.json())
.then(data => {
const credits = data.user.credits;
this.statusBar.querySelector('.credit-amount').textContent = credits;
// Show warning if credits are low
if (credits < 100) {
this.showWarning();
}
});
}
showWarning() {
const warning = this.statusBar.querySelector('.credit-warning');
warning.classList.remove('hidden');
}
}