Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 2802 Rev 2909
Línea 1... Línea 1...
1
import React, { useEffect } from 'react'
1
import React, { useEffect } from 'react'
2
import { useForm } from 'react-hook-form'
-
 
3
import { useDispatch } from 'react-redux'
2
import { useDispatch } from 'react-redux'
-
 
3
import { useForm } from 'react-hook-form'
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'
-
 
7
import { handleApiError } from 'utils/errors'
-
 
8
 
-
 
9
import Modal from 'components/UI/modal/Modal'
-
 
10
import DropzoneComponent from '../dropzone/DropzoneComponent'
-
 
Línea -... Línea 6...
-
 
6
import { addNotification } from '@store/notification/notification.actions'
11
import FormErrorFeedback from '../UI/form/FormErrorFeedback'
7
 
-
 
8
import Modal from '@components/UI/modal/Modal'
-
 
9
import DropzoneComponent from '@components/dropzone/DropzoneComponent'
-
 
10
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback'
-
 
11
 
-
 
12
const ImageModal = ({
-
 
13
  show = false,
-
 
14
  url = '',
-
 
15
  message = 'Imágenes recomendadas de 250x250',
-
 
16
  onComplete = (newImage) => {},
12
 
17
  onClose = () => {}
Línea 13... Línea 18...
13
const ImageModal = ({ show, onClose, sizes, id, onComplete }) => {
18
}) => {
14
  const dispatch = useDispatch()
19
  const dispatch = useDispatch()
15
 
20
 
16
  const {
21
  const {
17
    register,
-
 
18
    handleSubmit,
-
 
19
    setValue,
22
    register,
-
 
23
    handleSubmit,
20
    clearErrors,
24
    setValue,
21
    setError,
25
    watch,
22
    watch,
-
 
23
    formState: { errors }
-
 
24
  } = useForm()
26
    reset,
25
 
27
    formState: { errors }
26
  const onUploadedHandler = (files) => {
-
 
27
    setValue('image', files)
-
 
28
    clearErrors('image')
-
 
29
  }
-
 
30
 
-
 
31
  const onSubmitHandler = handleSubmit(({ image }) => {
-
 
32
    const type = window.location.pathname.split('/')[1]
-
 
33
    const typesUrl = {
-
 
34
      profile: `/profile/my-profiles/image/${id}/operation/upload`,
28
  } = useForm({
-
 
29
    defaultValues: {
Línea -... Línea 30...
-
 
30
      image: ''
35
      company: `/my-company/${id}/profile/image/upload`,
31
    }
36
      group: `/group/my-groups/image/${id}/operation/upload`
32
  })
Línea 37... Línea 33...
37
    }
33
 
38
 
34
  const onSubmitHandler = handleSubmit(({ image }) => {
39
    const formData = new FormData()
35
    const formData = new FormData()
40
    formData.append('image', image)
36
    formData.append('image', image)
Línea 41... Línea 37...
41
 
37
 
42
    axios
38
    axios
43
      .post(typesUrl[type], formData)
39
      .post(url, formData)
Línea 44... Línea 40...
44
      .then(({ data: response }) => {
40
      .then(({ data: response }) => {
Línea 45... Línea 41...
45
        const { data, success } = response
41
        const { data, success } = response
46
 
42
 
47
        if (!success) {
43
        if (!success) {
Línea 48... Línea 44...
48
          throw new Error(data)
44
          throw new Error('Error al actualizar la imagen')
Línea 49... Línea -...
49
        }
-
 
50
 
45
        }
51
        const newImage = data.profile ?? data
46
 
52
 
47
        const newImage = data.profile ?? data
-
 
48
 
53
        if (data.update_navbar) {
49
        if (data.update_navbar) {
54
          sessionStorage.setItem('user_session_image', data.user)
50
          sessionStorage.setItem('user_session_image', data.user)
55
        }
51
        }
56
 
-
 
57
        onComplete(newImage)
-
 
58
 
-
 
59
        setValue('image', '')
-
 
60
        dispatch(
52
 
61
          addNotification({ style: 'success', msg: 'Registro actualizado' })
53
        onComplete(newImage)
62
        )
54
 
Línea 63... Línea 55...
63
        onClose()
55
        dispatch(
64
      })
56
          addNotification({ style: 'success', msg: 'Registro actualizado' })
Línea 85... Línea 77...
85
      onReject={onClose}
77
      onReject={onClose}
86
      onAccept={onSubmitHandler}
78
      onAccept={onSubmitHandler}
87
    >
79
    >
88
      <DropzoneComponent
80
      <DropzoneComponent
89
        modalType='IMAGE'
81
        modalType='IMAGE'
90
        onUploaded={onUploadedHandler}
82
        onUploaded={(file) => setValue('image', file)}
91
        settedFile={watch('image')}
83
        settedFile={watch('image')}
92
        recomendationText={`Imágenes recomendadas de ${sizes}`}
84
        recomendationText={message}
93
      />
85
      />
Línea 94... Línea 86...
94
 
86
 
95
      {errors.image && (
87
      {errors.image && (
96
        <FormErrorFeedback>{errors.image.message}</FormErrorFeedback>
88
        <FormErrorFeedback>{errors.image.message}</FormErrorFeedback>