gab-social/app/javascript/gabsocial/features/group_create.js

160 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-03-06 15:38:22 +00:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component'
2020-02-29 15:42:47 +00:00
import { defineMessages, injectIntl } from 'react-intl'
2020-03-06 15:38:22 +00:00
import isObject from 'lodash.isobject'
import { changeValue, submit, setUp, reset } from '../actions/group_editor'
import ColumnIndicator from '../components/column_indicator';
2020-03-05 15:44:17 +00:00
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'
2019-07-02 08:10:25 +01:00
const messages = defineMessages({
2019-07-18 20:37:53 +01:00
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' },
2019-07-17 19:22:19 +01:00
create: { id: 'groups.form.create', defaultMessage: 'Create group' },
2020-03-06 15:38:22 +00:00
update: { id: 'groups.form.update', defaultMessage: 'Update group' },
2020-02-29 15:42:47 +00:00
})
2019-07-02 08:10:25 +01:00
2020-03-06 15:38:22 +00:00
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']),
}
}
2019-07-02 08:10:25 +01:00
const mapDispatchToProps = dispatch => ({
2019-07-17 19:22:19 +01:00
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()),
2020-03-06 15:38:22 +00:00
setUp: group => dispatch(setUp(group)),
2020-02-29 15:42:47 +00:00
})
2019-07-02 08:10:25 +01:00
2020-02-25 16:04:44 +00:00
export default
@connect(mapStateToProps, mapDispatchToProps)
2019-07-02 08:10:25 +01:00
@injectIntl
2020-03-06 15:38:22 +00:00
class Create extends ImmutablePureComponent {
2019-07-02 08:10:25 +01:00
2019-07-17 19:22:19 +01:00
static contextTypes = {
router: PropTypes.object
}
static propTypes = {
2020-03-06 15:38:22 +00:00
group: ImmutablePropTypes.map,
2019-07-17 19:22:19 +01:00
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,
2020-02-29 15:42:47 +00:00
}
2019-07-17 19:22:19 +01:00
componentWillMount() {
2020-03-06 15:38:22 +00:00
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)
}
2019-07-17 19:22:19 +01:00
}
handleTitleChange = e => {
2020-02-29 15:42:47 +00:00
this.props.onTitleChange(e.target.value)
2019-07-17 19:22:19 +01:00
}
handleDescriptionChange = e => {
2020-02-29 15:42:47 +00:00
this.props.onDescriptionChange(e.target.value)
2019-07-17 19:22:19 +01:00
}
handleCoverImageChange = e => {
2020-02-29 15:42:47 +00:00
this.props.onCoverImageChange(e.target.files[0])
2019-07-17 19:22:19 +01:00
}
handleSubmit = e => {
2020-02-29 15:42:47 +00:00
e.preventDefault()
this.props.onSubmit(this.context.router.history)
2019-07-17 19:22:19 +01:00
}
2020-02-29 15:42:47 +00:00
render() {
2020-03-05 15:44:17 +00:00
const {
2020-03-06 15:38:22 +00:00
group,
error,
2020-03-05 15:44:17 +00:00
title,
description,
coverImage,
disabled,
intl
} = this.props
2019-07-17 19:22:19 +01:00
2020-03-06 15:38:22 +00:00
if (!group && error) {
return <ColumnIndicator type='missing' />
}
2019-07-17 19:22:19 +01:00
return (
2020-03-05 15:44:17 +00:00
<form onSubmit={this.handleSubmit}>
<Input
title={intl.formatMessage(messages.title)}
value={title}
disabled={disabled}
onChange={this.handleTitleChange}
placeholder={'New group title...'}
/>
<Divider invisible />
<Textarea
title={intl.formatMessage(messages.description)}
value={description}
disabled={disabled}
onChange={this.handleDescriptionChange}
placeholder={'Some group description...'}
/>
<Divider invisible />
<FileInput
title={intl.formatMessage(coverImage === null ? messages.coverImage : messages.coverImageChange)}
disabled={disabled}
onChange={this.handleCoverImageChange}
width='340px'
height='145px'
/>
<Divider invisible />
<Button
2020-03-11 23:56:18 +00:00
className={_s.ml10}
2020-03-05 15:44:17 +00:00
>
<Text color='white'>
2020-03-06 15:38:22 +00:00
{intl.formatMessage(!!group ? messages.update : messages.create)}
2020-03-05 15:44:17 +00:00
</Text>
</Button>
</form>
2020-02-29 15:42:47 +00:00
)
2019-07-17 19:22:19 +01:00
}
2019-07-02 08:10:25 +01:00
}