/**
*
*
* ImgPreview
*
*/
import React from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import { cloneDeep, get, isEmpty, isObject, size } from 'lodash';
import cn from 'classnames';
import BkgImg from 'assets/icons/icon_upload.svg';
import ImgPreviewArrow from 'components/ImgPreviewArrow';
import ImgPreviewRemoveIcon from 'components/ImgPreviewRemoveIcon';
import ImgPreviewHint from 'components/ImgPreviewHint';
import styles from './styles.scss';
class ImgPreview extends React.Component {
state = {
imgURL: '',
isDraging: false,
isOver: false,
isOverArrow: false,
isImg: false,
};
componentDidMount() {
// We don't need the generateImgURL function here since the compo will
// always have an init value here
this.setState({
imgURL: get(this.props.files, ['0', 'url'], ''),
isImg: this.isPictureType(get(this.props.files, ['0', 'name'], '')),
});
}
componentWillReceiveProps(nextProps) {
if (nextProps.isUploading !== this.props.isUploading) {
const lastFile = nextProps.files.slice(-1)[0];
this.generateImgURL(lastFile);
this.updateFilePosition(nextProps.files.length - 1);
}
// Update the preview
if (nextProps.didDeleteFile !== this.props.didDeleteFile) {
const file = nextProps.files[nextProps.position] || '';
this.generateImgURL(file)
}
// Slide pictures
if (nextProps.position !== this.props.position) {
const file = nextProps.files[nextProps.position] || '';
this.generateImgURL(file);
}
}
componentDidCatch(error, info) {
console.log('An error occured in ImgPreview', info);
}
getFileType = (fileName) => fileName.split('.').slice(-1)[0];
/**
* [generateImgURL description]
* @param {FileList} files
* @return {URL}
*/
generateImgURL = (file) => {
if (this.isPictureType(file.name)) {
const reader = new FileReader();
reader.onloadend = () => {
this.setState({
imgURL: reader.result,
isImg: true,
});
}
reader.readAsDataURL(file);
} else {
this.setState({ isImg: false, imgURL: file.name });
}
}
handleClick = (type) => {
const { files, position } = this.props;
let file;
let nextPosition;
switch (type) {
case 'right':
file = files[position + 1] || files[0];
nextPosition = files[position + 1] ? position + 1 : 0;
break;
case 'left':
file = files[position - 1] || files[files.length - 1];
nextPosition = files[position - 1] ? position - 1 : files.length - 1;
break;
default:
// Do nothing
}
// Update the parent position
this.updateFilePosition(nextPosition);
}
handleDragEnter = (e) => {
e.preventDefault();
e.stopPropagation();
this.setState({ isDraging: true });
}
handleDragLeave = (e) => {
e.preventDefault();
e.stopPropagation();
this.setState({ isDraging: false });
}
handleDragOver = (e) => {
e.preventDefault();
e.stopPropagation();
}
handleDrop = (e) => {
this.setState({ isDraging: false });
this.props.onDrop(e);
}
handleMouseEnter = (e) => {
e.preventDefault();
e.stopPropagation();
this.setState({ isOver: true });
}
handleMouseLeave = (e) => {
e.preventDefault();
e.stopPropagation();
this.setState({ isOver: false });
}
// TODO change logic to depend on the type
isPictureType = (fileName) => /\.(jpe?g|png|gif)$/i.test(fileName);
renderContent = () => {
const fileType = this.getFileType(this.state.imgURL);
if (this.state.isImg) {
return (
);
}
return (