Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2909 | Rev 3272 | 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,
3271 stevensc 27
    formState: { errors, isSubmitting }
2909 stevensc 28
  } = useForm({
29
    defaultValues: {
30
      image: ''
31
    }
32
  })
5 stevensc 33
 
3271 stevensc 34
  const onSubmit = async ({ image }) => {
35
    try {
36
      const formData = new FormData()
37
      formData.append('image', image)
1979 stevensc 38
 
3271 stevensc 39
      const response = axios.post(url, formData)
5 stevensc 40
 
3271 stevensc 41
      const { data, success } = response
5 stevensc 42
 
3271 stevensc 43
      if (!success) throw new Error('Error al actualizar la imagen')
5 stevensc 44
 
3271 stevensc 45
      const newImage = data.profile ?? data
5 stevensc 46
 
3271 stevensc 47
      if (data.update_navbar) {
48
        sessionStorage.setItem('user_session_image', data.user)
49
      }
5 stevensc 50
 
3271 stevensc 51
      dispatch(addNotification({ style: 'success', msg: 'Imagen actualizada' }))
52
      onComplete(newImage)
53
      reset()
54
      onClose()
55
    } catch (error) {
56
      dispatch(addNotification({ style: 'error', msg: error.message }))
57
    }
58
  }
5 stevensc 59
 
60
  useEffect(() => {
61
    register('image', {
1214 stevensc 62
      required: { value: 'true', message: 'El campo es requerido' }
5 stevensc 63
    })
64
  }, [])
65
 
66
  return (
1214 stevensc 67
    <Modal
68
      show={show}
69
      title='Imagen'
70
      onClose={onClose}
1426 stevensc 71
      onReject={onClose}
3271 stevensc 72
      onAccept={handleSubmit(onSubmit)}
73
      loading={isSubmitting}
1214 stevensc 74
    >
75
      <DropzoneComponent
76
        modalType='IMAGE'
2909 stevensc 77
        onUploaded={(file) => setValue('image', file)}
1931 stevensc 78
        settedFile={watch('image')}
2909 stevensc 79
        recomendationText={message}
1214 stevensc 80
      />
81
 
82
      {errors.image && (
83
        <FormErrorFeedback>{errors.image.message}</FormErrorFeedback>
84
      )}
5 stevensc 85
    </Modal>
86
  )
87
}
88
 
89
export default ImageModal