led-controller/static/ApiService.js

84 lines
2.0 KiB
JavaScript

export class ApiService {
static async loadSettings() {
try {
const response = await fetch("/api/settings");
const settings = await response.json();
console.log("Settings loaded:", settings);
return settings;
} catch (error) {
console.error("Failed to load settings:", error);
return {};
}
}
static async createBar(barData) {
const response = await fetch("/api/settings/create", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(barData),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail);
}
return response.json();
}
static async deleteBar(barId) {
const response = await fetch("/api/settings/delete", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ barId }),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail);
}
return response.json();
}
static async saveColor(barId, color) {
try {
const response = await fetch("/api/settings/color", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ barId, color }),
});
if (response.ok) {
console.log(`Color saved for ${barId}: ${color}`);
}
} catch (error) {
console.error("Failed to save color:", error);
}
}
static async savePosition(barId, x, y) {
try {
const response = await fetch("/api/settings/position", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ barId, x, y }),
});
if (response.ok) {
console.log(`Position saved for ${barId}: ${x}, ${y}`);
}
} catch (error) {
console.error("Failed to save position:", error);
}
}
}