Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 8522 | Rev 14524 | 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 = ({
11152 stevensc 8
	isOpen,
9
	closeModal,
10
	editUrl,
11
	action
8468 stevensc 12
}) => {
13
 
11152 stevensc 14
	const { handleSubmit, register } = useForm()
15
	const [error, setError] = useState(null)
16
	const [industries, setIndustries] = useState([])
17
	const [currentIndustry, setCurrentIndustry] = useState('')
8468 stevensc 18
 
11152 stevensc 19
	const onSubmit = ({ industry }) => {
8520 stevensc 20
 
11152 stevensc 21
		const data = new FormData()
22
		data.append('industry_id', industry)
8479 stevensc 23
 
11152 stevensc 24
		axios.post(editUrl, data)
25
			.then(({ data }) => {
26
				if (!data.success) {
27
					return setError('Error en la respuesta')
28
				}
29
				action(data.data)
30
			})
31
			.then(() => {
32
				setError(null)
33
				closeModal()
34
			})
35
			.catch((err) => console.log(err))
36
	}
8468 stevensc 37
 
11152 stevensc 38
	useEffect(() => {
39
		getData(editUrl)
40
			.then(({ industries, industry_id }) => {
41
				Object.entries(industries).map(([key, value]) => {
42
					setIndustries(prev => [...prev, { value: key, name: value }])
43
				})
8473 stevensc 44
 
11152 stevensc 45
				setCurrentIndustry(industry_id)
46
			})
47
	}, [isOpen])
8468 stevensc 48
 
11152 stevensc 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">
62
						<label className="form-label">Industria</label>
63
						<select
64
							className='form-control'
65
							name="industry"
66
							ref={register}
67
						>
68
							{
69
								industries.map(({ value, name }) => (
70
									<option key={value} value={value}>{name}</option>
71
								))
72
							}
73
						</select>
74
					</div>
75
					{error && <p>{error}</p>}
76
				</Modal.Body>
77
				<Modal.Footer>
78
					<Button
79
						variant="primary"
80
						type="submit"
81
					>
8468 stevensc 82
                        Enviar
11152 stevensc 83
					</Button>
84
					<Button
85
						className='btn-tertiary'
86
						onClick={closeModal}
87
					>
8468 stevensc 88
                        Cancelar
11152 stevensc 89
					</Button>
90
				</Modal.Footer>
91
			</form>
92
		</Modal >
93
	)
8468 stevensc 94
}
95
 
96
export default IndustryModal