ghost-portal/packages/page-contracts/src/Codes/Add.tsx
Jaco Greeff b22f61347e
Remove unused TxModal components (#4250)
* Remove unused TxModal components

* Cleanups
2020-12-20 17:50:06 +01:00

88 lines
2.5 KiB
TypeScript

// Copyright 2017-2020 @polkadot/app-contracts authors & contributors
// SPDX-License-Identifier: Apache-2.0
import React, { useCallback, useState } from 'react';
import { Button, Input, Modal } from '@polkadot/react-components';
import { isNull } from '@polkadot/util';
import { ABI, InputName } from '../shared';
import store from '../store';
import { useTranslation } from '../translate';
import useAbi from '../useAbi';
import ValidateCode from './ValidateCode';
interface Props {
onClose: () => void;
}
function Add ({ onClose }: Props): React.ReactElement {
const { t } = useTranslation();
const [codeHash, setCodeHash] = useState('');
const [isCodeHashValid, setIsCodeHashValid] = useState(false);
const [name, setName] = useState<string | null>(null);
const { abi, contractAbi, errorText, isAbiError, isAbiSupplied, isAbiValid, onChangeAbi, onRemoveAbi } = useAbi();
const _onSave = useCallback(
(): void => {
if (!codeHash || !name) {
return;
}
store
.saveCode(codeHash, { abi, name, tags: [] })
.then(() => onClose())
.catch((error): void => {
console.error('Unable to save code', error);
});
},
[abi, codeHash, name, onClose]
);
const isNameValid = !isNull(name) && name.length > 0;
const isValid = isCodeHashValid && isNameValid && isAbiSupplied && isAbiValid;
return (
<Modal header={t('Add an existing code hash')}>
<Modal.Content>
<Input
autoFocus
help={t('The code hash for the on-chain deployed code.')}
isError={codeHash.length > 0 && !isCodeHashValid}
label={t('code hash')}
onChange={setCodeHash}
value={codeHash}
/>
<ValidateCode
codeHash={codeHash}
onChange={setIsCodeHashValid}
/>
<InputName
isError={!isNameValid}
onChange={setName}
value={name || undefined}
/>
<ABI
contractAbi={contractAbi}
errorText={errorText}
isError={isAbiError || !isAbiError}
isSupplied={isAbiSupplied}
isValid={isAbiValid}
onChange={onChangeAbi}
onRemove={onRemoveAbi}
/>
</Modal.Content>
<Modal.Actions onCancel={onClose}>
<Button
icon='save'
isDisabled={!isValid}
label={t('Save')}
onClick={_onSave}
/>
</Modal.Actions>
</Modal>
);
}
export default React.memo(Add);