|
@@ -0,0 +1,189 @@
|
|
|
+import { Form, Input, Button, Spin, Space, Row, Col } from 'antd'
|
|
|
+import { useEffect, useMemo, useState } from 'react'
|
|
|
+import HttpService from '../../../services/httpService'
|
|
|
+import { respuestas } from '../../../utilities'
|
|
|
+import { useNavigate } from 'react-router-dom'
|
|
|
+import { useQuery, useModel } from '../../../hooks'
|
|
|
+import { commonRules } from '../../../constants/rules'
|
|
|
+import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
|
|
|
+
|
|
|
+// const selectores = {
|
|
|
+// consejoElectoral: {
|
|
|
+// name: "v1/consejo-electoral",
|
|
|
+// },
|
|
|
+// distrito: {
|
|
|
+// name: "v1/distrito",
|
|
|
+// },
|
|
|
+// estado: {
|
|
|
+// name: "v1/estado",
|
|
|
+// },
|
|
|
+// municipio: {
|
|
|
+// name: "v1/municipio",
|
|
|
+// },
|
|
|
+// participantePolitico: {
|
|
|
+// name: "v1/participante-politico",
|
|
|
+// },
|
|
|
+// seccion: {
|
|
|
+// name: "v1/seccion",
|
|
|
+// },
|
|
|
+// tipoAgenda: {
|
|
|
+// name: "v1/agenda/tipo-agenda",
|
|
|
+// },
|
|
|
+// usuario: {
|
|
|
+// name: "v1/usuario",
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+const endpoints = {
|
|
|
+ producto: "producto",
|
|
|
+};
|
|
|
+
|
|
|
+const ProductoDetalle = () => {
|
|
|
+ const [form] = Form.useForm()
|
|
|
+ const navigate = useNavigate()
|
|
|
+ const [loading, setLoading] = useState(false)
|
|
|
+ const query = useQuery()
|
|
|
+ const id = query.get("id")
|
|
|
+ const [request, setRequest] = useState({})
|
|
|
+
|
|
|
+ // const extraParams = useMemo(() => ({
|
|
|
+ // idAgenda: id,
|
|
|
+ // }), [id])
|
|
|
+
|
|
|
+ const requestParams = useMemo(() => ({
|
|
|
+ name: endpoints.producto,
|
|
|
+ expand: 'subproductos',
|
|
|
+ id,
|
|
|
+ // extraParams
|
|
|
+ }), [id])
|
|
|
+
|
|
|
+
|
|
|
+ const { model, modelLoading } = useModel(request)
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (id) {
|
|
|
+ setRequest(requestParams)
|
|
|
+ }
|
|
|
+ return () => {
|
|
|
+ setRequest({})
|
|
|
+ }
|
|
|
+ }, [id, requestParams])
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (model) {
|
|
|
+ form.setFieldsValue({ //seteo cuando son varios
|
|
|
+ ...model,
|
|
|
+ subproductos: model.subproductos.map((subproducto, index) => ({
|
|
|
+ ...subproducto,
|
|
|
+ key: index
|
|
|
+ }))
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }, [form, model])
|
|
|
+
|
|
|
+ const onFinish = async (values) => {
|
|
|
+ try {
|
|
|
+ setLoading(true);
|
|
|
+
|
|
|
+ let body = {
|
|
|
+ ...values,
|
|
|
+ };
|
|
|
+
|
|
|
+ if (id) {
|
|
|
+ body.id = id
|
|
|
+ }
|
|
|
+
|
|
|
+ const res = await HttpService.post(`${endpoints.producto}/guardar`, body);
|
|
|
+ respuestas(res);
|
|
|
+ if (res?.status === 200) {
|
|
|
+ navigate('/administracion/catalogos/productos')
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.log(error);
|
|
|
+ setLoading(false);
|
|
|
+ } finally {
|
|
|
+ setLoading(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (modelLoading) {
|
|
|
+ return <Spin
|
|
|
+ size="large"
|
|
|
+ style={{ display: "block", margin: "auto", marginTop: "50px" }}
|
|
|
+ />
|
|
|
+ }
|
|
|
+
|
|
|
+ return (
|
|
|
+ <Form
|
|
|
+ layout="vertical"
|
|
|
+ name="basic"
|
|
|
+ form={form}
|
|
|
+ onFinish={onFinish}
|
|
|
+ onFinishFailed={() => { }}
|
|
|
+ >
|
|
|
+ <Row gutter={16}>
|
|
|
+ <Col span={24}>
|
|
|
+ <h2>Información del Producto</h2>
|
|
|
+ </Col>
|
|
|
+ <Col md={8} xs={24}>
|
|
|
+ <Form.Item
|
|
|
+ label="Nombre"
|
|
|
+ name="nombre"
|
|
|
+ rules={[
|
|
|
+ commonRules.requerido,
|
|
|
+ ]}
|
|
|
+ >
|
|
|
+ <Input />
|
|
|
+ </Form.Item>
|
|
|
+ </Col>
|
|
|
+ <Col span={24}>
|
|
|
+ <h2>Subproductos:</h2>
|
|
|
+ </Col>
|
|
|
+ <Col span={24}>
|
|
|
+ <Form.List name="subproductos">
|
|
|
+ {(fields, { add, remove }) => (
|
|
|
+ <>
|
|
|
+ {fields.map(({ key, name, ...restField }) => (
|
|
|
+ <Space key={key} style={{ display: 'flex', marginBottom: 8 }} align="baseline">
|
|
|
+ <Form.Item
|
|
|
+ {...restField}
|
|
|
+ name={[name, 'nombre']}
|
|
|
+ rules={[commonRules.requerido]}
|
|
|
+ style={{ width: '350px' }}
|
|
|
+ >
|
|
|
+ <Input placeholder="Nombre del Subproducto"/>
|
|
|
+ </Form.Item>
|
|
|
+ <MinusCircleOutlined onClick={() => remove(name)} />
|
|
|
+ </Space>
|
|
|
+ ))}
|
|
|
+ <Form.Item>
|
|
|
+ <Button
|
|
|
+ type="dashed"
|
|
|
+ onClick={() => add()}
|
|
|
+ icon={<PlusOutlined />}
|
|
|
+ >
|
|
|
+ Agregar Subproducto
|
|
|
+ </Button>
|
|
|
+ </Form.Item>
|
|
|
+ </>
|
|
|
+ )}
|
|
|
+ </Form.List>
|
|
|
+ </Col>
|
|
|
+ <Col span={24}>
|
|
|
+ <Form.Item>
|
|
|
+ <Button
|
|
|
+ type="primary"
|
|
|
+ htmlType="submit"
|
|
|
+ style={{ marginTop: "30px" }}
|
|
|
+ loading={loading}
|
|
|
+ >
|
|
|
+ Guardar
|
|
|
+ </Button>
|
|
|
+ </Form.Item>
|
|
|
+ </Col>
|
|
|
+ </Row>
|
|
|
+ </Form>
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+export default ProductoDetalle
|