From 04a787d8451d0f73283650c755fa1782f83ebf7d Mon Sep 17 00:00:00 2001 From: NastiaS Date: Tue, 29 May 2018 17:56:31 +0200 Subject: [PATCH] ensuring modal unmounting for the account settings when changing the account --- .../modals/AccountSettingRenderBody.js | 197 ++++++++++++++++++ src/components/modals/SettingsAccount.js | 190 +---------------- 2 files changed, 201 insertions(+), 186 deletions(-) create mode 100644 src/components/modals/AccountSettingRenderBody.js diff --git a/src/components/modals/AccountSettingRenderBody.js b/src/components/modals/AccountSettingRenderBody.js new file mode 100644 index 00000000..46d4c2cc --- /dev/null +++ b/src/components/modals/AccountSettingRenderBody.js @@ -0,0 +1,197 @@ +// @flow + +import React, { PureComponent } from 'react' +import styled from 'styled-components' +import { connect } from 'react-redux' +import { compose } from 'redux' +import get from 'lodash/get' +import { translate } from 'react-i18next' + +import type { Account, Unit, Currency } from '@ledgerhq/live-common/lib/types' +import type { T } from 'types/common' +import { MODAL_SETTINGS_ACCOUNT } from 'config/constants' + +import { updateAccount } from 'actions/accounts' +import { setDataModal } from 'reducers/modals' + +import CryptoCurrencyIcon from 'components/CryptoCurrencyIcon' +import Box from 'components/base/Box' +import Button from 'components/base/Button' +import Input, { ErrorMessageInput } from 'components/base/Input' +import Select from 'components/base/Select/index' +import { ModalBody, ModalTitle, ModalFooter, ModalContent } from 'components/base/Modal' + +type State = { + accountName: string | null, + accountUnit: Unit | null, + accountNameError: boolean, +} + +type Props = { + setDataModal: Function, + updateAccount: Function, + t: T, + onClose: () => void, + data: any, +} + +const mapDispatchToProps = { + setDataModal, + updateAccount, +} + +const defaultState = { + accountName: null, + accountUnit: null, + accountNameError: false, +} + +class HelperComp extends PureComponent { + state = { + ...defaultState, + } + + getAccount(data: Object) { + const { accountName } = this.state + const account = get(data, 'account', {}) + + return { + ...account, + ...(accountName !== null + ? { + name: accountName, + } + : {}), + } + } + + handleChangeName = (value: string) => + this.setState({ + accountName: value, + }) + + handleSubmit = (account: Account, onClose: () => void) => ( + e: SyntheticEvent, + ) => { + e.preventDefault() + + const { updateAccount, setDataModal } = this.props + const { accountName, accountUnit } = this.state + + if (accountName !== '') { + account = { ...account, unit: accountUnit || account.unit } + updateAccount(account) + setDataModal(MODAL_SETTINGS_ACCOUNT, { account }) + onClose() + } else { + this.setState({ accountNameError: true }) + } + } + + handleFocus = (e: any, name: string) => { + e.target.select() + + switch (name) { + case 'accountName': + this.setState({ accountNameError: false }) + break + default: + break + } + } + + handleChangeUnit = (value: Unit) => { + this.setState({ accountUnit: value }) + } + + render() { + const { accountUnit, accountNameError } = this.state + const { t, onClose, data } = this.props + + const account = this.getAccount(data) + + return ( + +
+ {t('account:settings.title')} + + + + {t('account:settings.accountName.title')} + {t('account:settings.accountName.desc')} + + + } + onFocus={e => this.handleFocus(e, 'accountName')} + /> + {accountNameError && ( + {t('account:settings.accountName.error')} + )} + + + + + {t('account:settings.unit.title')} + {t('account:settings.unit.desc')} + + + } - onFocus={() => this.handleFocus('accountName')} - /> - {accountNameError && ( - - {t('account:settings.accountName.error')} - - )} - - - - - {t('account:settings.unit.title')} - {t('account:settings.unit.desc')} - - -