| 6899 |
stevensc |
1 |
import React, { useState, useEffect } from 'react'
|
|
|
2 |
import { axios } from '../../../utils'
|
|
|
3 |
import { useForm } from 'react-hook-form'
|
|
|
4 |
import { Button, Modal } from 'react-bootstrap'
|
|
|
5 |
import { addNotification } from '../../../redux/notification/notification.actions'
|
|
|
6 |
import { useDispatch, useSelector } from 'react-redux'
|
|
|
7 |
import EditIcon from '@mui/icons-material/EditOutlined'
|
|
|
8 |
import IconButton from '@mui/material/IconButton'
|
|
|
9 |
|
|
|
10 |
import Spinner from '../../UI/Spinner'
|
|
|
11 |
import LoaderContainer from '../../UI/LoaderContainer'
|
|
|
12 |
import FormErrorFeedback from '../../UI/FormErrorFeedback'
|
|
|
13 |
|
|
|
14 |
const Industry = ({ groupId, industry: defaultValue, industries }) => {
|
|
|
15 |
const [industryKey, setIndustryKey] = useState('')
|
|
|
16 |
const [industry, setIndustry] = useState('')
|
|
|
17 |
const [isModalOpen, setIsModalOpen] = useState(false)
|
|
|
18 |
const [loading, setLoading] = useState(false)
|
|
|
19 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
20 |
const dispatch = useDispatch()
|
|
|
21 |
|
|
|
22 |
const { register, errors, handleSubmit, getValues, setError } = useForm()
|
|
|
23 |
|
|
|
24 |
const handleModalOpen = () => {
|
|
|
25 |
setIsModalOpen(!isModalOpen)
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
const onSubmitHandler = async (data) => {
|
|
|
29 |
const formData = new FormData()
|
|
|
30 |
Object.entries(data).map(([key, value]) => formData.append(key, value))
|
|
|
31 |
|
|
|
32 |
await axios
|
|
|
33 |
.post(`/group/my-groups/industry/${groupId}`, formData)
|
|
|
34 |
.then((response) => {
|
|
|
35 |
const { data, success } = response.data
|
|
|
36 |
|
|
|
37 |
if (!success) {
|
|
|
38 |
const resError = data
|
|
|
39 |
|
|
|
40 |
if (resError.constructor.name !== 'Object') {
|
|
|
41 |
dispatch(addNotification({ style: 'danger', msg: resError }))
|
|
|
42 |
return
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
Object.entries(resError).map(([key, value]) => {
|
|
|
46 |
if (key in getValues()) {
|
|
|
47 |
setError(key, {
|
|
|
48 |
type: 'manual',
|
|
|
49 |
message: Array.isArray(value) ? value[0] : value,
|
|
|
50 |
})
|
|
|
51 |
}
|
|
|
52 |
})
|
|
|
53 |
|
|
|
54 |
return
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
setIndustry(data)
|
|
|
58 |
handleModalOpen()
|
|
|
59 |
})
|
|
|
60 |
setLoading(false)
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
useEffect(() => {
|
|
|
64 |
axios.get(`/group/my-groups/industry/${groupId}`).then((response) => {
|
|
|
65 |
const { data, success } = response.data
|
|
|
66 |
if (success && data <= 0) {
|
|
|
67 |
return
|
|
|
68 |
}
|
|
|
69 |
setIndustryKey(data)
|
|
|
70 |
})
|
|
|
71 |
}, [industry])
|
|
|
72 |
|
|
|
73 |
useEffect(() => {
|
|
|
74 |
setIndustry(defaultValue)
|
|
|
75 |
}, [defaultValue])
|
|
|
76 |
|
|
|
77 |
return (
|
|
|
78 |
<>
|
|
|
79 |
<div className="experience__user-card">
|
|
|
80 |
<div className="card__options-container">
|
|
|
81 |
<h4>Industria</h4>
|
|
|
82 |
<IconButton onClick={handleModalOpen}>
|
|
|
83 |
<EditIcon />
|
|
|
84 |
</IconButton>
|
|
|
85 |
</div>
|
|
|
86 |
<span id="overview-industry">{industry}</span>
|
|
|
87 |
</div>
|
|
|
88 |
|
|
|
89 |
<Modal show={isModalOpen} onHide={handleModalOpen}>
|
|
|
90 |
<Modal.Header closeButton>
|
|
|
91 |
<Modal.Title>Industria</Modal.Title>
|
|
|
92 |
</Modal.Header>
|
|
|
93 |
<form onSubmit={handleSubmit(onSubmitHandler)}>
|
|
|
94 |
<Modal.Body>
|
|
|
95 |
<select
|
|
|
96 |
name="industry_id"
|
|
|
97 |
id="industry_id"
|
|
|
98 |
defaultValue={industryKey}
|
|
|
99 |
ref={register({
|
|
|
100 |
required: 'Por favor eliga una industria',
|
|
|
101 |
})}
|
|
|
102 |
>
|
|
|
103 |
<option value="" hidden>
|
|
|
104 |
Industria
|
|
|
105 |
</option>
|
| 6900 |
stevensc |
106 |
{industries &&
|
|
|
107 |
Object.entries(industries).map(([key, value]) => (
|
|
|
108 |
<option value={key} key={key}>
|
|
|
109 |
{value}
|
|
|
110 |
</option>
|
|
|
111 |
))}
|
| 6899 |
stevensc |
112 |
</select>
|
|
|
113 |
{errors.industry_id && (
|
|
|
114 |
<FormErrorFeedback>
|
|
|
115 |
{errors.industry_id.message}
|
|
|
116 |
</FormErrorFeedback>
|
|
|
117 |
)}
|
|
|
118 |
</Modal.Body>
|
|
|
119 |
<Modal.Footer>
|
|
|
120 |
<Button type="submit">{labels.accept}</Button>
|
|
|
121 |
<Button variant="danger" onClick={handleModalOpen}>
|
|
|
122 |
{labels.cancel}
|
|
|
123 |
</Button>
|
|
|
124 |
</Modal.Footer>
|
|
|
125 |
</form>
|
|
|
126 |
{loading && (
|
|
|
127 |
<LoaderContainer>
|
|
|
128 |
<Spinner />
|
|
|
129 |
</LoaderContainer>
|
|
|
130 |
)}
|
|
|
131 |
</Modal>
|
|
|
132 |
</>
|
|
|
133 |
)
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
export default Industry
|