// Copyright 2017-2018 Jaco Greeff // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. // @flow import type { KeyringPair } from '@polkadot/util-keyring/types'; import type { I18nProps } from '@polkadot/ui-app/types'; import React from 'react'; import Button from '@polkadot/ui-app/Button'; import Input from '@polkadot/ui-app/Input'; import InputAddress from '@polkadot/ui-app/InputAddress'; import classes from '@polkadot/ui-app/util/classes'; import keyring from '@polkadot/ui-keyring'; import Address from './Address'; import translate from './translate'; type Props = I18nProps & { onBack: () => void }; type State = { currentPair: KeyringPair | null, defaultPublicKey?: Uint8Array, editedName: string, isEdited: boolean } class Editor extends React.PureComponent { state: State; constructor (props: Props) { super(props); const pairs = keyring.getPairs(); const currentPair = pairs[pairs.length - 1] || null; this.state = this.createState(currentPair); this.state.defaultPublicKey = currentPair ? currentPair.publicKey() : void 0; } render (): React$Node { const { className, style, t } = this.props; const { defaultPublicKey, currentPair, editedName, isEdited } = this.state; if (!currentPair) { return (
{t('editor.none', { defaultValue: 'There are no saved accounts. Add some first.' })}
); } const address = currentPair.address(); return (
); } createState (currentPair: KeyringPair | null): $Shape { return { currentPair, editedName: currentPair ? currentPair.getMeta().name || '' : '', isEdited: false }; } nextState (newState?: $Shape = {}): void { this.setState( (prevState: State): $Shape => { let { currentPair = prevState.currentPair, editedName = prevState.editedName } = newState; const previous = prevState.currentPair || { address: () => null }; let isEdited = false; if (currentPair) { if (currentPair.address() !== previous.address()) { editedName = currentPair.getMeta().name || ''; } else if (editedName !== currentPair.getMeta().name) { isEdited = true; } } else { editedName = ''; } return { currentPair, editedName, isEdited }; } ); } onChangeAccount = (publicKey: Uint8Array): void => { const currentPair = keyring.getPair(publicKey); this.nextState({ currentPair }); } onChangeName = (editedName: string): void => { this.nextState({ editedName }); } onCommit = (): void => { const { currentPair, editedName } = this.state; if (!currentPair) { return; } keyring.saveAccountMeta(currentPair, { name: editedName, whenEdited: Date.now() }); this.nextState({}); } onDiscard = (): void => { const { currentPair } = this.state; if (!currentPair) { return; } this.nextState({ editedName: currentPair.getMeta().name }); } onForget = (): void => { const { currentPair } = this.state; if (!currentPair) { return; } const address = currentPair.address(); const pairs = keyring.getPairs().filter((item) => item.address() !== address ); const nextPair = pairs[pairs.length - 1] || null; const defaultPublicKey = nextPair ? nextPair.publicKey() : void 0; keyring.forgetAccount(address); this.nextState({ currentPair: nextPair, defaultPublicKey }); } } export default translate(Editor);