| 3719 |
stevensc |
1 |
import React from 'react';
|
|
|
2 |
import { useForm } from 'react-hook-form';
|
|
|
3 |
import { useDispatch, useSelector } from 'react-redux';
|
|
|
4 |
|
|
|
5 |
import { axios } from '@app/utils';
|
|
|
6 |
import { addNotification } from '@app/redux/notification/notification.actions';
|
|
|
7 |
|
|
|
8 |
import Modal from '../UI/modal/Modal';
|
|
|
9 |
import Input from '../UI/inputs/Input';
|
|
|
10 |
|
|
|
11 |
const AddProfileModal = ({ show = '', onHide = () => null, getProfiles = () => null }) => {
|
|
|
12 |
const labels = useSelector(({ intl }) => intl.labels);
|
|
|
13 |
const dispatch = useDispatch();
|
|
|
14 |
|
|
|
15 |
const {
|
|
|
16 |
control,
|
|
|
17 |
handleSubmit,
|
|
|
18 |
formState: { errors }
|
|
|
19 |
} = useForm();
|
|
|
20 |
|
|
|
21 |
const onSubmitHandler = handleSubmit(async (data) => {
|
|
|
22 |
const formData = new FormData();
|
|
|
23 |
Object.entries(data).map(([key, value]) => formData.append(key, value));
|
|
|
24 |
|
|
|
25 |
try {
|
|
|
26 |
const response = await axios.post('/profile/my-profiles/add', formData);
|
|
|
27 |
const { data, success } = response.data;
|
|
|
28 |
|
|
|
29 |
if (!success) throw new Error('Error al crear el perfil');
|
|
|
30 |
|
|
|
31 |
addNotification({ style: 'success', msg: data });
|
|
|
32 |
getProfiles();
|
|
|
33 |
onHide();
|
|
|
34 |
} catch (error) {
|
|
|
35 |
dispatch(addNotification({ style: 'danger', msg: error.message }));
|
|
|
36 |
}
|
|
|
37 |
});
|
|
|
38 |
|
|
|
39 |
return (
|
|
|
40 |
<Modal
|
|
|
41 |
title={labels.new_profile}
|
|
|
42 |
show={show}
|
|
|
43 |
onHide={onHide}
|
|
|
44 |
onAccept={onSubmitHandler}
|
|
|
45 |
onClose={onHide}
|
|
|
46 |
>
|
|
|
47 |
<Input
|
|
|
48 |
name='name'
|
|
|
49 |
placeholder={labels.create_profile}
|
|
|
50 |
control={control}
|
|
|
51 |
rules={{ required: 'Este campo es requerido' }}
|
|
|
52 |
error={errors.name?.message}
|
|
|
53 |
/>
|
|
|
54 |
</Modal>
|
|
|
55 |
);
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
export default AddProfileModal;
|