cinny/src/app/molecules/image-upload/ImageUpload.jsx

64 lines
1.7 KiB
React
Raw Normal View History

2021-09-09 07:47:26 +02:00
import React, { useRef } from 'react';
import PropTypes from 'prop-types';
import initMatrix from '../../../client/initMatrix';
2021-09-13 05:25:58 +02:00
import SettingsIC from '../../../../public/res/ic/outlined/settings.svg';
import Avatar from '../../atoms/avatar/Avatar';
2021-09-09 07:47:26 +02:00
2021-09-13 05:25:58 +02:00
import RawIcon from '../../atoms/system-icons/RawIcon';
2021-09-09 07:47:26 +02:00
import './ImageUpload.scss';
function ImageUpload({
text, bgColor, imageSrc, onUpload,
}) {
const uploadImageRef = useRef(null);
2021-09-09 08:06:25 +02:00
// Uploads image and passes resulting URI to onUpload function provided in component props.
2021-09-09 07:47:26 +02:00
function uploadImage(e) {
const file = e.target.files.item(0);
if (file !== null) { // TODO Add upload progress spinner
initMatrix.matrixClient.uploadContent(file, { onlyContentUri: false }).then((res) => {
if (res.content_uri !== null) {
onUpload({ content_uri: res.content_uri });
}
}, (err) => {
console.log(err); // TODO Replace with alert banner.
});
}
}
return (
2021-09-13 05:25:58 +02:00
<button type="button" className="img-upload" onClick={() => { uploadImageRef.current.click(); }}>
<div className="img-upload__mask">
2021-09-09 07:47:26 +02:00
<Avatar
imageSrc={imageSrc}
text={text.slice(0, 1)}
bgColor={bgColor}
size="large"
/>
</div>
2021-09-13 05:25:58 +02:00
<div className="img-upload__icon">
<RawIcon size="small" src={SettingsIC} />
2021-09-09 07:47:26 +02:00
</div>
<input onChange={uploadImage} style={{ display: 'none' }} ref={uploadImageRef} type="file" />
</button>
);
}
ImageUpload.defaultProps = {
text: null,
bgColor: 'transparent',
imageSrc: null,
onUpload: null,
};
ImageUpload.propTypes = {
text: PropTypes.string,
bgColor: PropTypes.string,
imageSrc: PropTypes.string,
onUpload: PropTypes.func,
};
export default ImageUpload;