29 lines
1.5 KiB
TypeScript
29 lines
1.5 KiB
TypeScript
'use server';
|
|
import type { ProConConnections } from '@/types/rk/procon-verbindungen';
|
|
|
|
const url = (id: string) => 'http://localhost:3000/anlage/' + encodeURIComponent(id) + '/procon-verbindungen';
|
|
export async function api_connections(id: string): Promise<ProConConnections> {
|
|
const response = await fetch(url(id), { cache: 'no-store' });
|
|
if (!response.ok) throw new Error('Verbindungen konnten nicht geladen werden.');
|
|
return response.json();
|
|
}
|
|
export async function api_connections_save(data: ProConConnections): Promise<{ success: true; data: ProConConnections } | { success: false; error: string }> {
|
|
try {
|
|
const response = await fetch(url(data.anlageId), {
|
|
method: 'PUT', headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ verbindungen: data.verbindungen, aktiveVerbindungId: data.aktiveVerbindungId }),
|
|
});
|
|
const result = await response.json();
|
|
if (!response.ok) return { success: false, error: result.message ?? 'Speichern fehlgeschlagen' };
|
|
return { success: true, data: result };
|
|
} catch { return { success: false, error: 'Keine Verbindung zum Backend.' }; }
|
|
}
|
|
export async function api_connections_test(anlageId: string, connectionId: string) {
|
|
try {
|
|
const response = await fetch(url(anlageId) + '/' + encodeURIComponent(connectionId) + '/test', { method: 'POST' });
|
|
const result = await response.json();
|
|
return { success: response.ok, message: result.message ?? 'Verbindungstest fehlgeschlagen' };
|
|
} catch { return { success: false, message: 'Keine Verbindung zum Backend.' }; }
|
|
}
|
|
|