1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/ui/components/multichain/import-account/json.js
David Walsh 28137798b6
UX: Multichain: Move Add Account and Import Account into Account Menu Popover (#19346)
* UX: Multichain: Move Add Account and Import Account into Account Menu Popover

* Create a new CreateAccount component for the Account Menu

* Add actions for import form

* Use separate actions for cancel vs. submit

* Fix jest tests

* Remove commented route navigation

* Accommodate for failing import

* Fix tests

* Remove routes for new account and import

* Remove old create account page

* Move import-account files to multichain directory

* Fix paths on the import files

* Remove deprecated component library variables

* Fix error property of add form

* Fix user-actions-benchmark
2023-06-13 10:07:01 -05:00

107 lines
2.7 KiB
JavaScript

import PropTypes from 'prop-types';
import React, { useState } from 'react';
import { useSelector } from 'react-redux';
import FileInput from 'react-simple-file-input';
import {
ButtonLink,
FormTextField,
Text,
TEXT_FIELD_SIZES,
TEXT_FIELD_TYPES,
} from '../../component-library';
import {
Size,
TextVariant,
TextAlign,
} from '../../../helpers/constants/design-system';
import ZENDESK_URLS from '../../../helpers/constants/zendesk-url';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { displayWarning } from '../../../store/actions';
import BottomButtons from './bottom-buttons';
JsonImportSubview.propTypes = {
importAccountFunc: PropTypes.func.isRequired,
onActionComplete: PropTypes.func.isRequired,
};
export default function JsonImportSubview({
importAccountFunc,
onActionComplete,
}) {
const t = useI18nContext();
const warning = useSelector((state) => state.appState.warning);
const [password, setPassword] = useState('');
const [fileContents, setFileContents] = useState('');
const isPrimaryDisabled = fileContents === '';
function handleKeyPress(event) {
if (!isPrimaryDisabled && event.key === 'Enter') {
event.preventDefault();
_importAccountFunc();
}
}
function _importAccountFunc() {
if (isPrimaryDisabled) {
displayWarning(t('needImportFile'));
} else {
importAccountFunc('JSON File', [fileContents, password]);
}
}
return (
<>
<Text variant={TextVariant.bodyMd} textAlign={TextAlign.Center}>
{t('usedByClients')}
<ButtonLink
size={Size.inherit}
href={ZENDESK_URLS.IMPORTED_ACCOUNTS}
target="_blank"
rel="noopener noreferrer"
>
{t('fileImportFail')}
</ButtonLink>
</Text>
<FileInput
id="file-input"
data-testid="file-input"
readAs="text"
onLoad={(event) => setFileContents(event.target.result)}
style={{
padding: '20px 0px 12px 15%',
fontSize: '16px',
display: 'flex',
justifyContent: 'center',
width: '100%',
}}
/>
<FormTextField
id="json-password-box"
size={TEXT_FIELD_SIZES.LARGE}
autoFocus
type={TEXT_FIELD_TYPES.PASSWORD}
helpText={warning}
error
placeholder={t('enterOptionalPassword')}
value={password}
onChange={(event) => {
setPassword(event.target.value);
}}
inputProps={{
onKeyPress: handleKeyPress,
}}
marginBottom={4}
/>
<BottomButtons
importAccountFunc={_importAccountFunc}
isPrimaryDisabled={isPrimaryDisabled}
onActionComplete={onActionComplete}
/>
</>
);
}