Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 14843 | Rev 15267 | 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'
14843 stevensc 5
import { useDispatch } from 'react-redux'
8468 stevensc 6
import { getData } from '../../../helpers/fetchHelpers'
14843 stevensc 7
import { addNotification } from '../../../redux/notification/notification.actions'
8468 stevensc 8
 
9
const IndustryModal = ({
11152 stevensc 10
	isOpen,
11
	closeModal,
12
	editUrl,
13
	action
8468 stevensc 14
}) => {
15
 
14524 stevensc 16
	const { handleSubmit, register, setValue } = useForm()
11152 stevensc 17
	const [error, setError] = useState(null)
18
	const [industries, setIndustries] = useState([])
14843 stevensc 19
	const dispatch = useDispatch()
8468 stevensc 20
 
11152 stevensc 21
	const onSubmit = ({ industry }) => {
8520 stevensc 22
 
11152 stevensc 23
		const data = new FormData()
24
		data.append('industry_id', industry)
8479 stevensc 25
 
11152 stevensc 26
		axios.post(editUrl, data)
27
			.then(({ data }) => {
28
				if (!data.success) {
14843 stevensc 29
					typeof data.data === 'string'
30
						?
31
						dispatch(addNotification({
32
							style: 'danger',
33
							msg: data.data
34
						}))
35
						: Object.entries(data.data).map(([key, value]) =>
36
							value.map(err =>
37
								dispatch(addNotification({
38
									style: 'danger',
39
									msg: `${key}: ${err}`
40
								}))
41
							)
42
						)
43
					return
11152 stevensc 44
				}
45
				action(data.data)
46
			})
47
			.then(() => {
48
				setError(null)
49
				closeModal()
50
			})
51
			.catch((err) => console.log(err))
52
	}
8468 stevensc 53
 
11152 stevensc 54
	useEffect(() => {
55
		getData(editUrl)
56
			.then(({ industries, industry_id }) => {
57
				Object.entries(industries).map(([key, value]) => {
58
					setIndustries(prev => [...prev, { value: key, name: value }])
59
				})
8473 stevensc 60
 
14524 stevensc 61
				setValue('industry', industry_id)
11152 stevensc 62
			})
63
	}, [isOpen])
8468 stevensc 64
 
11152 stevensc 65
	return (
66
		<Modal
67
			size="md"
68
			show={isOpen}
69
			onHide={closeModal}
70
			autoFocus={false}
71
		>
72
			<Modal.Header closeButton>
15227 stevensc 73
				<Modal.Title>Industria</Modal.Title>
11152 stevensc 74
			</Modal.Header>
75
			<form onSubmit={handleSubmit(onSubmit)}>
76
				<Modal.Body>
77
					<div className="mb-3">
78
						<label className="form-label">Industria</label>
79
						<select
80
							className='form-control'
81
							name="industry"
82
							ref={register}
83
						>
84
							{
85
								industries.map(({ value, name }) => (
86
									<option key={value} value={value}>{name}</option>
87
								))
88
							}
89
						</select>
90
					</div>
91
					{error && <p>{error}</p>}
92
				</Modal.Body>
93
				<Modal.Footer>
94
					<Button
95
						variant="primary"
96
						type="submit"
97
					>
14524 stevensc 98
						Enviar
11152 stevensc 99
					</Button>
100
					<Button
101
						className='btn-tertiary'
102
						onClick={closeModal}
103
					>
14524 stevensc 104
						Cancelar
11152 stevensc 105
					</Button>
106
				</Modal.Footer>
107
			</form>
108
		</Modal >
109
	)
8468 stevensc 110
}
111
 
112
export default IndustryModal