import ImmutablePropTypes from 'react-immutable-proptypes' import ImmutablePureComponent from 'react-immutable-pure-component' import { defineMessages, injectIntl } from 'react-intl' import isObject from 'lodash.isobject' import { changeValue, submit, setUp, reset } from '../actions/group_editor' import ColumnIndicator from '../components/column_indicator'; import Button from '../components/button' import Divider from '../components/divider' import Input from '../components/input' import Text from '../components/text' import Textarea from '../components/textarea' import FileInput from '../components/file_input' const messages = defineMessages({ title: { id: 'groups.form.title', defaultMessage: 'Enter a new group title' }, description: { id: 'groups.form.description', defaultMessage: 'Enter the group description' }, coverImage: { id: 'groups.form.coverImage', defaultMessage: 'Upload a banner image' }, coverImageChange: { id: 'groups.form.coverImageChange', defaultMessage: 'Banner image selected' }, create: { id: 'groups.form.create', defaultMessage: 'Create group' }, update: { id: 'groups.form.update', defaultMessage: 'Update group' }, }) const mapStateToProps = (state, { params }) => { console.log('params:', params) const groupId = isObject(params) ? params['id'] : null const group = state.getIn(['groups', groupId]) return { group, error: groupId && !group, title: state.getIn(['group_editor', 'title']), description: state.getIn(['group_editor', 'description']), coverImage: state.getIn(['group_editor', 'coverImage']), disabled: state.getIn(['group_editor', 'isSubmitting']), } } const mapDispatchToProps = dispatch => ({ onTitleChange: value => dispatch(changeValue('title', value)), onDescriptionChange: value => dispatch(changeValue('description', value)), onCoverImageChange: value => dispatch(changeValue('coverImage', value)), onSubmit: routerHistory => dispatch(submit(routerHistory)), reset: () => dispatch(reset()), setUp: group => dispatch(setUp(group)), }) export default @connect(mapStateToProps, mapDispatchToProps) @injectIntl class Create extends ImmutablePureComponent { static contextTypes = { router: PropTypes.object } static propTypes = { group: ImmutablePropTypes.map, title: PropTypes.string.isRequired, description: PropTypes.string.isRequired, coverImage: PropTypes.object, disabled: PropTypes.bool, intl: PropTypes.object.isRequired, onTitleChange: PropTypes.func.isRequired, onSubmit: PropTypes.func.isRequired, } componentWillMount() { if (!this.props.group) { this.props.reset() } else { this.props.setUp(this.props.group) } } componentWillReceiveProps(nextProps) { if (this.props.group !== nextProps.group && !!nextProps.group) { this.props.setUp(nextProps.group) } } handleTitleChange = e => { this.props.onTitleChange(e.target.value) } handleDescriptionChange = e => { this.props.onDescriptionChange(e.target.value) } handleCoverImageChange = e => { this.props.onCoverImageChange(e.target.files[0]) } handleSubmit = e => { e.preventDefault() this.props.onSubmit(this.context.router.history) } render() { const { group, error, title, description, coverImage, disabled, intl } = this.props if (!group && error) { return } return (