2018-05-21 23:10:51 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { connect, MapStateToProps } from 'react-redux';
|
|
|
|
import { CopyToClipboard } from 'react-copy-to-clipboard';
|
2018-06-18 01:53:00 +00:00
|
|
|
|
2018-05-21 23:10:51 +00:00
|
|
|
import translate, { translateRaw } from 'translations';
|
2018-06-18 01:53:00 +00:00
|
|
|
import { AppState } from 'features/reducers';
|
2018-05-21 23:10:51 +00:00
|
|
|
import {
|
2018-06-18 01:53:00 +00:00
|
|
|
addressBookConstants,
|
|
|
|
addressBookActions,
|
|
|
|
addressBookSelectors
|
|
|
|
} from 'features/addressBook';
|
2018-05-21 23:10:51 +00:00
|
|
|
import { Address, Identicon, Input } from 'components/ui';
|
|
|
|
|
|
|
|
interface StateProps {
|
2018-06-18 01:53:00 +00:00
|
|
|
entry: ReturnType<typeof addressBookSelectors.getAccountAddressEntry>;
|
2018-06-15 23:28:42 +00:00
|
|
|
addressLabel: string;
|
2018-05-21 23:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface DispatchProps {
|
2018-06-18 01:53:00 +00:00
|
|
|
changeAddressLabelEntry: addressBookActions.TChangeAddressLabelEntry;
|
|
|
|
saveAddressLabelEntry: addressBookActions.TSaveAddressLabelEntry;
|
|
|
|
removeAddressLabelEntry: addressBookActions.TRemoveAddressLabelEntry;
|
2018-05-21 23:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface OwnProps {
|
|
|
|
address: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Props = StateProps & DispatchProps & OwnProps;
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
copied: boolean;
|
|
|
|
editingLabel: boolean;
|
|
|
|
labelInputTouched: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AccountAddress extends React.Component<Props, State> {
|
|
|
|
public state = {
|
|
|
|
copied: false,
|
|
|
|
editingLabel: false,
|
|
|
|
labelInputTouched: false
|
|
|
|
};
|
|
|
|
|
|
|
|
private goingToClearCopied: number | null = null;
|
|
|
|
|
|
|
|
private labelInput: HTMLInputElement | null = null;
|
|
|
|
|
|
|
|
public handleCopy = () =>
|
|
|
|
this.setState(
|
|
|
|
(prevState: State) => ({
|
|
|
|
copied: !prevState.copied
|
|
|
|
}),
|
|
|
|
this.clearCopied
|
|
|
|
);
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
|
|
|
if (this.goingToClearCopied) {
|
|
|
|
window.clearTimeout(this.goingToClearCopied);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
2018-06-15 23:28:42 +00:00
|
|
|
const { address, addressLabel } = this.props;
|
2018-05-21 23:10:51 +00:00
|
|
|
const { copied } = this.state;
|
|
|
|
const labelContent = this.generateLabelContent();
|
|
|
|
const labelButton = this.generateLabelButton();
|
|
|
|
const addressClassName = `AccountInfo-address-addr ${
|
2018-06-15 23:28:42 +00:00
|
|
|
addressLabel ? 'AccountInfo-address-addr--small' : ''
|
2018-05-21 23:10:51 +00:00
|
|
|
}`;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="AccountInfo">
|
|
|
|
<h5 className="AccountInfo-section-header">{translate('SIDEBAR_ACCOUNTADDR')}</h5>
|
|
|
|
<div className="AccountInfo-section AccountInfo-address-section">
|
|
|
|
<div className="AccountInfo-address-icon">
|
|
|
|
<Identicon address={address} size="100%" />
|
|
|
|
</div>
|
|
|
|
<div className="AccountInfo-address-wrapper">
|
|
|
|
{labelContent}
|
|
|
|
<div className={addressClassName}>
|
|
|
|
<Address address={address} />
|
|
|
|
</div>
|
|
|
|
<CopyToClipboard onCopy={this.handleCopy} text={address}>
|
|
|
|
<div
|
|
|
|
className={`AccountInfo-copy ${copied ? 'is-copied' : ''}`}
|
2018-06-18 01:53:00 +00:00
|
|
|
title={translateRaw('COPY_TO_CLIPBOARD')}
|
2018-05-21 23:10:51 +00:00
|
|
|
>
|
|
|
|
<i className="fa fa-copy" />
|
2018-06-18 01:53:00 +00:00
|
|
|
<span>{translateRaw(copied ? 'COPIED' : 'COPY_ADDRESS')}</span>
|
2018-05-21 23:10:51 +00:00
|
|
|
</div>
|
|
|
|
</CopyToClipboard>
|
2018-06-18 01:53:00 +00:00
|
|
|
<div className="AccountInfo-label" title={translateRaw('EDIT_LABEL_2')}>
|
2018-05-21 23:10:51 +00:00
|
|
|
{labelButton}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private clearCopied = () =>
|
|
|
|
(this.goingToClearCopied = window.setTimeout(() => this.setState({ copied: false }), 2000));
|
|
|
|
|
|
|
|
private startEditingLabel = () =>
|
|
|
|
this.setState({ editingLabel: true }, () => {
|
|
|
|
if (this.labelInput) {
|
|
|
|
this.labelInput.focus();
|
|
|
|
this.labelInput.select();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
private stopEditingLabel = () => this.setState({ editingLabel: false });
|
|
|
|
|
|
|
|
private setLabelInputRef = (node: HTMLInputElement) => (this.labelInput = node);
|
|
|
|
|
|
|
|
private generateLabelContent = () => {
|
2018-06-15 23:28:42 +00:00
|
|
|
const { addressLabel, entry: { temporaryLabel, labelError } } = this.props;
|
2018-05-21 23:10:51 +00:00
|
|
|
const { editingLabel, labelInputTouched } = this.state;
|
2018-06-15 23:28:42 +00:00
|
|
|
const newLabelSameAsPrevious = temporaryLabel === addressLabel;
|
2018-05-21 23:10:51 +00:00
|
|
|
const labelInputTouchedWithError = labelInputTouched && !newLabelSameAsPrevious && labelError;
|
|
|
|
|
|
|
|
let labelContent = null;
|
|
|
|
|
|
|
|
if (editingLabel) {
|
|
|
|
labelContent = (
|
|
|
|
<React.Fragment>
|
|
|
|
<Input
|
|
|
|
title={translateRaw('ADD_LABEL')}
|
|
|
|
placeholder={translateRaw('NEW_LABEL')}
|
2018-06-15 23:28:42 +00:00
|
|
|
defaultValue={addressLabel}
|
2018-05-21 23:10:51 +00:00
|
|
|
onChange={this.handleLabelChange}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
onFocus={this.setTemporaryLabelTouched}
|
|
|
|
onBlur={this.handleBlur}
|
|
|
|
showInvalidBeforeBlur={true}
|
|
|
|
setInnerRef={this.setLabelInputRef}
|
|
|
|
isValid={!labelInputTouchedWithError}
|
|
|
|
/>
|
|
|
|
{labelInputTouchedWithError && (
|
|
|
|
<label className="AccountInfo-address-wrapper-error">{labelError}</label>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
} else {
|
2018-06-15 23:28:42 +00:00
|
|
|
labelContent = <label className="AccountInfo-address-label">{addressLabel}</label>;
|
2018-05-21 23:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return labelContent;
|
|
|
|
};
|
|
|
|
|
|
|
|
private generateLabelButton = () => {
|
2018-06-15 23:28:42 +00:00
|
|
|
const { addressLabel } = this.props;
|
2018-05-21 23:10:51 +00:00
|
|
|
const { editingLabel } = this.state;
|
|
|
|
const labelButton = editingLabel ? (
|
|
|
|
<React.Fragment>
|
|
|
|
<i className="fa fa-save" />
|
|
|
|
<span role="button" title={translateRaw('SAVE_LABEL')} onClick={this.stopEditingLabel}>
|
|
|
|
{translate('SAVE_LABEL')}
|
|
|
|
</span>
|
|
|
|
</React.Fragment>
|
|
|
|
) : (
|
|
|
|
<React.Fragment>
|
|
|
|
<i className="fa fa-pencil" />
|
|
|
|
<span
|
|
|
|
role="button"
|
2018-06-15 23:28:42 +00:00
|
|
|
title={addressLabel ? translateRaw('EDIT_LABEL') : translateRaw('ADD_LABEL_9')}
|
2018-05-21 23:10:51 +00:00
|
|
|
onClick={this.startEditingLabel}
|
|
|
|
>
|
2018-06-15 23:28:42 +00:00
|
|
|
{addressLabel ? translate('EDIT_LABEL') : translate('ADD_LABEL_9')}
|
2018-05-21 23:10:51 +00:00
|
|
|
</span>
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
|
|
|
|
return labelButton;
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleBlur = () => {
|
2018-06-15 23:28:42 +00:00
|
|
|
const { address, addressLabel, entry: { id, label, temporaryLabel, labelError } } = this.props;
|
2018-05-21 23:10:51 +00:00
|
|
|
|
|
|
|
this.clearTemporaryLabelTouched();
|
|
|
|
this.stopEditingLabel();
|
|
|
|
|
2018-06-15 23:28:42 +00:00
|
|
|
if (temporaryLabel === addressLabel) {
|
2018-05-21 23:10:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temporaryLabel && temporaryLabel.length > 0) {
|
|
|
|
this.props.saveAddressLabelEntry(id);
|
|
|
|
|
|
|
|
if (labelError) {
|
|
|
|
// If the new changes aren't valid, undo them.
|
|
|
|
this.props.changeAddressLabelEntry({
|
|
|
|
id,
|
|
|
|
address,
|
|
|
|
temporaryAddress: address,
|
|
|
|
label,
|
|
|
|
temporaryLabel: label,
|
|
|
|
overrideValidation: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.props.removeAddressLabelEntry(id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
|
|
switch (e.key) {
|
|
|
|
case 'Enter':
|
|
|
|
return this.handleBlur();
|
|
|
|
case 'Escape':
|
|
|
|
return this.stopEditingLabel();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleLabelChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
const { address } = this.props;
|
|
|
|
const label = e.target.value;
|
|
|
|
|
|
|
|
this.props.changeAddressLabelEntry({
|
2018-06-18 01:53:00 +00:00
|
|
|
id: addressBookConstants.ACCOUNT_ADDRESS_ID,
|
2018-05-21 23:10:51 +00:00
|
|
|
address,
|
|
|
|
label,
|
|
|
|
isEditing: true
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
labelInputTouched: true
|
|
|
|
},
|
|
|
|
() => label.length === 0 && this.clearTemporaryLabelTouched()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
private setTemporaryLabelTouched = () => {
|
|
|
|
const { labelInputTouched } = this.state;
|
|
|
|
|
|
|
|
if (!labelInputTouched) {
|
|
|
|
this.setState({ labelInputTouched: true });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private clearTemporaryLabelTouched = () => this.setState({ labelInputTouched: false });
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:28:42 +00:00
|
|
|
const mapStateToProps: MapStateToProps<StateProps, {}, AppState> = (
|
|
|
|
state: AppState,
|
|
|
|
ownProps: OwnProps
|
|
|
|
) => {
|
2018-06-18 01:53:00 +00:00
|
|
|
const labelEntry = addressBookSelectors.getAddressLabelEntryFromAddress(state, ownProps.address);
|
2018-06-15 23:28:42 +00:00
|
|
|
return {
|
2018-06-18 01:53:00 +00:00
|
|
|
entry: addressBookSelectors.getAccountAddressEntry(state),
|
2018-06-15 23:28:42 +00:00
|
|
|
addressLabel: labelEntry ? labelEntry.label : ''
|
|
|
|
};
|
|
|
|
};
|
2018-05-21 23:10:51 +00:00
|
|
|
|
|
|
|
const mapDispatchToProps: DispatchProps = {
|
2018-06-18 01:53:00 +00:00
|
|
|
changeAddressLabelEntry: addressBookActions.changeAddressLabelEntry,
|
|
|
|
saveAddressLabelEntry: addressBookActions.saveAddressLabelEntry,
|
|
|
|
removeAddressLabelEntry: addressBookActions.removeAddressLabelEntry
|
2018-05-21 23:10:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default connect<StateProps, DispatchProps, OwnProps, AppState>(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
|
|
|
)(AccountAddress);
|