-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
112 lines (96 loc) · 3.17 KB
/
script.js
File metadata and controls
112 lines (96 loc) · 3.17 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
const baseUrl = 'https://my-own-counter-api-production.up.railway.app';
function showLoading() {
document.getElementById('result').innerHTML = `
<div class="result-content">
<i class="fas fa-spinner fa-spin"></i> Processing...
</div>
`;
}
function showError(error) {
document.getElementById('result').innerHTML = `
<div class="result-content" style="color: var(--error-color)">
<i class="fas fa-exclamation-circle"></i> ${error}
</div>
`;
}
function showSuccess(message) {
document.getElementById('result').innerHTML = `
<div class="result-content" style="color: var(--success-color)">
<i class="fas fa-check-circle"></i> ${message}
</div>
`;
}
function createOrIncrementCounter() {
const namespace = document.getElementById('namespace').value;
const key = document.getElementById('key').value;
if (!namespace || !key) {
showError('Please provide both namespace and key');
return;
}
showLoading();
fetch(`${baseUrl}/hit/${namespace}/${key}`, {
method: 'POST'
})
.then(response => response.json())
.then(data => {
showSuccess(`Counter incremented: ${JSON.stringify(data)}`);
})
.catch(error => {
showError(`Error: ${error}`);
});
}
function getCounterValue() {
const namespace = document.getElementById('namespace').value;
const key = document.getElementById('key').value;
if (!namespace || !key) {
showError('Please provide both namespace and key');
return;
}
showLoading();
fetch(`${baseUrl}/get/${namespace}/${key}`, {
method: 'GET'
})
.then(response => response.json())
.then(data => {
showSuccess(`Counter value: ${JSON.stringify(data)}`);
})
.catch(error => {
showError(`Error: ${error}`);
});
}
function setCounterValue() {
const namespace = document.getElementById('namespace').value;
const key = document.getElementById('key').value;
const newValue = document.getElementById('newValue').value;
if (!namespace || !key || !newValue) {
showError('Please provide namespace, key, and new value');
return;
}
showLoading();
fetch(`${baseUrl}/set/${namespace}/${key}?value=${newValue}`, {
method: 'PUT'
})
.then(response => response.json())
.then(data => {
showSuccess(`Counter set: ${JSON.stringify(data)}`);
})
.catch(error => {
showError(`Error: ${error}`);
});
}
// Theme switcher
function initTheme() {
const savedTheme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);
}
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'light' ? 'dark' : 'light';
document.documentElement.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
}
// Add event listener when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
initTheme();
document.getElementById('themeToggle').addEventListener('click', toggleTheme);
});