Proyectos de Subversion LeadersLinked - Backend

Rev

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