1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 23:58:06 +01:00
metamask-extension/ui/pages/create-account/new-account.component.js

115 lines
3.4 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import Button from '../../components/ui/button';
import { EVENT, EVENT_NAMES } from '../../../shared/constants/metametrics';
import { getAccountNameErrorMessage } from '../../helpers/utils/accounts';
export default class NewAccountCreateForm extends Component {
static defaultProps = {
newAccountNumber: 0,
};
state = {
newAccountName: '',
defaultAccountName: this.context.t('newAccountNumberName', [
this.props.newAccountNumber,
]),
};
2020-11-03 00:41:28 +01:00
render() {
const { newAccountName, defaultAccountName } = this.state;
2022-07-31 20:26:40 +02:00
const { history, createAccount, mostRecentOverviewPage, accounts } =
this.props;
const createClick = (event) => {
event.preventDefault();
createAccount(newAccountName || defaultAccountName)
.then(() => {
2022-03-29 15:46:24 +02:00
this.context.trackEvent({
category: EVENT.CATEGORIES.ACCOUNTS,
event: EVENT_NAMES.ACCOUNT_ADDED,
2022-03-29 15:46:24 +02:00
properties: {
account_type: EVENT.ACCOUNT_TYPES.DEFAULT,
},
});
history.push(mostRecentOverviewPage);
})
.catch((e) => {
2022-03-29 15:46:24 +02:00
this.context.trackEvent({
category: EVENT.CATEGORIES.ACCOUNTS,
event: EVENT_NAMES.ACCOUNT_ADD_FAILED,
2022-03-29 15:46:24 +02:00
properties: {
account_type: EVENT.ACCOUNT_TYPES.DEFAULT,
error: e.message,
},
});
});
};
const { isValidAccountName, errorMessage } = getAccountNameErrorMessage(
accounts,
this.context,
newAccountName,
defaultAccountName,
);
return (
<div className="new-account-create-form">
<div className="new-account-create-form__input-label">
{this.context.t('accountName')}
</div>
<div>
<input
className={classnames({
'new-account-create-form__input': true,
'new-account-create-form__input__error': !isValidAccountName,
})}
value={newAccountName}
placeholder={defaultAccountName}
2020-11-03 00:41:28 +01:00
onChange={(event) =>
this.setState({ newAccountName: event.target.value })
}
autoFocus
/>
<div className="new-account-create-form__error new-account-create-form__error-amount">
{errorMessage}
</div>
<div className="new-account-create-form__buttons">
<Button
type="secondary"
large
className="new-account-create-form__button"
onClick={() => history.push(mostRecentOverviewPage)}
>
{this.context.t('cancel')}
</Button>
<Button
type="primary"
large
className="new-account-create-form__button"
onClick={createClick}
disabled={!isValidAccountName}
>
{this.context.t('create')}
</Button>
</div>
</div>
</div>
);
}
}
NewAccountCreateForm.propTypes = {
createAccount: PropTypes.func,
newAccountNumber: PropTypes.number,
history: PropTypes.object,
mostRecentOverviewPage: PropTypes.string.isRequired,
accounts: PropTypes.array,
};
NewAccountCreateForm.contextTypes = {
t: PropTypes.func,
2022-03-29 15:46:24 +02:00
trackEvent: PropTypes.func,
};