2018-07-03 16:31:10 +10:00
|
|
|
import { Button, FormGroup, ControlLabel, FormControl, HelpBlock, Image } from 'react-bootstrap';
|
|
|
|
import { withRouter } from 'react-router-dom'
|
2018-07-03 12:42:54 +10:00
|
|
|
import React, { Component } from 'react';
|
2018-07-03 16:31:10 +10:00
|
|
|
import FieldGroup from './FieldGroup';
|
2018-07-01 19:17:41 +10:00
|
|
|
|
2018-07-03 16:31:10 +10:00
|
|
|
class UpdateUser extends Component {
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
2018-07-01 19:17:41 +10:00
|
|
|
|
2018-07-03 16:31:10 +10:00
|
|
|
// initial state
|
|
|
|
this.state = {
|
|
|
|
isLoading: false,
|
|
|
|
picture: '',
|
|
|
|
description: this.props.user.description,
|
|
|
|
error: '',
|
2018-07-04 16:34:51 +10:00
|
|
|
formState: null,
|
|
|
|
formUpdated: false
|
2018-07-03 16:31:10 +10:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Events
|
|
|
|
*/
|
2018-07-04 16:34:51 +10:00
|
|
|
handleClick = async () => {
|
|
|
|
this.setState({ isLoading: true });
|
|
|
|
|
|
|
|
if(!this.state.formUpdated) return; // no form update => do nothing
|
|
|
|
|
|
|
|
let hash = '';
|
2018-07-03 16:31:10 +10:00
|
|
|
if (this.state.picture !== '') {
|
2018-07-04 16:34:51 +10:00
|
|
|
try{
|
|
|
|
hash = await EmbarkJS.Storage.uploadFile([this.inputPicture]);
|
|
|
|
}
|
|
|
|
catch(err){
|
|
|
|
return this.setState({ isLoading: false, formState: 'error', error: err.message });
|
|
|
|
}
|
|
|
|
}
|
2018-07-03 16:31:10 +10:00
|
|
|
|
2018-07-04 16:34:51 +10:00
|
|
|
const gasEstimate = await DTwitter.methods.editAccount(this.props.user.username, this.state.description, hash).estimateGas();
|
2018-07-03 16:31:10 +10:00
|
|
|
|
2018-07-04 16:34:51 +10:00
|
|
|
try{
|
|
|
|
const result = await DTwitter.methods.editAccount(this.props.user.username, this.state.description, hash).send({gas: gasEstimate});
|
|
|
|
if(result.status !== '0x1'){
|
|
|
|
return this.setState({ isLoading: false, formState: 'error', error: 'Error executing transaction, transaction details: ' + JSON.stringify(result) });
|
|
|
|
}
|
2018-07-03 16:31:10 +10:00
|
|
|
|
2018-07-04 16:34:51 +10:00
|
|
|
this.setState({ isLoading: false, formState: 'success' });
|
2018-07-03 16:31:10 +10:00
|
|
|
|
2018-07-04 16:34:51 +10:00
|
|
|
// tell parent we've got an updated user
|
|
|
|
return this.props.onAction();
|
2018-07-03 16:31:10 +10:00
|
|
|
}
|
2018-07-04 16:34:51 +10:00
|
|
|
catch(err){
|
|
|
|
this.setState({ isLoading: false, formState: 'error', error: err.message });
|
|
|
|
}
|
2018-07-03 16:31:10 +10:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChange(e) {
|
2018-07-04 16:34:51 +10:00
|
|
|
let state = {formUpdated: true};
|
2018-07-03 16:31:10 +10:00
|
|
|
const input = e.target.name;
|
|
|
|
const value = e.target.value;
|
|
|
|
|
|
|
|
state[input] = value;
|
|
|
|
|
|
|
|
this.setState(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* React methods
|
|
|
|
*/
|
|
|
|
render() {
|
2018-07-04 16:34:51 +10:00
|
|
|
const { isLoading, error, formState, description, picture } = this.state;
|
|
|
|
const { user } = this.props;
|
|
|
|
const feedback = formState === 'success' ? 'Saved' : error;
|
2018-07-03 16:31:10 +10:00
|
|
|
return (
|
|
|
|
<form>
|
2018-07-04 16:34:51 +10:00
|
|
|
<FieldGroup
|
|
|
|
type="text"
|
|
|
|
value={ user.username }
|
|
|
|
disabled={true}
|
|
|
|
name="username"
|
|
|
|
label="Username"
|
|
|
|
/>
|
|
|
|
<FieldGroup
|
|
|
|
type="text"
|
|
|
|
value={ description }
|
|
|
|
placeholder="description"
|
|
|
|
onChange={ (e) => this.handleChange(e) }
|
|
|
|
name="description"
|
|
|
|
label="Description"
|
|
|
|
validationState={ formState }
|
|
|
|
/>
|
|
|
|
<FieldGroup
|
|
|
|
type="file"
|
|
|
|
value={ picture }
|
|
|
|
onChange={ (e) => this.handleChange(e) }
|
|
|
|
name="picture"
|
|
|
|
label="Profile picture"
|
|
|
|
inputRef={ (input) => this.inputPicture = input }
|
|
|
|
validationState={ formState }
|
|
|
|
/>
|
|
|
|
{ user.picture.length ? <Image src={ user.picture } width="100" circle /> : '' }
|
|
|
|
<Button
|
|
|
|
bsStyle="primary"
|
|
|
|
disabled={ isLoading }
|
|
|
|
onClick={ isLoading ? null : (e) => this.handleClick(e) }
|
|
|
|
>
|
|
|
|
{ isLoading ? 'Loading...' : 'Update profile' }
|
|
|
|
</Button>
|
2018-07-03 16:31:10 +10:00
|
|
|
<FormGroup
|
2018-07-04 16:34:51 +10:00
|
|
|
validationState={ formState }
|
2018-07-03 16:31:10 +10:00
|
|
|
>
|
2018-07-04 16:34:51 +10:00
|
|
|
<HelpBlock>{ feedback }</HelpBlock>
|
2018-07-03 16:31:10 +10:00
|
|
|
</FormGroup>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
2018-07-01 19:17:41 +10:00
|
|
|
}
|
|
|
|
|
2018-07-03 16:31:10 +10:00
|
|
|
export default withRouter(UpdateUser);
|