/* @flow */ import React, { Component } from 'react'; import styled from 'styled-components'; import { H3 } from 'components/Heading'; import P from 'components/Paragraph'; import Button from 'components/Button'; import Input from 'components/inputs/Input'; import { getDuplicateInstanceNumber } from 'reducers/utils'; import { FONT_SIZE } from 'config/variables'; import Icon from 'components/Icon'; import icons from 'config/icons'; import colors from 'config/colors'; import Link from 'components/Link'; import type { Props } from 'components/modals/index'; type State = { defaultName: string; instance: number; instanceName: ?string; isUsed: boolean; } const StyledLink = styled(Link)` position: absolute; right: 15px; top: 15px; `; const Wrapper = styled.div` width: 360px; padding: 24px 48px; `; const Column = styled.div` display: flex; padding: 10px 0; flex-direction: column; `; const StyledP = styled(P)` padding: 10px 0px; `; const StyledButton = styled(Button)` margin: 0 0 10px 0; `; const Label = styled.div` display: flex; text-align: left; font-size: ${FONT_SIZE.SMALLER}; flex-direction: column; padding-bottom: 5px; `; const ErrorMessage = styled.div` color: ${colors.ERROR_PRIMARY}; font-size: ${FONT_SIZE.SMALLER}; padding-top: 5px; text-align: center; width: 100%; `; export default class DuplicateDevice extends Component { state: State; input: ?HTMLInputElement; keyboardHandler: (event: KeyboardEvent) => void; constructor(props: Props) { super(props); const device = props.modal.opened ? props.modal.device : null; if (!device) return; const instance = getDuplicateInstanceNumber(props.devices, device); this.state = { defaultName: `${device.label} (${instance.toString()})`, instance, instanceName: null, isUsed: false, }; } componentDidMount(): void { // one time autofocus if (this.input) this.input.focus(); this.keyboardHandler = this.keyboardHandler.bind(this); window.addEventListener('keydown', this.keyboardHandler, false); } componentWillUnmount(): void { window.removeEventListener('keydown', this.keyboardHandler, false); } onNameChange = (value: string): void => { let isUsed: boolean = false; if (value.length > 0) { isUsed = (this.props.devices.find(d => d.instanceName === value) !== undefined); } this.setState({ instanceName: value.length > 0 ? value : null, isUsed, }); } keyboardHandler(event: KeyboardEvent): void { if (event.keyCode === 13 && !this.state.isUsed) { event.preventDefault(); this.submit(); } } submit() { 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 }); } render() { if (!this.props.modal.opened) return null; const { device } = this.props.modal; const { onCancel } = this.props.modalActions; const { defaultName, instanceName, isUsed, } = this.state; return (

Clone { device.label }?

This will create new instance of device which can be used with different passphrase { this.input = element; }} onChange={event => this.onNameChange(event.currentTarget.value)} value={instanceName} /> { isUsed && Instance name is already in use } this.submit()} >Create new instance Cancel
); } }