You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/components/modals/device/Remember/index.js

174 lines
4.9 KiB

6 years ago
/* @flow */
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { H3 } from 'components/Heading';
import P from 'components/Paragraph';
import Loader from 'components/Loader';
import Button from 'components/Button';
import { FormattedMessage } from 'react-intl';
6 years ago
import type { TrezorDevice } from 'flowtype';
5 years ago
import l10nCommonMessages from 'views/common.messages';
import l10nDeviceMessages from '../common.messages';
import l10nMessages from './index.messages';
import type { Props as BaseProps } from '../../Container';
type Props = {
5 years ago
device: TrezorDevice,
instances: ?Array<TrezorDevice>,
onRememberDevice: $ElementType<$ElementType<BaseProps, 'modalActions'>, 'onRememberDevice'>,
onForgetDevice: $ElementType<$ElementType<BaseProps, 'modalActions'>, 'onForgetDevice'>,
};
6 years ago
type State = {
5 years ago
countdown: number,
ticker?: number,
};
6 years ago
const ButtonContent = styled.div`
display: flex;
justify-content: center;
align-items: center;
flex-direction: row;
`;
const StyledP = styled(P)`
padding: 20px 0;
`;
const Wrapper = styled.div`
width: 360px;
padding: 30px 48px;
`;
const Text = styled.div`
padding-right: 10px;
`;
const Column = styled.div`
display: flex;
flex-direction: column;
5 years ago
button + button {
margin-top: 10px;
}
`;
const StyledLoader = styled(Loader)`
position: absolute;
left: 200px;
`;
class RememberDevice extends PureComponent<Props, State> {
constructor(props: Props) {
6 years ago
super(props);
this.state = {
countdown: 10,
};
}
6 years ago
componentDidMount(): void {
const ticker = () => {
if (this.state.countdown - 1 <= 0) {
// TODO: possible race condition,
6 years ago
// device could be already connected but it didn't emit Device.CONNECT event yet
window.clearInterval(this.state.ticker);
this.props.onForgetDevice(this.props.device);
6 years ago
} else {
this.setState(previousState => ({
countdown: previousState.countdown - 1,
}));
6 years ago
}
};
6 years ago
this.setState({
countdown: 10,
ticker: window.setInterval(ticker, 1000),
6 years ago
});
this.keyboardHandler = this.keyboardHandler.bind(this);
window.addEventListener('keydown', this.keyboardHandler, false);
6 years ago
}
componentWillUnmount(): void {
window.removeEventListener('keydown', this.keyboardHandler, false);
6 years ago
if (this.state.ticker) {
window.clearInterval(this.state.ticker);
}
}
keyboardHandler(event: KeyboardEvent): void {
if (event.keyCode === 13) {
event.preventDefault();
this.forget();
}
}
keyboardHandler: (event: KeyboardEvent) => void;
forget() {
this.props.onForgetDevice(this.props.device);
}
render() {
const { device, instances, onRememberDevice } = this.props;
let { label } = device;
const deviceCount = instances ? instances.length : 0;
if (instances && instances.length > 0) {
5 years ago
label = instances.map(instance => instance.instanceLabel).join(',');
}
6 years ago
return (
<Wrapper>
<H3>
<FormattedMessage
{...l10nDeviceMessages.TR_FORGET_LABEL}
values={{
deviceLabel: label,
}}
/>
</H3>
<StyledP isSmaller>
<FormattedMessage
{...l10nMessages.TR_WOULD_YOU_LIKE_TREZOR_WALLET_TO}
values={{
deviceCount,
}}
/>
</StyledP>
<Column>
<Button onClick={() => this.forget()}>
<ButtonContent>
<Text>
5 years ago
<FormattedMessage {...l10nCommonMessages.TR_FORGET_DEVICE} />
</Text>
<StyledLoader
isSmallText
isWhiteText
size={28}
text={this.state.countdown.toString()}
/>
</ButtonContent>
</Button>
5 years ago
<Button isWhite onClick={() => onRememberDevice(device)}>
5 years ago
<FormattedMessage {...l10nMessages.TR_REMEMBER_DEVICE} />
</Button>
</Column>
</Wrapper>
6 years ago
);
}
}
RememberDevice.propTypes = {
device: PropTypes.object.isRequired,
instances: PropTypes.array.isRequired,
onRememberDevice: PropTypes.func.isRequired,
onForgetDevice: PropTypes.func.isRequired,
};
5 years ago
export default RememberDevice;