3719 |
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((response) => {
|
|
|
45 |
const { data, success } = response.data;
|
|
|
46 |
|
|
|
47 |
if (!success) {
|
|
|
48 |
const errorMessage =
|
|
|
49 |
typeof data === 'string' ? data : 'Ha ocurrido un error, Por favor intente más tarde';
|
|
|
50 |
throw new Error(errorMessage);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
sessionStorage.setItem('user_session_image', data);
|
|
|
54 |
addNotification({
|
|
|
55 |
style: 'success',
|
|
|
56 |
msg: 'Imagen cambiada exitosamente'
|
|
|
57 |
});
|
|
|
58 |
})
|
|
|
59 |
.catch((err) => addNotification({ style: 'danger', msg: err.message }))
|
|
|
60 |
.finally(() => setLoading(false));
|
|
|
61 |
};
|
|
|
62 |
|
|
|
63 |
const validate = () => {
|
|
|
64 |
let errorMsg = '';
|
|
|
65 |
|
|
|
66 |
if (!imageFile) {
|
|
|
67 |
errorMsg = 'Por favor ingrese una imagen';
|
|
|
68 |
setError(errorMsg);
|
|
|
69 |
return false;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
return true;
|
|
|
73 |
};
|
|
|
74 |
|
|
|
75 |
const getUserImage = () => {
|
|
|
76 |
setLoading(true);
|
|
|
77 |
|
|
|
78 |
axios
|
|
|
79 |
.get('/account-settings/image/upload')
|
|
|
80 |
.then((response) => {
|
|
|
81 |
const { data, success } = response.data;
|
|
|
82 |
|
|
|
83 |
if (!success) {
|
|
|
84 |
throw new Error(data);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
setImageUrl(data);
|
|
|
88 |
})
|
|
|
89 |
.catch((err) => {
|
|
|
90 |
addNotification({ style: 'danger', msg: err.message });
|
|
|
91 |
})
|
|
|
92 |
.finally(() => setLoading(false));
|
|
|
93 |
};
|
|
|
94 |
|
|
|
95 |
useEffect(() => {
|
|
|
96 |
getUserImage();
|
|
|
97 |
}, []);
|
|
|
98 |
|
|
|
99 |
return (
|
|
|
100 |
<Widget>
|
|
|
101 |
<Widget.Header title='Cambiar Imagen' />
|
|
|
102 |
|
|
|
103 |
<Widget.Body>
|
|
|
104 |
{imageUrl && <Avatar src={imageUrl} sx={{ width: 100, height: 100, margin: 'auto' }} />}
|
|
|
105 |
|
|
|
106 |
<DropzoneComponent onUploaded={handleOnDropImage} modalType='IMAGE' />
|
|
|
107 |
{error ? <FormErrorFeedback>{error}</FormErrorFeedback> : null}
|
|
|
108 |
|
|
|
109 |
{loading && <Spinner />}
|
|
|
110 |
|
|
|
111 |
<Button color='primary' onClick={handleSubmit} styles={{ marginTop: '1rem' }} fullWidth>
|
|
|
112 |
Guardar
|
|
|
113 |
</Button>
|
|
|
114 |
</Widget.Body>
|
|
|
115 |
</Widget>
|
|
|
116 |
);
|
|
|
117 |
};
|
|
|
118 |
|
|
|
119 |
const mapDispatchToProps = {
|
|
|
120 |
addNotification: (notification) => addNotification(notification)
|
|
|
121 |
};
|
|
|
122 |
|
|
|
123 |
export default connect(null, mapDispatchToProps)(ChangeImage);
|