/**
*
*
* 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,
isImg: false,
position: 0,
};
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);
}
}
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 { position } = this.state;
const { files } = 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
}
if (!file.url) {
this.generateImgURL(file)
} else {
this.setState({ imgURL: file.url, isImg: this.isPictureType(file.url) });
}
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) => this.setState({ isOver: false });
// TODO change logic to depend on the type
isPictureType = (fileName) => /\.(jpe?g|png|gif)$/i.test(fileName);
removeFile = (e) => {
e.preventDefault();
e.stopPropagation();
const value = cloneDeep(this.props.files);
value.splice(this.state.position, 1);
const target = {
name: this.props.name,
type: 'file',
value,
};
this.props.onChange({ target });
if (size(value) === 0) {
this.setState({ position: 0, imgURL: '' });
return this.props.updateFilePosition(0);
}
// Display the last file
const nextFile = value.slice(-1)[0];
const nextPosition = value.length -1;
this.updateFilePosition(nextPosition);
if (!nextFile.url) {
this.generateImgURL(nextFile);
} else {
this.setState({ imgURL: nextFile.url });
}
}
renderContent = () => {
const fileType = this.getFileType(this.state.imgURL);
if (this.state.isImg) {
return (
);
}
return (