bigbuffet-rw/app/soapbox/features/edit_profile/index.js

320 lines
13 KiB
JavaScript
Raw Normal View History

2020-04-21 16:00:05 -07:00
import React from 'react';
import { connect } from 'react-redux';
2020-06-06 13:52:33 -07:00
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
2020-04-21 16:00:05 -07:00
import ImmutablePureComponent from 'react-immutable-pure-component';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import snackbar from 'soapbox/actions/snackbar';
2020-04-21 16:00:05 -07:00
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,
SimpleTextarea,
2020-05-28 15:52:07 -07:00
} from 'soapbox/features/forms';
2020-04-22 14:26:44 -07:00
import ProfilePreview from './components/profile_preview';
import {
Map as ImmutableMap,
List as ImmutableList,
} from 'immutable';
2020-05-28 15:52:07 -07:00
import { patchMe } from 'soapbox/actions/me';
import { updateNotificationSettings } from 'soapbox/actions/accounts';
2020-08-09 13:13:36 -07:00
import { unescape } from 'lodash';
2021-03-15 19:50:16 -07:00
import { isVerified } from 'soapbox/utils/accounts';
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
import { getFeatures } from 'soapbox/utils/features';
2020-04-21 16:00:05 -07:00
2021-06-16 12:20:57 -07:00
const hidesNetwork = account => {
const pleroma = account.get('pleroma');
if (!pleroma) return false;
const { hide_followers, hide_follows, hide_followers_count, hide_follows_count } = pleroma.toJS();
return hide_followers && hide_follows && hide_followers_count && hide_follows_count;
};
2020-04-21 16:00:05 -07:00
const messages = defineMessages({
heading: { id: 'column.edit_profile', defaultMessage: 'Edit profile' },
2020-06-06 13:52:33 -07:00
metaFieldLabel: { id: 'edit_profile.fields.meta_fields.label_placeholder', defaultMessage: 'Label' },
metaFieldContent: { id: 'edit_profile.fields.meta_fields.content_placeholder', defaultMessage: 'Content' },
verified: { id: 'edit_profile.fields.verified_display_name', defaultMessage: 'Verified users may not update their display name' },
2020-04-21 16:00:05 -07:00
});
const mapStateToProps = state => {
const me = state.get('me');
const soapbox = getSoapboxConfig(state);
const meta = state.getIn(['meta', 'pleroma']);
const account = state.getIn(['accounts', me]).set('pleroma', meta);
2020-04-21 16:00:05 -07:00
return {
account,
maxFields: state.getIn(['instance', 'pleroma', 'metadata', 'fields_limits', 'max_fields'], 4),
verifiedCanEditName: soapbox.get('verifiedCanEditName'),
supportsEmailList: getFeatures(state.get('instance')).emailList,
2020-04-21 16:00:05 -07:00
};
};
// Forces fields to be maxFields size, filling empty values
const normalizeFields = (fields, maxFields) => (
ImmutableList(fields).setSize(maxFields).map(field =>
field ? field : ImmutableMap({ name: '', value: '' }),
)
);
// HTML unescape for special chars, eg <br>
const unescapeParams = (map, params) => (
params.reduce((map, param) => (
map.set(param, unescape(map.get(param)))
), map)
);
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,
maxFields: PropTypes.number,
verifiedCanEditName: PropTypes.bool,
2020-04-21 16:00:05 -07:00
};
2020-04-21 17:22:00 -07:00
state = {
isLoading: false,
2020-04-21 16:00:05 -07:00
}
2020-07-04 17:32:07 -07:00
constructor(props) {
super(props);
const { account } = this.props;
const strangerNotifications = account.getIn(['pleroma', 'notification_settings', 'block_from_strangers']);
2021-06-14 20:07:48 -07:00
const acceptsEmailList = account.getIn(['pleroma', 'accepts_email_list']);
const initialState = account.withMutations(map => {
2020-07-04 17:32:07 -07:00
map.merge(map.get('source'));
map.delete('source');
map.set('fields', normalizeFields(map.get('fields'), props.maxFields));
map.set('stranger_notifications', strangerNotifications);
2021-06-14 20:07:48 -07:00
map.set('accepts_email_list', acceptsEmailList);
2021-06-16 12:20:57 -07:00
map.set('hide_network', hidesNetwork(account));
2020-08-09 13:13:36 -07:00
unescapeParams(map, ['display_name', 'bio']);
2020-07-04 17:32:07 -07:00
});
this.state = initialState.toObject();
}
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,
}));
}
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;
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,
2021-06-14 20:07:48 -07:00
accepts_email_list: state.accepts_email_list,
2021-06-16 12:20:57 -07:00
hide_followers: state.hide_network,
hide_follows: state.hide_network,
hide_followers_count: state.hide_network,
hide_follows_count: state.hide_network,
}, 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-08-09 13:13:36 -07:00
// Compact the submission. This should probably be done better.
const shouldAppend = Boolean(data[key] !== undefined || 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;
const credentials = dispatch(patchMe(this.getFormdata()));
const notifications = dispatch(updateNotificationSettings({
block_from_strangers: this.state.stranger_notifications || false,
}));
this.setState({ isLoading: true });
Promise.all([credentials, notifications]).then(() => {
2020-04-21 16:00:05 -07:00
this.setState({ isLoading: false });
dispatch(snackbar.success('Profile saved!'));
2020-04-21 16:00:05 -07:00
}).catch((error) => {
this.setState({ isLoading: false });
});
2020-04-21 16:00:05 -07:00
event.preventDefault();
}
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 });
}
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() {
const { intl, maxFields, account, verifiedCanEditName, supportsEmailList } = this.props;
2021-03-15 19:50:16 -07:00
const verified = isVerified(account);
const canEditName = verifiedCanEditName || !verified;
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
className={canEditName ? '' : 'disabled'}
2020-06-06 13:52:33 -07:00
label={<FormattedMessage id='edit_profile.fields.display_name_label' defaultMessage='Display name' />}
2020-04-21 16:00:05 -07:00
name='display_name'
2020-04-21 17:22:00 -07:00
value={this.state.display_name}
onChange={this.handleTextChange}
disabled={!canEditName}
hint={!canEditName && intl.formatMessage(messages.verified)}
2020-04-21 16:00:05 -07:00
/>
<SimpleTextarea
2020-06-06 13:52:33 -07:00
label={<FormattedMessage id='edit_profile.fields.bio_label' defaultMessage='Bio' />}
2020-04-21 16:00:05 -07:00
name='note'
autoComplete='off'
2020-08-09 13:13:36 -07:00
value={this.state.note}
wrap='hard'
2020-08-09 13:13:36 -07:00
onChange={this.handleTextChange}
rows={3}
2020-04-21 17:22:00 -07:00
/>
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
2020-06-06 13:52:33 -07:00
label={<FormattedMessage id='edit_profile.fields.header_label' defaultMessage='Header' />}
2020-04-22 14:26:44 -07:00
name='header'
2020-06-06 13:52:33 -07:00
hint={<FormattedMessage id='edit_profile.hints.header' defaultMessage='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
2020-06-06 13:52:33 -07:00
label={<FormattedMessage id='edit_profile.fields.avatar_label' defaultMessage='Avatar' />}
2020-04-22 14:26:44 -07:00
name='avatar'
2020-06-06 13:52:33 -07:00
hint={<FormattedMessage id='edit_profile.hints.avatar' defaultMessage='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
2020-06-06 13:52:33 -07:00
label={<FormattedMessage id='edit_profile.fields.locked_label' defaultMessage='Lock account' />}
hint={<FormattedMessage id='edit_profile.hints.locked' defaultMessage='Requires you to manually approve followers' />}
2020-04-21 17:22:00 -07:00
name='locked'
checked={this.state.locked}
onChange={this.handleCheckboxChange}
/>
2021-06-16 12:20:57 -07:00
<Checkbox
label={<FormattedMessage id='edit_profile.fields.hide_network_label' defaultMessage='Hide network' />}
hint={<FormattedMessage id='edit_profile.hints.hide_network' defaultMessage='Who you follow and who follows you will not be shown on your profile' />}
name='hide_network'
checked={this.state.hide_network}
onChange={this.handleCheckboxChange}
/>
2020-04-21 17:22:00 -07:00
<Checkbox
2020-06-06 13:52:33 -07:00
label={<FormattedMessage id='edit_profile.fields.bot_label' defaultMessage='This is a bot account' />}
hint={<FormattedMessage id='edit_profile.hints.bot' defaultMessage='This account mainly performs automated actions and might not be monitored' />}
2020-04-21 17:22:00 -07:00
name='bot'
checked={this.state.bot}
onChange={this.handleCheckboxChange}
2020-04-21 16:00:05 -07:00
/>
<Checkbox
label={<FormattedMessage id='edit_profile.fields.stranger_notifications_label' defaultMessage='Block notifications from strangers' />}
hint={<FormattedMessage id='edit_profile.hints.stranger_notifications' defaultMessage='Only show notifications from people you follow' />}
name='stranger_notifications'
checked={this.state.stranger_notifications}
onChange={this.handleCheckboxChange}
/>
{supportsEmailList && <Checkbox
2021-06-14 20:07:48 -07:00
label={<FormattedMessage id='edit_profile.fields.accepts_email_list_label' defaultMessage='Subscribe to newsletter' />}
hint={<FormattedMessage id='edit_profile.hints.accepts_email_list' defaultMessage='Opt-in to news and marketing updates.' />}
name='accepts_email_list'
checked={this.state.accepts_email_list}
onChange={this.handleCheckboxChange}
/>}
2020-04-21 16:00:05 -07:00
</FieldsGroup>
<FieldsGroup>
<div className='fields-row__column fields-group'>
<div className='input with_block_label'>
2020-06-06 13:52:33 -07:00
<label><FormattedMessage id='edit_profile.fields.meta_fields_label' defaultMessage='Profile metadata' /></label>
<span className='hint'>
<FormattedMessage id='edit_profile.hints.meta_fields' defaultMessage='You can have up to {count, plural, one {# item} other {# items}} displayed as a table on your profile' values={{ count: maxFields }} />
2020-06-06 13:52:33 -07:00
</span>
{
this.state.fields.map((field, i) => (
<div className='row' key={i}>
<TextInput
2020-06-06 13:52:33 -07:00
placeholder={intl.formatMessage(messages.metaFieldLabel)}
value={field.get('name')}
onChange={this.handleFieldChange(i, 'name')}
/>
<TextInput
2020-06-06 13:52:33 -07:00
placeholder={intl.formatMessage(messages.metaFieldContent)}
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'>
2020-06-06 13:52:33 -07:00
<button name='button' type='submit' className='btn button button-primary'>
<FormattedMessage id='edit_profile.save' defaultMessage='Save' />
</button>
2020-04-21 16:00:05 -07:00
</div>
</SimpleForm>
</Column>
);
}
}