Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
2781 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { connect } from 'react-redux'
3
import { Avatar } from '@mui/material'
4
 
5
import { axios } from '@app/utils'
6
import { addNotification } from '@app/redux/notification/notification.actions'
7
 
8
import Button from '@app/components/UI/buttons/Buttons'
9
import Widget from '@app/components/UI/Widget'
10
import Spinner from '@app/components/UI/Spinner'
11
import DropzoneComponent from '@app/components/dropzone/DropzoneComponent'
12
import FormErrorFeedback from '@app/components/UI/form/FormErrorFeedback'
13
 
14
const ChangeImage = ({ addNotification }) => {
15
  const [imageUrl, setImageUrl] = useState('')
16
  const [imageFile, setImageFile] = useState(null)
17
  const [loading, setLoading] = useState(false)
18
  const [error, setError] = useState('')
19
 
20
  const handleOnDropImage = (acceptedFile) => {
21
    if (!acceptedFile) {
22
      setImageUrl('')
23
      setImageFile(null)
24
      setError('')
25
      return
26
    }
27
 
28
    setError('')
29
    const newImgUrl = URL.createObjectURL(acceptedFile)
30
    setImageUrl(newImgUrl)
31
    setImageFile(acceptedFile)
32
  }
33
 
34
  const handleSubmit = async () => {
35
    if (!validate()) return
36
 
37
    setLoading(true)
38
 
39
    const formData = new FormData()
40
    formData.append('image', imageFile)
41
 
42
    axios
43
      .post('/account-settings/image/upload', formData)
44
      .then(({ data: responseData }) => {
45
        const { data, success } = responseData
46
 
47
        if (!success) {
48
          const errorMessage =
49
            typeof data === 'string'
50
              ? data
51
              : 'Ha ocurrido un error, Por favor intente más tarde'
52
          throw new Error(errorMessage)
53
        }
54
 
55
        sessionStorage.setItem('user_session_image', data)
56
        addNotification({
57
          style: 'success',
58
          msg: 'Imagen cambiada exitosamente'
59
        })
60
      })
61
      .catch((err) => addNotification({ style: 'danger', msg: err.message }))
62
      .finally(() => setLoading(false))
63
  }
64
 
65
  const validate = () => {
66
    let errorMsg = ''
67
 
68
    if (!imageFile) {
69
      errorMsg = 'Por favor ingrese una imagen'
70
      setError(errorMsg)
71
      return false
72
    }
73
 
74
    return true
75
  }
76
 
77
  const getUserImage = () => {
78
    setLoading(true)
79
 
80
    axios
81
      .get('/account-settings/image/upload')
82
      .then(({ data: responseData }) => {
83
        const { data, success } = responseData
84
 
85
        if (!success) {
86
          throw new Error(data)
87
        }
88
 
89
        setImageUrl(data)
90
      })
91
      .catch((err) => {
92
        addNotification({ style: 'danger', msg: err.message })
93
      })
94
      .finally(() => setLoading(false))
95
  }
96
 
97
  useEffect(() => {
98
    getUserImage()
99
  }, [])
100
 
101
  return (
102
    <Widget>
103
      <Widget.Header title='Cambiar Imagen' />
104
 
105
      <Widget.Body>
106
        {imageUrl && (
107
          <Avatar
108
            src={imageUrl}
109
            sx={{ width: 100, height: 100, margin: 'auto' }}
110
          />
111
        )}
112
 
113
        <DropzoneComponent onUploaded={handleOnDropImage} modalType='IMAGE' />
114
        {error ? <FormErrorFeedback>{error}</FormErrorFeedback> : null}
115
 
116
        {loading && <Spinner />}
117
 
118
        <Button
3156 stevensc 119
          color='primary'
2781 stevensc 120
          onClick={handleSubmit}
121
          styles={{ marginTop: '1rem' }}
122
          fullWidth
123
        >
124
          Guardar
125
        </Button>
126
      </Widget.Body>
127
    </Widget>
128
  )
129
}
130
 
131
const mapDispatchToProps = {
132
  addNotification: (notification) => addNotification(notification)
133
}
134
 
135
export default connect(null, mapDispatchToProps)(ChangeImage)