| 3682 |
stevensc |
1 |
import React from 'react';
|
|
|
2 |
|
|
|
3 |
import { useAlert, useApi } from '@shared/hooks';
|
|
|
4 |
import { parseHelperToSelect } from '@shared/utils';
|
|
|
5 |
import { getGroupTypes, getIndustries, saveGroup } from '@groups/services';
|
|
|
6 |
|
|
|
7 |
import { Form, FormButton, FormInput, FormSelect, Spinner } from '@shared/components';
|
|
|
8 |
|
|
|
9 |
export const AddGroupForm = ({ onSubmit }) => {
|
|
|
10 |
const { data: groupTypes, loading: loadingGroupTypes } = useApi(getGroupTypes, {
|
|
|
11 |
autoFetch: true
|
|
|
12 |
});
|
|
|
13 |
const { data: industries, loading: loadingIndustries } = useApi(getIndustries, {
|
|
|
14 |
autoFetch: true
|
|
|
15 |
});
|
|
|
16 |
|
|
|
17 |
const { showError, showSuccess } = useAlert();
|
|
|
18 |
|
|
|
19 |
const { execute } = useApi(saveGroup, {
|
|
|
20 |
onSuccess: (message) => {
|
|
|
21 |
showSuccess(message);
|
|
|
22 |
onSubmit();
|
|
|
23 |
},
|
|
|
24 |
onError: (error) => {
|
|
|
25 |
showError(error.message);
|
|
|
26 |
}
|
|
|
27 |
});
|
|
|
28 |
|
| 3684 |
stevensc |
29 |
if (loadingGroupTypes || loadingIndustries || !groupTypes || !industries) return <Spinner />;
|
| 3682 |
stevensc |
30 |
|
|
|
31 |
return (
|
|
|
32 |
<Form onSubmit={execute} defaultValues={{ name: '', type_id: '', industry_id: '' }} reset>
|
|
|
33 |
<FormInput
|
|
|
34 |
name='name'
|
|
|
35 |
label='Nombre'
|
|
|
36 |
placeholder='Nombre del grupo'
|
|
|
37 |
rules={{ required: 'Este campo es requerido' }}
|
|
|
38 |
/>
|
|
|
39 |
|
|
|
40 |
<FormSelect
|
|
|
41 |
label='Industria'
|
|
|
42 |
name='type_id'
|
|
|
43 |
rules={{ required: 'Por favor eliga un tipo' }}
|
|
|
44 |
options={parseHelperToSelect(groupTypes)}
|
|
|
45 |
/>
|
|
|
46 |
|
|
|
47 |
<FormSelect
|
|
|
48 |
label='Tipo'
|
|
|
49 |
name='industry_id'
|
|
|
50 |
rules={{ required: 'Por favor eliga un tipo' }}
|
|
|
51 |
options={parseHelperToSelect(industries)}
|
|
|
52 |
/>
|
|
|
53 |
|
|
|
54 |
<FormButton type='submit'>Crear grupo</FormButton>
|
|
|
55 |
</Form>
|
|
|
56 |
);
|
|
|
57 |
};
|