Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 8520 | Rev 11152 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
8468 stevensc 1
import axios from 'axios'
2
import React, { useEffect, useState } from 'react'
3
import { Modal, Button } from 'react-bootstrap'
4
import { useForm } from 'react-hook-form'
5
import { getData } from '../../../helpers/fetchHelpers'
6
 
7
const IndustryModal = ({
8
    isOpen,
9
    closeModal,
8520 stevensc 10
    editUrl,
8468 stevensc 11
    action
12
}) => {
13
 
8516 stevensc 14
    const { handleSubmit, register } = useForm()
8468 stevensc 15
    const [error, setError] = useState(null);
8472 stevensc 16
    const [industries, setIndustries] = useState([]);
8473 stevensc 17
    const [currentIndustry, setCurrentIndustry] = useState("");
8468 stevensc 18
 
8480 stevensc 19
    const onSubmit = ({ industry }) => {
8520 stevensc 20
 
8480 stevensc 21
        const data = new FormData()
8479 stevensc 22
        data.append("industry_id", industry)
23
 
8468 stevensc 24
        axios.post(editUrl, data)
8495 stevensc 25
            .then(({ data }) => {
26
                if (!data.success) {
27
                    return setError("Error en la respuesta")
28
                }
8522 stevensc 29
                action(data.data)
8468 stevensc 30
            })
8495 stevensc 31
            .then(() => {
32
                setError(null)
33
                closeModal()
34
            })
8480 stevensc 35
            .catch((err) => console.log(err))
8468 stevensc 36
    }
37
 
38
    useEffect(() => {
8520 stevensc 39
        getData(editUrl)
8473 stevensc 40
            .then(({ industries, industry_id }) => {
8470 stevensc 41
                Object.entries(industries).map(([key, value]) => {
42
                    setIndustries(prev => [...prev, { value: key, name: value }])
43
                })
8473 stevensc 44
 
45
                setCurrentIndustry(industry_id)
8470 stevensc 46
            })
8468 stevensc 47
    }, [isOpen]);
48
 
49
    return (
50
        <Modal
51
            size="md"
52
            show={isOpen}
53
            onHide={closeModal}
54
            autoFocus={false}
55
        >
56
            <Modal.Header closeButton>
57
                <Modal.Title>Cambiar</Modal.Title>
58
            </Modal.Header>
59
            <form onSubmit={handleSubmit(onSubmit)}>
60
                <Modal.Body>
61
                    <div className="mb-3">
8473 stevensc 62
                        <label className="form-label">Industria</label>
8470 stevensc 63
                        <select
64
                            className='form-control'
8468 stevensc 65
                            name="industry"
66
                            ref={register}
8470 stevensc 67
                        >
68
                            {
69
                                industries.map(({ value, name }) => (
70
                                    <option key={value} value={value}>{name}</option>
71
                                ))
72
                            }
73
                        </select>
8468 stevensc 74
                    </div>
75
                    {error && <p>{error}</p>}
76
                </Modal.Body>
77
                <Modal.Footer>
78
                    <Button
79
                        variant="primary"
80
                        type="submit"
81
                    >
82
                        Enviar
83
                    </Button>
84
                    <Button
85
                        variant="default"
86
                        onClick={closeModal}
87
                    >
88
                        Cancelar
89
                    </Button>
90
                </Modal.Footer>
91
            </form>
92
        </Modal >
93
    )
94
}
95
 
96
export default IndustryModal