corrreciones al formulario de las osp
This commit is contained in:
@@ -45,6 +45,7 @@ import {
|
||||
CardTitle,
|
||||
} from '@repo/shadcn/components/ui/card';
|
||||
import React from 'react';
|
||||
import { SelectSearchable } from '@repo/shadcn/components/ui/select-searchable';
|
||||
|
||||
const OSP_TYPES = ['EPSIC', 'EPSDC', 'UPF', 'OTROS', 'COOPERATIVA'];
|
||||
const STATUS_OPTIONS = ['ACTIVA', 'INACTIVA'];
|
||||
@@ -158,10 +159,14 @@ export function CreateTrainingForm({
|
||||
equipmentList: defaultValues?.equipmentList || [],
|
||||
productionList: defaultValues?.productionList || [],
|
||||
productList: defaultValues?.productList || [],
|
||||
state: defaultValues?.state || undefined,
|
||||
municipality: defaultValues?.municipality || undefined,
|
||||
parish: defaultValues?.parish || undefined,
|
||||
},
|
||||
mode: 'onChange',
|
||||
});
|
||||
|
||||
|
||||
// Cascading Select Logic
|
||||
const ecoSector = useWatch({ control: form.control, name: 'ecoSector' });
|
||||
const productiveSector = useWatch({
|
||||
@@ -217,7 +222,7 @@ export function CreateTrainingForm({
|
||||
: [{ id: 0, stateId: 0, name: 'Sin Municipios' }];
|
||||
|
||||
const coorParishOptions =
|
||||
Array.isArray(dataCoorParish?.data) && dataCoorParish.data.length > 0
|
||||
Array.isArray(dataCoorParish?.data) && dataCoorParish?.data.length > 0
|
||||
? dataCoorParish.data
|
||||
: [{ id: 0, stateId: 0, name: 'Sin Parroquias' }];
|
||||
|
||||
@@ -263,7 +268,7 @@ export function CreateTrainingForm({
|
||||
// 1. Definimos las claves que NO queremos enviar en el bucle general
|
||||
// 'files' se procesa aparte.
|
||||
// 'photo1/2/3' son strings (urls viejas) que no queremos reenviar como texto.
|
||||
const excludedKeys = ['files', 'photo1', 'photo2', 'photo3'];
|
||||
const excludedKeys = ['files', 'photo1', 'photo2', 'photo3', 'coorState', 'coorMunicipality', 'coorParish'];
|
||||
|
||||
Object.entries(formData).forEach(([key, value]) => {
|
||||
// 2. Condición actualizada: Si la key está en la lista de excluidos, la saltamos
|
||||
@@ -282,6 +287,17 @@ export function CreateTrainingForm({
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// 2. Mapeo manual y conversión a numérico
|
||||
// if (formData.state) {
|
||||
// data.append('state', Number(formData.state).toString());
|
||||
// }
|
||||
// if (formData.municipality) {
|
||||
// data.append('municipality', Number(formData.municipality).toString());
|
||||
// }
|
||||
// if (formData.parish) {
|
||||
// data.append('parish', Number(formData.parish).toString());
|
||||
// }
|
||||
if (defaultValues?.id) {
|
||||
data.append('id', defaultValues.id.toString());
|
||||
}
|
||||
@@ -289,24 +305,24 @@ export function CreateTrainingForm({
|
||||
// 4. Aquí se agregan las NUEVAS fotos (binary) si el usuario seleccionó alguna
|
||||
selectedFiles.forEach((file) => {
|
||||
data.append('files', file);
|
||||
});
|
||||
|
||||
});
|
||||
console.log(data);
|
||||
const mutation = defaultValues?.id ? updateTraining : createTraining;
|
||||
|
||||
mutation(data as any, {
|
||||
onSuccess: () => {
|
||||
form.reset();
|
||||
setSelectedFiles([]);
|
||||
onSuccess?.();
|
||||
},
|
||||
onError: (e) => {
|
||||
console.error(e);
|
||||
form.setError('root', {
|
||||
type: 'manual',
|
||||
message: 'Error al guardar el registro',
|
||||
});
|
||||
},
|
||||
});
|
||||
// mutation(data as any, {
|
||||
// onSuccess: () => {
|
||||
// form.reset();
|
||||
// setSelectedFiles([]);
|
||||
// onSuccess?.();
|
||||
// },
|
||||
// onError: (e) => {
|
||||
// console.error(e);
|
||||
// form.setError('root', {
|
||||
// type: 'manual',
|
||||
// message: 'Error al guardar el registro',
|
||||
// });
|
||||
// },
|
||||
// });
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -382,6 +398,8 @@ export function CreateTrainingForm({
|
||||
)}
|
||||
/>
|
||||
|
||||
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="visitDate"
|
||||
@@ -399,6 +417,76 @@ export function CreateTrainingForm({
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="state"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Estado</FormLabel>
|
||||
<SelectSearchable
|
||||
options={stateOptions.map((item) => ({
|
||||
value: item.id.toString(),
|
||||
label: item.name,
|
||||
}))}
|
||||
onValueChange={(value) => {
|
||||
field.onChange(Number(value));
|
||||
setState(Number(value));
|
||||
setDisabledMunicipality(false);
|
||||
setDisabledParish(true);
|
||||
}}
|
||||
placeholder="Selecciona un estado"
|
||||
defaultValue={field.value?.toString()}
|
||||
/>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="municipality"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Municipio</FormLabel>
|
||||
<SelectSearchable
|
||||
options={municipalityOptions.map((item) => ({
|
||||
value: item.id.toString(),
|
||||
label: item.name,
|
||||
}))}
|
||||
onValueChange={(value) => {
|
||||
field.onChange(Number(value));
|
||||
setMunicipality(Number(value));
|
||||
setDisabledParish(false);
|
||||
}}
|
||||
placeholder="Selecciona un municipio"
|
||||
disabled={disabledMunicipality}
|
||||
defaultValue={field.value?.toString()}
|
||||
/>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="parish"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Parroquia</FormLabel>
|
||||
<SelectSearchable
|
||||
options={parishOptions.map((item) => ({
|
||||
value: item.id.toString(),
|
||||
label: item.name,
|
||||
}))}
|
||||
onValueChange={(value) => field.onChange(Number(value))}
|
||||
placeholder="Selecciona una parroquia"
|
||||
disabled={disabledParish}
|
||||
defaultValue={field.value?.toString()}
|
||||
/>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
@@ -833,6 +921,7 @@ export function CreateTrainingForm({
|
||||
<CardTitle>3. Detalles de la ubicación</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="grid grid-cols-1 lg:grid-cols-2 gap-6 items-start">
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="ospAddress"
|
||||
|
||||
Reference in New Issue
Block a user