Connor Bryan 04f75a6a27 Address Manager (#1657)
* Add a new route for AddressBook

* Further templating of the AddressBook view

* Add initial functionality to handle a table of existing address labels

* Make the linter happy

* Adjust paths

* Factor out TableRow and add common functionality

* Add initial Redux boilerplate for addressBook | fix minor linting issues

* Swap out terminology and types

* Connect up to Redux

* Connect data for AddressBookTable to Redux

* Use temporary fields for addition

* Remove alignment and index column

* Stopping point

* Adjust the sizing of rows to be consistent

* Initial implementation of a dropdown for the address field

* Minor styling to dropdown

* Stopping point

* Apply a focus concept onto the factory

* Add keyboard controls for the address field dropdown

* Adjust label of address field when it matches an addressBook entry

* Properly handle attempting to blur a non-existent component

* Minor styling changes on dropdown box

* Standardize address casing, add accessibility to dropdown

* Create an addressLabel component

* Pass refs correctly and fix some typings

* Exact version

* Add module name mapping for shared/keycodes

* addressBook reducer tests

* Add functionality to DeterministicModal

* Minor changes / Add test for addressBook selectors

* Move out AddressBookTable to a component

* Typing, translation and restructuring

* More typing and translation fixes

* More linting fixes

* More type changes

* Variable name for dropdown background

* Fix TS type errors, lint errors, remove unused props

* Used a different selector and removed method: AddressBookTable

* Linter was mad

* Linter mad again :(

* Add a translation and adjust styling of AddressBookTable

* Move the onBlur to a class method

* Prevent the default behavior of up/down/enter for dropdown

* Let's do it this way instead

* Adjust the styling on DeterministicWalletModal labels

* Change `AddressBookTable` into a pseudo-table using section and div

* Use readable keys vs. keycodes

* Put the dropdown in InputFactory and position it correctly

* Sanitation of label adding and changing

* Prevent duplicate labels in AddressBook and Row

* Add a box shadow and use `invalid` class insted of custom

* Use emphasis vs strong for address in dropdown

* Display the label undernearth the input vs. changing it

* Isolate AccountAddress into its own component

* Introduce interactivity to AccountAddress

* Fully incorporate with Redux

* Validation for AccountAddress

* Add validation notifications for address field on AddressBookTable

* Minor formatting

* Adjust wrappage for optimal flexboxxing

* Make AddressBookTable responsive

* Show an invalid input in "real time" instead of only on submit

* Real time input validation for AddressBookTableRow

* Responsive-ize the To address dropdown

* Hide identicons instead at small enough screen sizes

* Fix repsonsiveness of dropdown further

* Fix responsiveness of table rows and inputs

* Truncate account info and switch identicons to the right for consistency

* Use classnames instead of targetting element directly for DWM

* Display a notice if the entered query doesnt match a label and isnt an addr

* Don't show an error on the To address if its a label entry

* Display an error under AddressBookTableRow in real time

* Display errors in real time for AddressBookTable temp inputs

* Add realtime validation to AccountAddress

* Ensure toChecksumAddress is used when entering labels to address manager

* Show errors even after blurring.

* Only show errors on address/label entry if they have been blurred

* On certain inputs, show an invalid input immediately

* Add displayed errors for labels with 0x and labels containing ens

* Move ENS checking validation out

* Add a saga for addLabelForAddress

* Completely revamp the redux side of Address Manager and test it all

* Adjust components to use new redux addressBook

* Incorporate new redux into AddressBookTableRow and clean up for linter

* Make linter and tests happy

* Another reduxy overhaul

* Still fixing it

* More redux updates

* Finalize redux stuff.

* Incorporate new reduxy way into AddressBookTable & Row

* Incorporate redux changes into Account Address

* Small tests fix

* Add and fix some selector tests

* Addressing Will's comments

* Shortened visibility class for line length reasons.
2018-05-21 18:10:51 -05:00

273 lines
7.8 KiB
TypeScript

import React from 'react';
import { connect, MapStateToProps } from 'react-redux';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import translate, { translateRaw } from 'translations';
import { AppState } from 'reducers';
import {
changeAddressLabelEntry,
TChangeAddressLabelEntry,
saveAddressLabelEntry,
TSaveAddressLabelEntry,
removeAddressLabelEntry,
TRemoveAddressLabelEntry
} from 'actions/addressBook';
import { getAccountAddressEntry, getAddressLabels } from 'selectors/addressBook';
import { Address, Identicon, Input } from 'components/ui';
interface StateProps {
entry: ReturnType<typeof getAccountAddressEntry>;
addressLabels: ReturnType<typeof getAddressLabels>;
}
interface DispatchProps {
changeAddressLabelEntry: TChangeAddressLabelEntry;
saveAddressLabelEntry: TSaveAddressLabelEntry;
removeAddressLabelEntry: TRemoveAddressLabelEntry;
}
interface OwnProps {
address: string;
}
type Props = StateProps & DispatchProps & OwnProps;
interface State {
copied: boolean;
editingLabel: boolean;
labelInputTouched: boolean;
}
export const ACCOUNT_ADDRESS_ID: string = 'ACCOUNT_ADDRESS_ID';
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() {
const { address, addressLabels } = this.props;
const { copied } = this.state;
const label = addressLabels[address];
const labelContent = this.generateLabelContent();
const labelButton = this.generateLabelButton();
const addressClassName = `AccountInfo-address-addr ${
label ? 'AccountInfo-address-addr--small' : ''
}`;
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' : ''}`}
title="Copy To clipboard"
>
<i className="fa fa-copy" />
<span>{copied ? 'copied!' : 'copy address'}</span>
</div>
</CopyToClipboard>
<div className="AccountInfo-label" title="Edit label">
{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 = () => {
const { address, addressLabels, entry: { temporaryLabel, labelError } } = this.props;
const { editingLabel, labelInputTouched } = this.state;
const storedLabel = addressLabels[address];
const newLabelSameAsPrevious = temporaryLabel === storedLabel;
const labelInputTouchedWithError = labelInputTouched && !newLabelSameAsPrevious && labelError;
let labelContent = null;
if (editingLabel) {
labelContent = (
<React.Fragment>
<Input
title={translateRaw('ADD_LABEL')}
placeholder={translateRaw('NEW_LABEL')}
defaultValue={storedLabel}
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 {
labelContent = (
<label title={storedLabel} className="AccountInfo-address-label">
{storedLabel}
</label>
);
}
return labelContent;
};
private generateLabelButton = () => {
const { address, addressLabels } = this.props;
const { editingLabel } = this.state;
const label = addressLabels[address];
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"
title={label ? translateRaw('EDIT_LABEL') : translateRaw('ADD_LABEL_9')}
onClick={this.startEditingLabel}
>
{label ? translate('EDIT_LABEL') : translate('ADD_LABEL_9')}
</span>
</React.Fragment>
);
return labelButton;
};
private handleBlur = () => {
const { address, addressLabels, entry: { id, label, temporaryLabel, labelError } } = this.props;
const storedLabel = addressLabels[address];
this.clearTemporaryLabelTouched();
this.stopEditingLabel();
if (temporaryLabel === storedLabel) {
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({
id: ACCOUNT_ADDRESS_ID,
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 });
}
const mapStateToProps: MapStateToProps<StateProps, {}, AppState> = (state: AppState) => ({
entry: getAccountAddressEntry(state),
addressLabels: getAddressLabels(state)
});
const mapDispatchToProps: DispatchProps = {
changeAddressLabelEntry,
saveAddressLabelEntry,
removeAddressLabelEntry
};
export default connect<StateProps, DispatchProps, OwnProps, AppState>(
mapStateToProps,
mapDispatchToProps
)(AccountAddress);