1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-13 20:08:56 +00:00
trezor-wallet/src/components/modal/DuplicateDevice.js

111 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-02-20 09:30:36 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
2018-02-20 09:30:36 +00:00
2018-05-15 14:52:57 +00:00
import React, { Component } from 'react';
2018-08-20 11:01:43 +00:00
import { getDuplicateInstanceNumber } from 'reducers/utils';
2018-05-05 11:52:03 +00:00
import type { Props } from './index';
2018-02-20 09:30:36 +00:00
2018-05-15 14:52:57 +00:00
type State = {
defaultName: string;
instance: number;
2018-05-15 14:52:57 +00:00
instanceName: ?string;
2018-05-15 17:47:24 +00:00
isUsed: boolean;
2018-02-20 09:30:36 +00:00
}
2018-05-15 14:52:57 +00:00
export default class DuplicateDevice extends Component<Props, State> {
2018-05-15 17:03:27 +00:00
keyboardHandler: (event: KeyboardEvent) => void;
2018-07-30 10:52:13 +00:00
2018-05-15 14:52:57 +00:00
state: State;
2018-07-30 10:52:13 +00:00
2018-05-15 17:47:24 +00:00
input: ?HTMLInputElement;
2018-05-15 14:52:57 +00:00
constructor(props: Props) {
super(props);
const device = props.modal.opened ? props.modal.device : null;
if (!device) return;
const instance = getDuplicateInstanceNumber(props.devices, device);
2018-05-15 14:52:57 +00:00
this.state = {
defaultName: `${device.label} (${instance.toString()})`,
instance,
2018-05-15 17:47:24 +00:00
instanceName: null,
isUsed: false,
2018-07-30 10:52:13 +00:00
};
2018-05-15 14:52:57 +00:00
}
2018-05-15 17:03:27 +00:00
keyboardHandler(event: KeyboardEvent): void {
2018-05-15 17:47:24 +00:00
if (event.keyCode === 13 && !this.state.isUsed) {
2018-05-15 17:03:27 +00:00
event.preventDefault();
this.submit();
}
}
componentDidMount(): void {
2018-05-15 17:47:24 +00:00
// one time autofocus
2018-07-30 10:52:13 +00:00
if (this.input) this.input.focus();
2018-05-15 17:03:27 +00:00
this.keyboardHandler = this.keyboardHandler.bind(this);
window.addEventListener('keydown', this.keyboardHandler, false);
}
componentWillUnmount(): void {
window.removeEventListener('keydown', this.keyboardHandler, false);
}
2018-05-15 14:52:57 +00:00
onNameChange = (value: string): void => {
2018-05-15 17:47:24 +00:00
let isUsed: boolean = false;
if (value.length > 0) {
2018-07-30 10:52:13 +00:00
isUsed = (this.props.devices.find(d => d.instanceName === value) !== undefined);
2018-05-15 17:47:24 +00:00
}
2018-07-30 10:52:13 +00:00
2018-05-15 14:52:57 +00:00
this.setState({
2018-05-15 17:47:24 +00:00
instanceName: value.length > 0 ? value : null,
2018-07-30 10:52:13 +00:00
isUsed,
2018-05-15 14:52:57 +00:00
});
}
2018-05-15 17:03:27 +00:00
submit() {
2018-05-15 17:47:24 +00:00
if (!this.props.modal.opened) return;
const extended: Object = { instanceName: this.state.instanceName, instance: this.state.instance };
this.props.modalActions.onDuplicateDevice({ ...this.props.modal.device, ...extended });
2018-05-15 17:03:27 +00:00
}
2018-05-15 14:52:57 +00:00
render() {
if (!this.props.modal.opened) return null;
const { device } = this.props.modal;
const { onCancel, onDuplicateDevice } = this.props.modalActions;
2018-05-15 17:47:24 +00:00
const {
defaultName,
instanceName,
2018-07-30 10:52:13 +00:00
isUsed,
2018-05-15 17:47:24 +00:00
} = this.state;
2018-05-15 14:52:57 +00:00
return (
<div className="duplicate">
2018-07-30 10:52:13 +00:00
<button className="close-modal transparent" onClick={onCancel} />
2018-05-15 14:52:57 +00:00
<h3>Clone { device.label }?</h3>
<p>This will create new instance of device which can be used with different passphrase</p>
2018-05-15 17:47:24 +00:00
<div className="row">
<label>Instance name</label>
2018-07-30 10:52:13 +00:00
<input
type="text"
2018-05-15 17:47:24 +00:00
autoComplete="off"
autoCorrect="off"
autoCapitalize="off"
spellCheck="false"
2018-07-30 10:52:13 +00:00
className={isUsed ? 'not-valid' : null}
placeholder={defaultName}
ref={(element) => { this.input = element; }}
onChange={event => this.onNameChange(event.currentTarget.value)}
defaultValue={instanceName}
/>
2018-05-15 17:47:24 +00:00
{ isUsed ? <span className="error">Instance name is already in use</span> : null }
</div>
2018-07-30 10:52:13 +00:00
<button disabled={isUsed} onClick={event => this.submit()}>Create new instance</button>
<button className="white" onClick={onCancel}>Cancel</button>
2018-05-15 14:52:57 +00:00
</div>
);
}
}