The trip files have been reorganized. Errors related to vehicle and driver editing have been resolved, archive display issues fixed, and model data display standardized across the project; order and trip-related issues have also been addressed.

This commit is contained in:
m7amedez5511
2026-07-22 14:19:32 +03:00
parent f1a521dd5a
commit b87d81100f
59 changed files with 2261 additions and 3261 deletions

View File

@@ -89,40 +89,26 @@ export function CarFormModal({
}: CarFormModalProps) {
const isNew = editCar === null;
// ── Local branches (auto-fetched if prop is empty) ─────────────────────────
const [branches, setBranches] = useState<Branch[]>(branchesProp);
const loadBranches = useCallback(() => {
if (branchesProp.length > 0) {
queueMicrotask(() => setBranches(branchesProp));
return;
}
const token = getStoredToken();
branchService
.getOptions(token)
.then((list) => {
queueMicrotask(() => setBranches(list as unknown as Branch[]));
})
.catch(() => {
/* silently ignore */
});
}, [branchesProp]);
useEffect(() => {
loadBranches();
}, [loadBranches]);
// ── react-hook-form ────────────────────────────────────────────────────────
// Moved above the branches-loading effect below: that effect calls
// setValue(), so useForm() (which defines it) must run first — otherwise
// setValue is referenced before its own initialization (TDZ ReferenceError),
// same issue already fixed in TripFormModal / DriverFormModal.
const {
register,
handleSubmit,
setError,
setFocus,
setValue,
formState: { errors, isSubmitting },
} = useForm<CarFormValues>({
// The create/update schemas have structurally different required fields,
// so yup's inferred types don't unify — cast the schema itself (not the
// yupResolver() call) to sidestep the mismatch.
resolver: yupResolver<CarFormValues>((isNew ? createCarSchema : updateCarSchema) as any),
// Cast both the schema argument and the resolver's result — same fix
// already applied in TripFormModal/DriverFormModal: createCarSchema/
// updateCarSchema have structurally different required fields (union
// type yupResolver's single-schema signature won't accept), and yup's
// inferred type for .optional() fields is structurally incompatible
// with CarFormValues.
resolver: yupResolver((isNew ? createCarSchema : updateCarSchema) as any) as any,
defaultValues: {
manufacturer: editCar?.manufacturer ?? "",
model: editCar?.model ?? "",
@@ -148,6 +134,39 @@ export function CarFormModal({
},
});
// ── Local branches (auto-fetched if prop is empty) ─────────────────────────
const [branches, setBranches] = useState<Branch[]>(branchesProp);
const loadBranches = useCallback(() => {
// defaultValues.branchId was applied before this list existed, so the
// <select> had no matching <option> yet and silently fell back to "" —
// reapply the saved id now that the option actually exists in the DOM.
const savedBranchId = editCar?.branch?.id;
if (branchesProp.length > 0) {
queueMicrotask(() => {
setBranches(branchesProp);
if (savedBranchId) setValue("branchId", savedBranchId);
});
return;
}
const token = getStoredToken();
branchService
.getOptions(token)
.then((list) => {
queueMicrotask(() => {
setBranches(list as unknown as Branch[]);
if (savedBranchId) setValue("branchId", savedBranchId);
});
})
.catch(() => {
/* silently ignore */
});
}, [branchesProp, editCar, setValue]);
useEffect(() => {
loadBranches();
}, [loadBranches]);
const [apiError, setApiError] = useState("");
// register("manufacturer") already provides a ref for this input, so we