54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
'use server';
|
|
|
|
import { BACKEND_URL } from '@/_config/backend';
|
|
import { revalidatePath } from 'next/cache';
|
|
import type { UnitMaterialSettings, MaterialSensor } from '@/types/anlagen/materialfluss';
|
|
|
|
export interface UnitMaterialData {
|
|
anlageId: string;
|
|
version: number;
|
|
settings: UnitMaterialSettings;
|
|
sensors: MaterialSensor[];
|
|
}
|
|
|
|
// Ohne Daten wird gelesen (GET), mit Daten gespeichert (PUT).
|
|
async function request(id: string, data?: UnitMaterialData): Promise<UnitMaterialData> {
|
|
const requestUrl = `${BACKEND_URL}/rk2/units/${encodeURIComponent(id)}/materialfluss`;
|
|
const method = data ? 'PUT' : 'GET';
|
|
|
|
const body = data
|
|
? JSON.stringify({
|
|
anlageId: data.anlageId,
|
|
version: data.version,
|
|
settings: data.settings,
|
|
})
|
|
: undefined;
|
|
|
|
const response = await fetch(requestUrl, {
|
|
method,
|
|
cache: 'no-store',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body,
|
|
});
|
|
|
|
const result = await response.json();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(result.message ?? 'Unit-Materialfluss konnte nicht gespeichert werden.');
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
export async function api_unitMaterial(id: string) {
|
|
return request(id);
|
|
}
|
|
|
|
export async function api_saveUnitMaterial(id: string, data: UnitMaterialData) {
|
|
const result = await request(id, data);
|
|
|
|
revalidatePath('/ablaeufe');
|
|
|
|
return result;
|
|
}
|