3719 |
stevensc |
1 |
import React from 'react';
|
|
|
2 |
import { useDispatch, useSelector } from 'react-redux';
|
|
|
3 |
import { useForm } from 'react-hook-form';
|
|
|
4 |
|
|
|
5 |
import { axios } from '@app/utils';
|
|
|
6 |
import { useFetchHelper } from '@hooks';
|
|
|
7 |
import { addNotification } from '@app/redux/notification/notification.actions';
|
|
|
8 |
|
|
|
9 |
import Modal from '@components/UI/modal/Modal';
|
|
|
10 |
import Input from '@components/UI/inputs/Input';
|
|
|
11 |
import Select from '@components/UI/inputs/Select';
|
|
|
12 |
|
|
|
13 |
const AddCompanyModal = ({ show = false, onHide = () => null, fetchCompanies = () => [] }) => {
|
|
|
14 |
const { data: companySizes } = useFetchHelper('company-sizes');
|
|
|
15 |
const { data: industries } = useFetchHelper('industries');
|
|
|
16 |
const labels = useSelector(({ intl }) => intl.labels);
|
|
|
17 |
const dispatch = useDispatch();
|
|
|
18 |
|
|
|
19 |
const {
|
|
|
20 |
control,
|
|
|
21 |
handleSubmit,
|
|
|
22 |
formState: { errors }
|
|
|
23 |
} = useForm();
|
|
|
24 |
|
|
|
25 |
const onSubmit = handleSubmit((data) => {
|
|
|
26 |
const formData = new FormData();
|
|
|
27 |
Object.entries(data).map(([key, value]) => formData.append(key, value));
|
|
|
28 |
|
|
|
29 |
axios
|
|
|
30 |
.post('/company/my-companies/add', formData)
|
|
|
31 |
.then((response) => {
|
|
|
32 |
const { success, data } = response.data;
|
|
|
33 |
if (!success) {
|
|
|
34 |
const errorMessage =
|
|
|
35 |
typeof data === 'string'
|
|
|
36 |
? data
|
|
|
37 |
: Object.entries(data).map(([key, value]) => `${key}: ${value[0]}`);
|
|
|
38 |
|
|
|
39 |
dispatch(addNotification({ style: 'danger', msg: errorMessage }));
|
|
|
40 |
return;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
fetchCompanies();
|
|
|
44 |
onHide();
|
|
|
45 |
})
|
|
|
46 |
.catch((err) => {
|
|
|
47 |
dispatch(addNotification({ style: 'danger', msg: err.message }));
|
|
|
48 |
});
|
|
|
49 |
});
|
|
|
50 |
|
|
|
51 |
return (
|
|
|
52 |
<Modal title={labels.new_company} show={show} onClose={onHide} onAccept={onSubmit}>
|
|
|
53 |
<Input
|
|
|
54 |
name='name'
|
|
|
55 |
placeholder={labels.name_of_company}
|
|
|
56 |
error={errors.name?.message}
|
|
|
57 |
control={control}
|
|
|
58 |
rules={{ required: 'Por favor ingrese el nombre de la Empresa' }}
|
|
|
59 |
/>
|
|
|
60 |
|
|
|
61 |
<Select
|
|
|
62 |
name='industry_id'
|
|
|
63 |
options={industries}
|
|
|
64 |
control={control}
|
|
|
65 |
rules={{ required: labels.select_industry }}
|
|
|
66 |
error={errors.industry_id?.message}
|
|
|
67 |
/>
|
|
|
68 |
|
|
|
69 |
<Select
|
|
|
70 |
name='company_size_id'
|
|
|
71 |
options={companySizes}
|
|
|
72 |
control={control}
|
|
|
73 |
rules={{ required: labels.select_company_size }}
|
|
|
74 |
error={errors.company_size_id?.message}
|
|
|
75 |
/>
|
|
|
76 |
</Modal>
|
|
|
77 |
);
|
|
|
78 |
};
|
|
|
79 |
|
|
|
80 |
export default AddCompanyModal;
|