Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 1214 Rev 1215
Línea 2... Línea 2...
2
import { useForm } from 'react-hook-form'
2
import { useForm } from 'react-hook-form'
3
import { useDispatch } from 'react-redux'
3
import { useDispatch } from 'react-redux'
Línea 4... Línea 4...
4
 
4
 
5
import { axios } from '../../utils'
5
import { axios } from '../../utils'
-
 
6
import { addNotification } from '../../redux/notification/notification.actions'
Línea 6... Línea 7...
6
import { addNotification } from '../../redux/notification/notification.actions'
7
import { handleApiError } from 'utils/errors'
7
 
8
 
8
import Modal from 'components/UI/Modal'
9
import Modal from 'components/UI/Modal'
Línea 36... Línea 37...
36
    }
37
    }
Línea 37... Línea 38...
37
 
38
 
38
    const formData = new FormData()
39
    const formData = new FormData()
Línea -... Línea 40...
-
 
40
    formData.append('image', image)
-
 
41
 
39
    formData.append('image', image)
42
    axios
40
 
43
      .post(typesUrl[type], formData)
Línea 41... Línea 44...
41
    axios.post(typesUrl[type], formData).then(({ data: response }) => {
44
      .then(({ data: response }) => {
42
      const { data, success } = response
45
        const { data, success } = response
43
 
-
 
44
      if (!success) {
-
 
45
        const resError = data
-
 
46
        if (resError.constructor.name === 'Object') {
-
 
47
          Object.entries(resError).forEach(([key, value]) => {
-
 
48
            if (key in getValues()) {
-
 
49
              setError(key, {
-
 
50
                type: 'manual',
-
 
51
                message: Array.isArray(value) ? value[0] : value
-
 
52
              })
-
 
53
            }
-
 
54
          })
46
 
55
        } else {
-
 
Línea 56... Línea 47...
56
          dispatch(addNotification({ style: 'danger', msg: resError }))
47
        if (!success) {
Línea 57... Línea 48...
57
        }
48
          throw new Error(data)
58
      }
49
        }
59
 
50
 
60
      const newImage = data.profile ?? data
51
        const newImage = data.profile ?? data
61
 
52
 
62
      if (data.update_navbar) {
53
        if (data.update_navbar) {
63
        sessionStorage.setItem('user_session_image', data.user)
54
          sessionStorage.setItem('user_session_image', data.user)
64
      }
55
        }
65
 
56
 
66
      onComplete(newImage)
57
        onComplete(newImage)
67
 
58
 
-
 
59
        setValue('image', '')
-
 
60
        dispatch(
-
 
61
          addNotification({ style: 'success', msg: 'Registro actualizado' })
-
 
62
        )
-
 
63
        onClose()
-
 
64
      })
-
 
65
      .catch((err) => {
68
      setValue('image', '')
66
        const error = handleApiError(err)
69
      dispatch(
67
 
Línea 70... Línea 68...
70
        addNotification({ style: 'success', msg: 'Registro actualizado' })
68
        Array.isArray(error)
71
      )
69
          ? error.forEach(({ key, message }) => setError(key, { message }))
72
      onClose()
70
          : addNotification({ style: 'error', msg: error })