feat: add vehicle type assignment to vehicle edit/create form
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
import React, { useEffect, useState, useCallback } from 'react';
|
import React, { useEffect, useState, useCallback } from 'react';
|
||||||
import {
|
import {
|
||||||
Alert,
|
Alert,
|
||||||
|
Autocomplete,
|
||||||
Box,
|
Box,
|
||||||
Button,
|
Button,
|
||||||
|
Chip,
|
||||||
CircularProgress,
|
CircularProgress,
|
||||||
Container,
|
Container,
|
||||||
Grid,
|
Grid,
|
||||||
@@ -15,6 +17,8 @@ import { useNavigate, useParams } from 'react-router-dom';
|
|||||||
import DashboardLayout from '../components/dashboard/DashboardLayout';
|
import DashboardLayout from '../components/dashboard/DashboardLayout';
|
||||||
import GermanDateField from '../components/shared/GermanDateField';
|
import GermanDateField from '../components/shared/GermanDateField';
|
||||||
import { vehiclesApi } from '../services/vehicles';
|
import { vehiclesApi } from '../services/vehicles';
|
||||||
|
import { fahrzeugTypenApi } from '../services/fahrzeugTypen';
|
||||||
|
import type { FahrzeugTyp } from '../types/checklist.types';
|
||||||
import {
|
import {
|
||||||
CreateFahrzeugPayload,
|
CreateFahrzeugPayload,
|
||||||
UpdateFahrzeugPayload,
|
UpdateFahrzeugPayload,
|
||||||
@@ -78,12 +82,22 @@ function FahrzeugForm() {
|
|||||||
const [saveError, setSaveError] = useState<string | null>(null);
|
const [saveError, setSaveError] = useState<string | null>(null);
|
||||||
const [fieldErrors, setFieldErrors] = useState<Partial<Record<keyof FormState, string>>>({});
|
const [fieldErrors, setFieldErrors] = useState<Partial<Record<keyof FormState, string>>>({});
|
||||||
|
|
||||||
|
const [allTypes, setAllTypes] = useState<FahrzeugTyp[]>([]);
|
||||||
|
const [selectedTypes, setSelectedTypes] = useState<FahrzeugTyp[]>([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fahrzeugTypenApi.getAll().then(setAllTypes).catch(() => {});
|
||||||
|
}, []);
|
||||||
|
|
||||||
const fetchVehicle = useCallback(async () => {
|
const fetchVehicle = useCallback(async () => {
|
||||||
if (!id) return;
|
if (!id) return;
|
||||||
try {
|
try {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
const vehicle = await vehiclesApi.getById(id);
|
const [vehicle, types] = await Promise.all([
|
||||||
|
vehiclesApi.getById(id),
|
||||||
|
fahrzeugTypenApi.getTypesForVehicle(id).catch(() => [] as FahrzeugTyp[]),
|
||||||
|
]);
|
||||||
setForm({
|
setForm({
|
||||||
bezeichnung: vehicle.bezeichnung,
|
bezeichnung: vehicle.bezeichnung,
|
||||||
kurzname: vehicle.kurzname ?? '',
|
kurzname: vehicle.kurzname ?? '',
|
||||||
@@ -98,6 +112,7 @@ function FahrzeugForm() {
|
|||||||
paragraph57a_faellig_am: toDateInput(vehicle.paragraph57a_faellig_am),
|
paragraph57a_faellig_am: toDateInput(vehicle.paragraph57a_faellig_am),
|
||||||
naechste_wartung_am: toDateInput(vehicle.naechste_wartung_am),
|
naechste_wartung_am: toDateInput(vehicle.naechste_wartung_am),
|
||||||
});
|
});
|
||||||
|
setSelectedTypes(types);
|
||||||
} catch {
|
} catch {
|
||||||
setError('Fahrzeug konnte nicht geladen werden.');
|
setError('Fahrzeug konnte nicht geladen werden.');
|
||||||
} finally {
|
} finally {
|
||||||
@@ -162,6 +177,7 @@ function FahrzeugForm() {
|
|||||||
naechste_wartung_am: form.naechste_wartung_am || null,
|
naechste_wartung_am: form.naechste_wartung_am || null,
|
||||||
};
|
};
|
||||||
await vehiclesApi.update(id, payload);
|
await vehiclesApi.update(id, payload);
|
||||||
|
await fahrzeugTypenApi.setTypesForVehicle(id, selectedTypes.map((t) => t.id));
|
||||||
navigate(`/fahrzeuge/${id}`);
|
navigate(`/fahrzeuge/${id}`);
|
||||||
} else {
|
} else {
|
||||||
const payload: CreateFahrzeugPayload = {
|
const payload: CreateFahrzeugPayload = {
|
||||||
@@ -179,6 +195,7 @@ function FahrzeugForm() {
|
|||||||
naechste_wartung_am: form.naechste_wartung_am || undefined,
|
naechste_wartung_am: form.naechste_wartung_am || undefined,
|
||||||
};
|
};
|
||||||
const newVehicle = await vehiclesApi.create(payload);
|
const newVehicle = await vehiclesApi.create(payload);
|
||||||
|
await fahrzeugTypenApi.setTypesForVehicle(newVehicle.id, selectedTypes.map((t) => t.id));
|
||||||
navigate(`/fahrzeuge/${newVehicle.id}`);
|
navigate(`/fahrzeuge/${newVehicle.id}`);
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@@ -268,6 +285,24 @@ function FahrzeugForm() {
|
|||||||
placeholder="z.B. WN-FW 1"
|
placeholder="z.B. WN-FW 1"
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
<Grid item xs={12}>
|
||||||
|
<Autocomplete
|
||||||
|
multiple
|
||||||
|
options={allTypes}
|
||||||
|
getOptionLabel={(o) => o.name}
|
||||||
|
value={selectedTypes}
|
||||||
|
onChange={(_e, val) => setSelectedTypes(val)}
|
||||||
|
isOptionEqualToValue={(a, b) => a.id === b.id}
|
||||||
|
renderTags={(value, getTagProps) =>
|
||||||
|
value.map((option, index) => (
|
||||||
|
<Chip label={option.name} size="small" {...getTagProps({ index })} key={option.id} />
|
||||||
|
))
|
||||||
|
}
|
||||||
|
renderInput={(params) => (
|
||||||
|
<TextField {...params} label="Fahrzeugtypen" placeholder={selectedTypes.length === 0 ? 'Typen auswählen…' : ''} />
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
<Typography variant="h6" gutterBottom sx={{ mt: 3 }}>Prüf- und Wartungsfristen</Typography>
|
<Typography variant="h6" gutterBottom sx={{ mt: 3 }}>Prüf- und Wartungsfristen</Typography>
|
||||||
|
|||||||
Reference in New Issue
Block a user