Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2802 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 2802 Rev 2861
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 { connect, useSelector } from 'react-redux'
3
import { useDispatch, useSelector } from 'react-redux'
Línea 4... Línea 4...
4
 
4
 
5
import { axios } from '@app/utils'
5
import { axios } from '@app/utils'
Línea 6... Línea 6...
6
import { addNotification } from '@app/redux/notification/notification.actions'
6
import { addNotification } from '@app/redux/notification/notification.actions'
7
 
7
 
Línea 8... Línea 8...
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 = ({
11
const AddProfileModal = ({
12
  show = '',
-
 
13
  onHide = () => null,
12
  show = '',
14
  getProfiles = () => null,
13
  onHide = () => null,
-
 
14
  getProfiles = () => null
Línea 15... Línea 15...
15
  addNotification // redux action
15
}) => {
16
}) => {
16
  const labels = useSelector(({ intl }) => intl.labels)
17
  const labels = useSelector(({ intl }) => intl.labels)
17
  const dispatch = useDispatch()
18
 
18
 
19
  const {
19
  const {
Línea 20... Línea 20...
20
    register,
20
    control,
21
    handleSubmit,
21
    handleSubmit,
22
    formState: { errors }
22
    formState: { errors }
Línea 23... Línea 23...
23
  } = useForm()
23
  } = useForm()
24
 
24
 
25
  const onSubmitHandler = handleSubmit(async (data) => {
-
 
26
    const formData = new FormData()
25
  const onSubmitHandler = handleSubmit(async (data) => {
27
    Object.entries(data).map(([key, value]) => formData.append(key, value))
26
    const formData = new FormData()
28
 
-
 
29
    axios
-
 
30
      .post('/profile/my-profiles/add', formData)
-
 
31
      .then(({ data: response }) => {
-
 
32
        const { data, success } = response
27
    Object.entries(data).map(([key, value]) => formData.append(key, value))
33
 
-
 
34
        if (!success) {
-
 
35
          const errorMessage =
-
 
36
            typeof data === 'string'
28
 
37
              ? data
-
 
38
              : 'Ha ocurrido un error, por favor intente más tarde'
29
    try {
39
          addNotification({ style: 'success', msg: errorMessage })
30
      const response = await axios.post('/profile/my-profiles/add', formData)
40
          return
31
      const { data, success } = response.data
41
        }
32
 
42
 
33
      if (!success) throw new Error('Error al crear el perfil')
43
        getProfiles()
34
 
44
        addNotification({ style: 'success', msg: data })
35
      addNotification({ style: 'success', msg: data })
Línea 45... Línea 36...
45
        onHide()
36
      getProfiles()
46
      })
37
      onHide()
47
      .catch((err) => {
38
    } catch (error) {
Línea 58... Línea 49...
58
      onClose={onHide}
49
      onClose={onHide}
59
    >
50
    >
60
      <Input
51
      <Input
61
        name='name'
52
        name='name'
62
        placeholder={labels.create_profile}
53
        placeholder={labels.create_profile}
-
 
54
        control={control}
63
        inputRef={register({ required: 'Este campo es requerido' })}
55
        rules={{ required: 'Este campo es requerido' }}
64
        error={errors.name?.message}
56
        error={errors.name?.message}
65
      />
57
      />
66
    </Modal>
58
    </Modal>
67
  )
59
  )
68
}
60
}
Línea 69... Línea -...
69
 
-
 
70
const mapDispatchToProps = {
-
 
71
  addNotification: (notification) => addNotification(notification)
-
 
72
}
-
 
73
 
61