Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2802 | Rev 3271 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1214 stevensc 1
import React, { useEffect } from 'react'
2909 stevensc 2
import { useDispatch } from 'react-redux'
5 stevensc 3
import { useForm } from 'react-hook-form'
1214 stevensc 4
 
2909 stevensc 5
import { axios } from '@utils'
6
import { addNotification } from '@store/notification/notification.actions'
5 stevensc 7
 
2909 stevensc 8
import Modal from '@components/UI/modal/Modal'
9
import DropzoneComponent from '@components/dropzone/DropzoneComponent'
10
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback'
5 stevensc 11
 
2909 stevensc 12
const ImageModal = ({
13
  show = false,
14
  url = '',
15
  message = 'Imágenes recomendadas de 250x250',
16
  onComplete = (newImage) => {},
17
  onClose = () => {}
18
}) => {
5 stevensc 19
  const dispatch = useDispatch()
20
 
2802 stevensc 21
  const {
22
    register,
23
    handleSubmit,
24
    setValue,
25
    watch,
2909 stevensc 26
    reset,
2802 stevensc 27
    formState: { errors }
2909 stevensc 28
  } = useForm({
29
    defaultValues: {
30
      image: ''
31
    }
32
  })
5 stevensc 33
 
1214 stevensc 34
  const onSubmitHandler = handleSubmit(({ image }) => {
1979 stevensc 35
    const formData = new FormData()
36
    formData.append('image', image)
37
 
1215 stevensc 38
    axios
2909 stevensc 39
      .post(url, formData)
1215 stevensc 40
      .then(({ data: response }) => {
41
        const { data, success } = response
5 stevensc 42
 
1215 stevensc 43
        if (!success) {
2909 stevensc 44
          throw new Error('Error al actualizar la imagen')
5 stevensc 45
        }
46
 
1215 stevensc 47
        const newImage = data.profile ?? data
5 stevensc 48
 
1215 stevensc 49
        if (data.update_navbar) {
50
          sessionStorage.setItem('user_session_image', data.user)
51
        }
5 stevensc 52
 
1215 stevensc 53
        onComplete(newImage)
5 stevensc 54
 
1215 stevensc 55
        dispatch(
56
          addNotification({ style: 'success', msg: 'Registro actualizado' })
57
        )
2909 stevensc 58
        reset()
1215 stevensc 59
        onClose()
60
      })
61
      .catch((err) => {
2909 stevensc 62
        addNotification({ style: 'error', msg: err.message })
1215 stevensc 63
      })
1214 stevensc 64
  })
5 stevensc 65
 
66
  useEffect(() => {
67
    register('image', {
1214 stevensc 68
      required: { value: 'true', message: 'El campo es requerido' }
5 stevensc 69
    })
70
  }, [])
71
 
72
  return (
1214 stevensc 73
    <Modal
74
      show={show}
75
      title='Imagen'
76
      onClose={onClose}
1426 stevensc 77
      onReject={onClose}
1214 stevensc 78
      onAccept={onSubmitHandler}
79
    >
80
      <DropzoneComponent
81
        modalType='IMAGE'
2909 stevensc 82
        onUploaded={(file) => setValue('image', file)}
1931 stevensc 83
        settedFile={watch('image')}
2909 stevensc 84
        recomendationText={message}
1214 stevensc 85
      />
86
 
87
      {errors.image && (
88
        <FormErrorFeedback>{errors.image.message}</FormErrorFeedback>
89
      )}
5 stevensc 90
    </Modal>
91
  )
92
}
93
 
94
export default ImageModal