// - Import react components
import React, { Component } from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { List, ListItem } from 'material-ui/List'
import Paper from 'material-ui/Paper'
import Dialog from 'material-ui/Dialog';
import FlatButton from 'material-ui/FlatButton';
import RaisedButton from 'material-ui/RaisedButton';
import { grey400, grey800, darkBlack, lightBlack } from 'material-ui/styles/colors'
import IconButton from 'material-ui/IconButton'
import TextField from 'material-ui/TextField'
import MenuItem from 'material-ui/MenuItem'
import SvgRemoveImage from 'material-ui/svg-icons/content/remove-circle'
import SvgCamera from 'material-ui/svg-icons/image/photo-camera'
import IconMenu from 'material-ui/IconMenu'
import MoreVertIcon from 'material-ui/svg-icons/navigation/more-vert'
// - Import app components
import ImageGallery from 'ImageGallery'
import Img from 'Img'
import UserAvatar from 'UserAvatar'
// - Import API
import * as AuthAPI from 'AuthAPI'
import * as PostAPI from 'PostAPI'
// - Import actions
import * as imageGalleryActions from 'imageGalleryActions'
import * as postActions from 'postActions'
// - Create PostWrite component class
export class PostWrite extends Component {
static propTypes = {
/**
* If it's true post writing page will be open
*/
open: PropTypes.bool,
/**
* Recieve request close function
*/
onRequestClose: PropTypes.func,
/**
* Post write style
*/
style: PropTypes.object,
/**
* If it's true, post will be in edit view
*/
edit: PropTypes.bool.isRequired,
/**
* The text of post in editing state
*/
text: PropTypes.string,
/**
* The image of post in editing state
*/
image: PropTypes.string,
/**
* If post state is editing this id sould be filled with post identifier
*/
id: PropTypes.string
}
/**
* Component constructor
* @param {object} props is an object properties of component
*/
constructor(props) {
super(props)
// Default state
this.state = {
/**
* Post text
*/
postText: this.props.edit ? this.props.text : '',
/**
* The URL image of the post
*/
image: this.props.edit ? this.props.image : '',
/**
* The path identifier of image on the server
*/
imageFullPath: this.props.edit ? this.props.imageFullPath : '',
/**
* If it's true gallery will be open
*/
galleryOpen: false,
/**
* If it's true post button will be disabled
*/
disabledPost: true,
/**
* If it's true comment will be disabled on post
*/
disableComments: this.props.edit ? this.props.disableComments : false,
/**
* If it's true share will be disabled on post
*/
disableSharing: this.props.edit ? this.props.disableSharing : false,
}
// Binding functions to `this`
this.handleOnChange = this.handleOnChange.bind(this)
this.handleCloseGallery = this.handleCloseGallery.bind(this)
this.handleOpenGallery = this.handleOpenGallery.bind(this)
this.onRequestSetImage = this.onRequestSetImage.bind(this)
this.handlePost = this.handlePost.bind(this)
this.handleRemoveImage = this.handleRemoveImage.bind(this)
this.handleToggleComments = this.handleToggleComments.bind(this)
this.handleToggleSharing = this.handleToggleSharing.bind(this)
}
/**
* Toggle comments of the post to disable/enable
*
*
* @memberof PostWrite
*/
handleToggleComments = () => {
this.setState({
disableComments: !this.state.disableComments,
disabledPost: false
})
}
/**
* Toggle sharing of the post to disable/enable
*
*
* @memberof PostWrite
*/
handleToggleSharing = () => {
this.setState({
disableSharing: !this.state.disableSharing,
disabledPost: false
})
}
/**
* Romove the image of post
*
*
* @memberof PostWrite
*/
handleRemoveImage = () => {
this.setState({
image: '',
imageFullPath: '',
disabledPost: false
})
}
/**
* Handle send post to the server
* @param {event} evt passed by clicking on the post button
*/
handlePost = (evt) => {
const {
image,
imageFullPath,
disableComments,
disableSharing,
postText } = this.state
const {
id,
avatar,
name,
edit,
onRequestClose,
post,
update } = this.props
var tags = PostAPI.getContentTags(postText)
// In edit status we should fire update if not we should fire post function
if (!edit) {
if (image !== '') {
post({
body: postText,
tags: tags,
image: image,
imageFullPath: imageFullPath,
avatar: avatar,
name: name,
disableComments: disableComments,
disableSharing: disableSharing
}, onRequestClose)
}
else {
post({
body: postText,
tags: tags,
avatar: avatar,
name: name,
disableComments: disableComments,
disableSharing: disableSharing
}, onRequestClose)
}
}
// In edit status we pass post to update functions
else {
update({
id: id,
body: postText,
tags: tags,
image: image,
imageFullPath: imageFullPath,
disableComments: disableComments,
disableSharing: disableSharing
}, onRequestClose)
}
}
/**
* Set post image url
*/
onRequestSetImage = (url, fullPath) => {
this.setState({
image: url,
imageFullPath: fullPath,
disabledPost: false
})
}
/**
* 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
*/
handleOnChange = (evt, data) => {
this.setState({ postText: data })
if (data.length === 0 || data.trim() === '' || (this.props.edit && data.trim() === this.props.text)) {
this.setState({
postText: data,
disabledPost: true
})
}
else {
this.setState({
postText: data,
disabledPost: false
})
}
}
/**
* Close image gallery
*/
handleCloseGallery = () => {
this.setState({
galleryOpen: false
})
}
/**
* Open image gallery
*/
handleOpenGallery = () => {
this.setState({
galleryOpen: true
})
}
componentWillReceiveProps(nextProps) {
if (!nextProps.open) {
this.setState({
/**
* Post text
*/
postText: this.props.edit ? this.props.text : '',
/**
* The image of the post
*/
image: this.props.edit ? this.props.image : '',
/**
* If it's true gallery will be open
*/
galleryOpen: false,
/**
* If it's true post button will be disabled
*/
disabledPost: true,
/**
* If it's true comment will be disabled on post
*/
disableComments: this.props.edit ? this.props.disableComments : false,
/**
* If it's true share will be disabled on post
*/
disableSharing: this.props.edit ? this.props.disableSharing : false,
})
}
}
/**
* Reneder component DOM
* @return {react element} return the DOM which rendered by component
*/
render() {
const iconButtonElement = (