|
@@ -1,5 +1,5 @@
|
|
import { Form, Input, Button, Spin, Row, Col, Switch, Select as AntdSelect, Checkbox, notification, Typography, Modal, Divider } from 'antd';
|
|
import { Form, Input, Button, Spin, Row, Col, Switch, Select as AntdSelect, Checkbox, notification, Typography, Modal, Divider } from 'antd';
|
|
-import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
|
+import { useEffect, useMemo, useState, useCallback } from 'react';
|
|
import HttpService from '../../services/httpService';
|
|
import HttpService from '../../services/httpService';
|
|
import { respuestas } from '../../utilities';
|
|
import { respuestas } from '../../utilities';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { useNavigate } from 'react-router-dom';
|
|
@@ -52,6 +52,7 @@ const CondicionanteDetalle = () => {
|
|
const [selectedOrigenes, setSelectedOrigenes] = useState([]);
|
|
const [selectedOrigenes, setSelectedOrigenes] = useState([]);
|
|
const [selectedDestinos, setSelectedDestinos] = useState([]);
|
|
const [selectedDestinos, setSelectedDestinos] = useState([]);
|
|
const [isModalVisible, setIsModalVisible] = useState(false);
|
|
const [isModalVisible, setIsModalVisible] = useState(false);
|
|
|
|
+ const [catalogLoaded, setCatalogLoaded] = useState(false);
|
|
|
|
|
|
const requestParams = useMemo(() => ({
|
|
const requestParams = useMemo(() => ({
|
|
name: endpoints.condicionante,
|
|
name: endpoints.condicionante,
|
|
@@ -60,6 +61,16 @@ const CondicionanteDetalle = () => {
|
|
}), [id]);
|
|
}), [id]);
|
|
|
|
|
|
const { model, modelLoading } = useModel(request);
|
|
const { model, modelLoading } = useModel(request);
|
|
|
|
+ const fetchSubproductos = useCallback(async (productoId) => {
|
|
|
|
+ try {
|
|
|
|
+ const response = await HttpService.get(`producto?idPadre=${productoId}`);
|
|
|
|
+ if (response?.status === 200) {
|
|
|
|
+ setSubproductos(response.resultado || []);
|
|
|
|
+ }
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error("Error fetching subproductos:", error);
|
|
|
|
+ }
|
|
|
|
+ }, []);
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
if (id) {
|
|
if (id) {
|
|
@@ -75,6 +86,7 @@ const CondicionanteDetalle = () => {
|
|
let parsedSubproductos = [];
|
|
let parsedSubproductos = [];
|
|
try {
|
|
try {
|
|
parsedSubproductos = JSON.parse(model.subproductos) || [];
|
|
parsedSubproductos = JSON.parse(model.subproductos) || [];
|
|
|
|
+ console.log(parsedSubproductos);
|
|
} catch (e) {
|
|
} catch (e) {
|
|
console.error("Error parsing subproductos:", e);
|
|
console.error("Error parsing subproductos:", e);
|
|
}
|
|
}
|
|
@@ -93,36 +105,44 @@ const CondicionanteDetalle = () => {
|
|
setSelectedTipos(model.condicionanteTipo.map(tipo => tipo.idTipo));
|
|
setSelectedTipos(model.condicionanteTipo.map(tipo => tipo.idTipo));
|
|
setSelectedOrigenes(model.condicionanteOrigen.map(origen => origen.idOrigen));
|
|
setSelectedOrigenes(model.condicionanteOrigen.map(origen => origen.idOrigen));
|
|
setSelectedDestinos(model.condicionanteDestino.map(destino => destino.idDestino));
|
|
setSelectedDestinos(model.condicionanteDestino.map(destino => destino.idDestino));
|
|
|
|
+ if (model.idProducto) {
|
|
|
|
+ fetchSubproductos(model.idProducto);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }, [form, model]);
|
|
|
|
|
|
+ }, [form, model, fetchSubproductos]);
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
- const fetchCatalogData = async () => {
|
|
|
|
- try {
|
|
|
|
- const finesResponse = await HttpService.get(`${endpoints.fines}`);
|
|
|
|
- const tiposResponse = await HttpService.get(`${endpoints.tipos}`);
|
|
|
|
- const estadosResponse = await HttpService.get(`${endpoints.estados}`);
|
|
|
|
|
|
+ if (!catalogLoaded) {
|
|
|
|
+ const fetchCatalogData = async () => {
|
|
|
|
+ try {
|
|
|
|
+ const [finesResponse, tiposResponse, estadosResponse] = await Promise.all([
|
|
|
|
+ HttpService.get(`${endpoints.fines}`),
|
|
|
|
+ HttpService.get(`${endpoints.tipos}`),
|
|
|
|
+ HttpService.get(`${endpoints.estados}`)
|
|
|
|
+ ]);
|
|
|
|
|
|
- if (finesResponse?.status === 200) {
|
|
|
|
- setFines(finesResponse?.resultado || []);
|
|
|
|
|
|
+ if (finesResponse?.status === 200) {
|
|
|
|
+ setFines(finesResponse?.resultado || []);
|
|
|
|
+ }
|
|
|
|
+ if (tiposResponse?.status === 200) {
|
|
|
|
+ setTipos(tiposResponse?.resultado || []);
|
|
|
|
+ }
|
|
|
|
+ if (estadosResponse?.status === 200) {
|
|
|
|
+ setEstados(estadosResponse?.resultado || []);
|
|
|
|
+ }
|
|
|
|
+ setCatalogLoaded(true);
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error(error);
|
|
|
|
+ notification.error({
|
|
|
|
+ message: "Error",
|
|
|
|
+ description: "No se pudieron cargar los datos del catálogo.",
|
|
|
|
+ });
|
|
}
|
|
}
|
|
- if (tiposResponse?.status === 200) {
|
|
|
|
- setTipos(tiposResponse?.resultado || []);
|
|
|
|
- }
|
|
|
|
- if (estadosResponse?.status === 200) {
|
|
|
|
- setEstados(estadosResponse?.resultado || []);
|
|
|
|
- }
|
|
|
|
- } catch (error) {
|
|
|
|
- console.error(error);
|
|
|
|
- notification.error({
|
|
|
|
- message: "Error",
|
|
|
|
- description: "No se pudieron cargar los datos del catálogo.",
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
|
|
+ };
|
|
|
|
|
|
- fetchCatalogData();
|
|
|
|
- }, []);
|
|
|
|
|
|
+ fetchCatalogData();
|
|
|
|
+ }
|
|
|
|
+ }, [catalogLoaded]);
|
|
|
|
|
|
const onFinish = async (values) => {
|
|
const onFinish = async (values) => {
|
|
try {
|
|
try {
|
|
@@ -168,6 +188,7 @@ const CondicionanteDetalle = () => {
|
|
return <Spin size="large" style={{ display: "block", margin: "auto", marginTop: "50px" }} />;
|
|
return <Spin size="large" style={{ display: "block", margin: "auto", marginTop: "50px" }} />;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ console.log(subproductos);
|
|
return (
|
|
return (
|
|
<DefaultLayout>
|
|
<DefaultLayout>
|
|
<Form layout="vertical" name="basic" form={form} onFinish={onFinish} onFinishFailed={() => { }}>
|
|
<Form layout="vertical" name="basic" form={form} onFinish={onFinish} onFinishFailed={() => { }}>
|
|
@@ -276,9 +297,10 @@ const CondicionanteDetalle = () => {
|
|
valueProp="id"
|
|
valueProp="id"
|
|
append={[model?.producto]}
|
|
append={[model?.producto]}
|
|
extraParams={{ padre: true }}
|
|
extraParams={{ padre: true }}
|
|
- onChange={(_, item) => {
|
|
|
|
|
|
+ onChange={async (_, item) => {
|
|
setSubproductos(item?.subproductos || []);
|
|
setSubproductos(item?.subproductos || []);
|
|
setSelectedSubproductos(form.getFieldValue('subproductos'));
|
|
setSelectedSubproductos(form.getFieldValue('subproductos'));
|
|
|
|
+ console.log(item)
|
|
}}
|
|
}}
|
|
/>
|
|
/>
|
|
</Form.Item>
|
|
</Form.Item>
|