aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/routes/(admin)/admin
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/routes/(admin)/admin')
-rw-r--r--frontend/src/routes/(admin)/admin/+page.server.js8
-rw-r--r--frontend/src/routes/(admin)/admin/+page.svelte49
2 files changed, 45 insertions, 12 deletions
diff --git a/frontend/src/routes/(admin)/admin/+page.server.js b/frontend/src/routes/(admin)/admin/+page.server.js
index bdf498b..f0a6f44 100644
--- a/frontend/src/routes/(admin)/admin/+page.server.js
+++ b/frontend/src/routes/(admin)/admin/+page.server.js
@@ -1,11 +1,13 @@
-const API = 'http://localhost:8080/api';
+import { building } from '$app/environment';
+
+const API_BASE = building ? 'http://localhost:8080/api' : '/api';
export async function load({ locals }) {
const token = locals.token;
const headers = token ? { Authorization: `Bearer ${token}` } : {};
- const res = await fetch(`${API}/services`, { headers });
+ const res = await fetch(`${API_BASE}/services`, { headers });
if (!res.ok) {
return { services: [], error: 'Falha ao carregar serviços', token: token ?? '' };
}
@@ -14,7 +16,7 @@ export async function load({ locals }) {
const servicesWithStats = await Promise.all(
services.map(async (svc) => {
try {
- const statsRes = await fetch(`${API}/services/${svc.id}/stats`, { headers });
+ const statsRes = await fetch(`${API_BASE}/services/${svc.id}/stats`, { headers });
if (statsRes.ok) {
svc.stats = await statsRes.json();
}
diff --git a/frontend/src/routes/(admin)/admin/+page.svelte b/frontend/src/routes/(admin)/admin/+page.svelte
index 7fa361d..9a48ba0 100644
--- a/frontend/src/routes/(admin)/admin/+page.svelte
+++ b/frontend/src/routes/(admin)/admin/+page.svelte
@@ -1,5 +1,7 @@
<script lang="ts">
- const API = 'http://localhost:8080/api';
+ import { onMount } from 'svelte';
+
+ const API = '/api';
let { data } = $props();
@@ -8,15 +10,42 @@
let services = $state<any[]>([]);
let showForm = $state(false);
- $effect(() => {
- if (!initialized && data) {
- token = data.token;
- services = data.services ?? [];
- initialized = true;
- fetchMaintenances();
- fetchServerStats();
+ function getCookie(name: string) {
+ return document.cookie.split('; ').find(r => r.startsWith(name + '='))?.split('=')[1] ?? '';
+ }
+
+ async function bootstrap() {
+ if (initialized) return;
+ initialized = true;
+ token = data?.token || getCookie('auth_token') || '';
+ if (data?.services && data.services.length > 0) {
+ services = data.services;
+ } else {
+ await fetchServices();
}
- });
+ fetchMaintenances();
+ fetchServerStats();
+ }
+
+ async function fetchServices() {
+ try {
+ const res = await fetch(`${API}/services`, { headers: { Authorization: `Bearer ${token}` } });
+ if (await handleUnauthorized(res)) return;
+ if (res.ok) {
+ const svcs = await res.json();
+ const withStats = await Promise.all(
+ svcs.map(async (svc: any) => {
+ try {
+ const sr = await fetch(`${API}/services/${svc.id}/stats`, { headers: { Authorization: `Bearer ${token}` } });
+ if (sr.ok) svc.stats = await sr.json();
+ } catch { /* ignore */ }
+ return svc;
+ })
+ );
+ services = withStats;
+ }
+ } catch { /* ignore */ }
+ }
let editingSvc: any = $state(null);
let saving = $state(false);
let formError = $state('');
@@ -24,6 +53,8 @@
let toggling = $state<Record<number, boolean>>({});
// maintenance state
+ onMount(bootstrap);
+
let maintenances = $state<any[]>([]);
let showMtnForm = $state(false);
let editingMtn: any = $state(null);