2020-04-21 16:00:05 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import Column from '../ui/components/column';
|
|
|
|
import {
|
|
|
|
SimpleForm,
|
|
|
|
FieldsGroup,
|
|
|
|
TextInput,
|
2020-04-21 17:22:00 -07:00
|
|
|
Checkbox,
|
2020-04-22 14:26:44 -07:00
|
|
|
FileChooser,
|
2020-04-21 16:00:05 -07:00
|
|
|
} from 'gabsocial/features/forms';
|
2020-04-22 14:26:44 -07:00
|
|
|
import ProfilePreview from './components/profile_preview';
|
2020-05-18 10:20:39 -07:00
|
|
|
import {
|
|
|
|
Map as ImmutableMap,
|
|
|
|
List as ImmutableList,
|
|
|
|
} from 'immutable';
|
2020-04-21 16:00:05 -07:00
|
|
|
import { patchMe } from 'gabsocial/actions/me';
|
2020-05-27 17:53:47 -07:00
|
|
|
import { unescape } from 'lodash';
|
2020-04-21 16:00:05 -07:00
|
|
|
|
2020-05-18 10:20:39 -07:00
|
|
|
const MAX_FIELDS = 4; // TODO: Make this dynamic by the instance
|
|
|
|
|
2020-04-21 16:00:05 -07:00
|
|
|
const messages = defineMessages({
|
|
|
|
heading: { id: 'column.edit_profile', defaultMessage: 'Edit profile' },
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapStateToProps = state => {
|
|
|
|
const me = state.get('me');
|
|
|
|
return {
|
|
|
|
account: state.getIn(['accounts', me]),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-18 10:20:39 -07:00
|
|
|
// Forces fields to be MAX_SIZE, filling empty values
|
|
|
|
const normalizeFields = fields => (
|
|
|
|
ImmutableList(fields).setSize(MAX_FIELDS).map(field =>
|
|
|
|
field ? field : ImmutableMap({ name: undefined, value: undefined })
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2020-04-21 16:00:05 -07:00
|
|
|
export default @connect(mapStateToProps)
|
|
|
|
@injectIntl
|
|
|
|
class EditProfile extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
dispatch: PropTypes.func.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
account: ImmutablePropTypes.map,
|
|
|
|
};
|
|
|
|
|
2020-04-21 17:22:00 -07:00
|
|
|
state = {
|
|
|
|
isLoading: false,
|
2020-05-18 10:20:39 -07:00
|
|
|
fields: normalizeFields(Array.from({ length: MAX_FIELDS })),
|
2020-04-21 16:00:05 -07:00
|
|
|
}
|
|
|
|
|
2020-04-22 15:04:08 -07:00
|
|
|
makePreviewAccount = () => {
|
|
|
|
const { account } = this.props;
|
|
|
|
return account.merge(ImmutableMap({
|
|
|
|
header: this.state.header,
|
|
|
|
avatar: this.state.avatar,
|
|
|
|
display_name: this.state.display_name,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:20:39 -07:00
|
|
|
getFieldParams = () => {
|
|
|
|
let params = ImmutableMap();
|
|
|
|
this.state.fields.forEach((f, i) =>
|
|
|
|
params = params
|
|
|
|
.set(`fields_attributes[${i}][name]`, f.get('name'))
|
|
|
|
.set(`fields_attributes[${i}][value]`, f.get('value'))
|
|
|
|
);
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:22:00 -07:00
|
|
|
getParams = () => {
|
|
|
|
const { state } = this;
|
2020-05-18 10:20:39 -07:00
|
|
|
return Object.assign({
|
2020-04-21 17:22:00 -07:00
|
|
|
discoverable: state.discoverable,
|
|
|
|
bot: state.bot,
|
|
|
|
display_name: state.display_name,
|
|
|
|
note: state.note,
|
2020-04-22 15:04:08 -07:00
|
|
|
avatar: state.avatar_file,
|
|
|
|
header: state.header_file,
|
2020-04-21 17:22:00 -07:00
|
|
|
locked: state.locked,
|
2020-05-18 10:20:39 -07:00
|
|
|
}, this.getFieldParams().toJS());
|
2020-04-21 16:00:05 -07:00
|
|
|
}
|
|
|
|
|
2020-04-22 15:04:08 -07:00
|
|
|
getFormdata = () => {
|
|
|
|
const data = this.getParams();
|
|
|
|
let formData = new FormData();
|
|
|
|
for (let key in data) {
|
2020-05-18 10:20:39 -07:00
|
|
|
const shouldAppend = Boolean(data[key] || key.startsWith('fields_attributes'));
|
|
|
|
if (shouldAppend) formData.append(key, data[key] || '');
|
2020-04-22 15:04:08 -07:00
|
|
|
}
|
|
|
|
return formData;
|
|
|
|
}
|
|
|
|
|
2020-04-21 16:00:05 -07:00
|
|
|
handleSubmit = (event) => {
|
|
|
|
const { dispatch } = this.props;
|
2020-04-22 15:04:08 -07:00
|
|
|
dispatch(patchMe(this.getFormdata())).then(() => {
|
2020-04-21 16:00:05 -07:00
|
|
|
this.setState({ isLoading: false });
|
|
|
|
}).catch((error) => {
|
|
|
|
this.setState({ isLoading: false });
|
|
|
|
});
|
|
|
|
this.setState({ isLoading: true });
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:22:00 -07:00
|
|
|
componentWillMount() {
|
|
|
|
const { account } = this.props;
|
2020-05-17 10:12:56 -07:00
|
|
|
const sourceData = account.get('source');
|
2020-05-18 10:20:39 -07:00
|
|
|
const accountData = account.merge(sourceData).delete('source');
|
|
|
|
const fields = normalizeFields(accountData.get('fields'));
|
|
|
|
const initialState = accountData.set('fields', fields);
|
|
|
|
this.setState(initialState.toObject());
|
2020-04-21 17:22:00 -07:00
|
|
|
}
|
|
|
|
|
2020-05-27 17:53:47 -07:00
|
|
|
componentDidMount() {
|
|
|
|
const display_name = unescape(this.state.display_name);
|
|
|
|
this.setState({ display_name: display_name });
|
|
|
|
const note = unescape(this.state.note);
|
|
|
|
this.setState({ note: note });
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:22:00 -07:00
|
|
|
handleCheckboxChange = e => {
|
|
|
|
this.setState({ [e.target.name]: e.target.checked });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleTextChange = e => {
|
|
|
|
this.setState({ [e.target.name]: e.target.value });
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:20:39 -07:00
|
|
|
handleFieldChange = (i, key) => {
|
|
|
|
return (e) => {
|
|
|
|
this.setState({
|
|
|
|
fields: this.state.fields.setIn([i, key], e.target.value),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-22 15:04:08 -07:00
|
|
|
handleFileChange = e => {
|
|
|
|
const { name } = e.target;
|
|
|
|
const [file] = e.target.files || [];
|
|
|
|
const url = file ? URL.createObjectURL(file) : this.state[name];
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
[name]: url,
|
|
|
|
[`${name}_file`]: file,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-21 16:00:05 -07:00
|
|
|
render() {
|
2020-04-21 17:22:00 -07:00
|
|
|
const { intl } = this.props;
|
2020-04-21 16:00:05 -07:00
|
|
|
|
|
|
|
return (
|
2020-04-21 17:24:57 -07:00
|
|
|
<Column icon='user' heading={intl.formatMessage(messages.heading)} backBtnSlim>
|
2020-04-21 16:00:05 -07:00
|
|
|
<SimpleForm onSubmit={this.handleSubmit}>
|
|
|
|
<fieldset disabled={this.state.isLoading}>
|
|
|
|
<FieldsGroup>
|
|
|
|
<TextInput
|
|
|
|
label='Display name'
|
|
|
|
name='display_name'
|
2020-04-21 17:22:00 -07:00
|
|
|
value={this.state.display_name}
|
|
|
|
maxLength={30}
|
2020-04-22 14:38:16 -07:00
|
|
|
size={30}
|
2020-04-21 17:22:00 -07:00
|
|
|
onChange={this.handleTextChange}
|
2020-04-21 16:00:05 -07:00
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
label='Bio'
|
|
|
|
name='note'
|
2020-04-21 17:22:00 -07:00
|
|
|
value={this.state.note}
|
|
|
|
onChange={this.handleTextChange}
|
|
|
|
/>
|
2020-04-22 14:26:44 -07:00
|
|
|
<div className='fields-row'>
|
|
|
|
<div className='fields-row__column fields-row__column-6'>
|
2020-04-22 15:04:08 -07:00
|
|
|
<ProfilePreview account={this.makePreviewAccount()} />
|
2020-04-22 14:26:44 -07:00
|
|
|
</div>
|
|
|
|
<div className='fields-row__column fields-group fields-row__column-6'>
|
|
|
|
<FileChooser
|
|
|
|
label='Header'
|
|
|
|
name='header'
|
|
|
|
hint='PNG, GIF or JPG. At most 2 MB. Will be downscaled to 1500x500px'
|
2020-04-22 15:04:08 -07:00
|
|
|
onChange={this.handleFileChange}
|
2020-04-22 14:26:44 -07:00
|
|
|
/>
|
|
|
|
<FileChooser
|
|
|
|
label='Avatar'
|
|
|
|
name='avatar'
|
|
|
|
hint='PNG, GIF or JPG. At most 2 MB. Will be downscaled to 400x400px'
|
2020-04-22 15:04:08 -07:00
|
|
|
onChange={this.handleFileChange}
|
2020-04-22 14:26:44 -07:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-04-21 17:22:00 -07:00
|
|
|
<Checkbox
|
|
|
|
label='Lock account'
|
|
|
|
hint='Requires you to manually approve followers'
|
|
|
|
name='locked'
|
|
|
|
checked={this.state.locked}
|
|
|
|
onChange={this.handleCheckboxChange}
|
|
|
|
/>
|
|
|
|
<Checkbox
|
|
|
|
label='This is a bot account'
|
|
|
|
hint='This account mainly performs automated actions and might not be monitored'
|
|
|
|
name='bot'
|
|
|
|
checked={this.state.bot}
|
|
|
|
onChange={this.handleCheckboxChange}
|
2020-04-21 16:00:05 -07:00
|
|
|
/>
|
|
|
|
</FieldsGroup>
|
2020-05-18 10:20:39 -07:00
|
|
|
<FieldsGroup>
|
|
|
|
<div className='fields-row__column fields-group'>
|
|
|
|
<div className='input with_block_label'>
|
|
|
|
<label>Profile metadata</label>
|
|
|
|
<span className='hint'>You can have up to {MAX_FIELDS} items displayed as a table on your profile</span>
|
|
|
|
{
|
|
|
|
this.state.fields.map((field, i) => (
|
|
|
|
<div className='row' key={i}>
|
|
|
|
<TextInput
|
|
|
|
placeholder='Label'
|
|
|
|
value={field.get('name')}
|
|
|
|
onChange={this.handleFieldChange(i, 'name')}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
placeholder='Content'
|
|
|
|
value={field.get('value')}
|
|
|
|
onChange={this.handleFieldChange(i, 'value')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</FieldsGroup>
|
2020-04-21 16:00:05 -07:00
|
|
|
</fieldset>
|
|
|
|
<div className='actions'>
|
|
|
|
<button name='button' type='submit' className='btn button button-primary'>Save changes</button>
|
|
|
|
</div>
|
|
|
|
</SimpleForm>
|
|
|
|
</Column>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|