// - Impoer react components import React, { Component } from 'react' import PropTypes from 'prop-types' import { connect } from 'react-redux' import { GridList, GridTile } from 'material-ui/GridList' import IconButton from 'material-ui/IconButton' import Subheader from 'material-ui/Subheader' import StarBorder from 'material-ui/svg-icons/toggle/star-border' import FloatingActionButton from 'material-ui/FloatingActionButton'; import SvgUpload from 'material-ui/svg-icons/file/cloud-upload' import SvgAddImage from 'material-ui/svg-icons/image/add-a-photo' import SvgDelete from 'material-ui/svg-icons/action/delete' import { grey200, grey600 } from 'material-ui/styles/colors' import FlatButton from 'material-ui/FlatButton' import uuid from 'uuid' // - Import actions import * as imageGalleryActions from 'imageGalleryActions' import * as fileActions from 'fileActions' import * as globalActions from 'globalActions' // - Import app components import Img from 'Img' // - Import API import FileAPI from 'FileAPI' /** * Create ImageGallery component class */ export class ImageGallery extends Component { static propTypes = { /** * Callback function to ser image url on parent component */ open: PropTypes.func } /** * Component constructor * @param {object} props is an object properties of component */ constructor(props) { super(props); // Binding function to `this` this.close = this.close.bind(this) this.onFileChange = this.onFileChange.bind(this) this.handleSetImage = this.handleSetImage.bind(this) this.handleDeleteImage = this.handleDeleteImage.bind(this) this.imageList = this.imageList.bind(this) } /** * Handle set image * @param {event} evt passed by on click event on add image * @param {string} name is the name of the image */ handleSetImage = (evt, name) => { this.props.set(name) this.close() } /** * Handle delete image * @param {event} evt passed by on click event on delete image * @param {integer} id is the image identifier which selected to delete */ handleDeleteImage = (evt, id) => { this.props.deleteImage(id) } componentDidMount() { window.addEventListener("onSendResizedImage", this.handleSendResizedImage) } componentWillUnmount() { window.removeEventListener("onSendResizedImage", this.handleSendResizedImage) } /** * Handle send image resize event that pass the resized image * * * @memberof ImageGallery */ handleSendResizedImage = (event) => { const { resizedImage, fileName } = event.detail this.props.saveImage(resizedImage, fileName) } /** * Handle on change file upload */ onFileChange = (evt) => { const extension = FileAPI.getExtension(evt.target.files[0].name) var fileName = (`${uuid()}.${extension}`) let image = FileAPI.constraintImage(evt.target.files[0], fileName) } /** * Hide image gallery */ close = () => { this.props.close() } imageList = () => { console.log('===================================='); console.log(this.props.images); console.log('===================================='); return this.props.images.map((image, index) => { return ( this.handleDeleteImage(evt, image.id)} />} subtitle={} actionIcon={ this.handleSetImage(evt, image.name)} />} >
) }) } /** * When the post text changed * @param {event} evt is an event passed by change post text callback funciton * @param {string} data is the post content which user writes */ render() { /** * Component styles * @type {Object} */ const styles = { root: { display: 'flex', flexWrap: 'wrap', justifyContent: 'space-around', }, gridList: { width: 500, height: 450, overflowY: 'auto', }, uploadButton: { verticalAlign: 'middle', }, uploadInput: { cursor: 'pointer', position: 'absolute', top: 0, bottom: 0, right: 0, left: 0, width: '100%', opacity: 0, } } return (
{this.imageList()}
) } } /** * Map dispatch to props * @param {func} dispatch is the function to dispatch action to reducers * @param {object} ownProps is the props belong to component * @return {object} props of component */ const mapDispatchToProps = (dispatch, ownProps) => { return { saveImage: (file, fileName) => { dispatch(imageGalleryActions.dbUploadImage(file, fileName)) }, deleteImage: (id) => { dispatch(imageGalleryActions.dbDeleteImage(id)) } } } /** * Map state to props * @param {object} state is the obeject from redux store * @param {object} ownProps is the props belong to component * @return {object} props of component */ const mapStateToProps = (state) => { return { images: state.imageGallery.images, avatar: state.user.info && state.user.info[state.authorize.uid] ? state.user.info[state.authorize.uid].avatar : '' } } // - Connect component to redux store export default connect(mapStateToProps, mapDispatchToProps)(ImageGallery)