Improve profile editor

This commit is contained in:
Alex Gleason 2020-04-21 19:22:00 -05:00
parent f2b1305ce9
commit f16779219a
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 57 additions and 20 deletions

View file

@ -9,6 +9,7 @@ import {
SimpleForm, SimpleForm,
FieldsGroup, FieldsGroup,
TextInput, TextInput,
Checkbox,
} from 'gabsocial/features/forms'; } from 'gabsocial/features/forms';
import { patchMe } from 'gabsocial/actions/me'; import { patchMe } from 'gabsocial/actions/me';
@ -33,21 +34,27 @@ class EditProfile extends ImmutablePureComponent {
account: ImmutablePropTypes.map, account: ImmutablePropTypes.map,
}; };
constructor(props) { state = {
super(props); isLoading: false,
this.state = { isLoading: false };
} }
getFormData = (form) => { getParams = () => {
return Object.fromEntries( const { state } = this;
new FormData(form).entries() return {
); discoverable: state.discoverable,
bot: state.bot,
display_name: state.display_name,
note: state.note,
// avatar: state.avatar,
// header: state.header,
locked: state.locked,
fields_attributes: state.fields_attributes,
};
} }
handleSubmit = (event) => { handleSubmit = (event) => {
const { dispatch } = this.props; const { dispatch } = this.props;
const formData = this.getFormData(event.target); dispatch(patchMe(this.getParams())).then(() => {
dispatch(patchMe(formData)).then(() => {
this.setState({ isLoading: false }); this.setState({ isLoading: false });
}).catch((error) => { }).catch((error) => {
this.setState({ isLoading: false }); this.setState({ isLoading: false });
@ -56,8 +63,21 @@ class EditProfile extends ImmutablePureComponent {
event.preventDefault(); event.preventDefault();
} }
componentWillMount() {
const { account } = this.props;
this.setState(account.toJS());
}
handleCheckboxChange = e => {
this.setState({ [e.target.name]: e.target.checked });
}
handleTextChange = e => {
this.setState({ [e.target.name]: e.target.value });
}
render() { render() {
const { account, intl } = this.props; const { intl } = this.props;
return ( return (
<Column icon='users' heading={intl.formatMessage(messages.heading)} backBtnSlim> <Column icon='users' heading={intl.formatMessage(messages.heading)} backBtnSlim>
@ -67,12 +87,29 @@ class EditProfile extends ImmutablePureComponent {
<TextInput <TextInput
label='Display name' label='Display name'
name='display_name' name='display_name'
defaultValue={account.get('display_name')} value={this.state.display_name}
maxLength={30}
onChange={this.handleTextChange}
/> />
<TextInput <TextInput
label='Bio' label='Bio'
name='note' name='note'
defaultValue={account.get('bio')} value={this.state.note}
onChange={this.handleTextChange}
/>
<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}
/> />
</FieldsGroup> </FieldsGroup>
</fieldset> </fieldset>

View file

@ -48,6 +48,7 @@ export class Checkbox extends ImmutablePureComponent {
static propTypes = { static propTypes = {
label: PropTypes.string, label: PropTypes.string,
name: PropTypes.string,
checked: PropTypes.bool, checked: PropTypes.bool,
onChange: PropTypes.func, onChange: PropTypes.func,
} }
@ -57,7 +58,7 @@ export class Checkbox extends ImmutablePureComponent {
} }
render() { render() {
const { label, checked, onChange } = this.props; const { label, name, checked, onChange } = this.props;
const id = uuidv4(); const id = uuidv4();
return ( return (
@ -70,6 +71,7 @@ export class Checkbox extends ImmutablePureComponent {
<label className='checkbox'> <label className='checkbox'>
<input <input
id={id} id={id}
name={name}
className='boolean optional' className='boolean optional'
type='checkbox' type='checkbox'
checked={checked} checked={checked}
@ -189,27 +191,25 @@ export class TextInput extends ImmutablePureComponent {
static propTypes = { static propTypes = {
label: PropTypes.string, label: PropTypes.string,
name: PropTypes.string,
defaultValue: PropTypes.string,
maxLength: PropTypes.number, maxLength: PropTypes.number,
} }
render() { render() {
const { label, name, defaultValue, maxLength } = this.props; const { label, maxLength, ...props } = this.props;
const id = uuidv4();
return ( return (
<div className='input with_label string optional'> <div className='input with_label string optional'>
<div className='label_input'> <div className='label_input'>
<label className='string optional' htmlFor={name}>{label}</label> <label className='string optional' htmlFor={id}>{label}</label>
<div className='label_input__wrapper'> <div className='label_input__wrapper'>
<input <input
maxlength={maxLength} maxlength={maxLength}
className='string optional' className='string optional'
size={maxLength} size={maxLength}
type='text' type='text'
defaultValue={defaultValue} id={id}
name={name} {...props}
id={name}
/> />
</div> </div>
</div> </div>