bigbuffet-rw/app/soapbox/features/groups/create/index.js

116 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { changeValue, submit, reset } from '../../../actions/group_editor';
import { defineMessages, injectIntl } from 'react-intl';
import classNames from 'classnames';
const messages = defineMessages({
2020-04-14 11:44:40 -07: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' },
create: { id: 'groups.form.create', defaultMessage: 'Create group' },
2020-03-27 13:59:38 -07:00
});
const mapStateToProps = state => ({
2020-04-14 11:44:40 -07:00
title: state.getIn(['group_editor', 'title']),
description: state.getIn(['group_editor', 'description']),
coverImage: state.getIn(['group_editor', 'coverImage']),
disabled: state.getIn(['group_editor', 'isSubmitting']),
2020-03-27 13:59:38 -07:00
});
const mapDispatchToProps = dispatch => ({
2020-04-14 11:44:40 -07: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-27 13:59:38 -07:00
});
export default @connect(mapStateToProps, mapDispatchToProps)
@injectIntl
class Create extends React.PureComponent {
2020-04-14 13:45:38 -07:00
static contextTypes = {
router: PropTypes.object,
}
2020-03-27 13:59:38 -07:00
2020-04-14 13:45:38 -07:00
static propTypes = {
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-04-14 14:37:17 -07:00
reset: PropTypes.func.isRequired,
onDescriptionChange: PropTypes.func.isRequired,
onCoverImageChange: PropTypes.func.isRequired,
2020-04-14 13:45:38 -07:00
};
2020-03-27 13:59:38 -07:00
2020-04-14 13:45:38 -07:00
componentWillMount() {
this.props.reset();
}
2020-03-27 13:59:38 -07:00
2020-04-14 13:45:38 -07:00
handleTitleChange = e => {
this.props.onTitleChange(e.target.value);
}
2020-03-27 13:59:38 -07:00
2020-04-14 13:45:38 -07:00
handleDescriptionChange = e => {
this.props.onDescriptionChange(e.target.value);
}
2020-03-27 13:59:38 -07:00
2020-04-14 13:45:38 -07:00
handleCoverImageChange = e => {
this.props.onCoverImageChange(e.target.files[0]);
}
2020-03-27 13:59:38 -07:00
2020-04-14 13:45:38 -07:00
handleSubmit = e => {
e.preventDefault();
this.props.onSubmit(this.context.router.history);
}
2020-03-27 13:59:38 -07:00
render() {
2020-04-14 13:45:38 -07:00
const { title, description, coverImage, disabled, intl } = this.props;
2020-03-27 13:59:38 -07:00
2020-04-14 13:45:38 -07:00
return (
<form className='group-form' onSubmit={this.handleSubmit}>
<div>
<input
className='standard'
type='text'
value={title}
disabled={disabled}
onChange={this.handleTitleChange}
placeholder={intl.formatMessage(messages.title)}
/>
</div>
<div>
<textarea
className='standard'
type='text'
value={description}
disabled={disabled}
onChange={this.handleDescriptionChange}
placeholder={intl.formatMessage(messages.description)}
/>
</div>
<div>
<label htmlFor='group_cover_image' className={classNames('group-form__file-label', { 'group-form__file-label--selected': coverImage !== null })}>
{intl.formatMessage(coverImage === null ? messages.coverImage : messages.coverImageChange)}
</label>
<input
type='file'
className='group-form__file'
id='group_cover_image'
disabled={disabled}
onChange={this.handleCoverImageChange}
/>
<button className='standard-small'>{intl.formatMessage(messages.create)}</button>
</div>
</form>
);
}
2020-03-27 13:59:38 -07:00
}