Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2861 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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