Add.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2017-2020 @polkadot/app-contracts authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { StringOrNull } from '@polkadot/react-components/types';
  5. import React, { useCallback, useMemo, useState } from 'react';
  6. import { createType } from '@polkadot/types';
  7. import { registry } from '@polkadot/react-api';
  8. import { Button, Input, Modal } from '@polkadot/react-components';
  9. import { useToggle } from '@polkadot/react-hooks';
  10. import { isNull } from '@polkadot/util';
  11. import { ABI, InputName } from '../shared';
  12. import ValidateCode from './ValidateCode';
  13. import store from '../store';
  14. import { useTranslation } from '../translate';
  15. import useAbi from '../useAbi';
  16. function Add (): React.ReactElement {
  17. const { t } = useTranslation();
  18. const [isOpen, toggleIsOpen, setIsOpen] = useToggle();
  19. const [codeHash, setCodeHash] = useState('');
  20. const [isCodeHashValid, setIsCodeHashValid] = useState(false);
  21. const [name, setName] = useState<StringOrNull>(null);
  22. const { abi, contractAbi, errorText, isAbiError, isAbiSupplied, isAbiValid, onChangeAbi, onRemoveAbi } = useAbi();
  23. const isNameValid = useMemo(
  24. (): boolean => !isNull(name) && name.length > 0,
  25. [name]
  26. );
  27. const isValid = useMemo(
  28. (): boolean => isCodeHashValid && isNameValid,
  29. [isCodeHashValid, isNameValid]
  30. );
  31. const _onSave = useCallback(
  32. (): void => {
  33. if (!codeHash || !name) {
  34. return;
  35. }
  36. store
  37. .saveCode(createType(registry, 'Hash', codeHash), { abi, name, tags: [] })
  38. .then((): void => setIsOpen(false))
  39. .catch((error): void => {
  40. console.error('Unable to save code', error);
  41. });
  42. },
  43. [abi, codeHash, name, setIsOpen]
  44. );
  45. return (
  46. <>
  47. <Button
  48. icon='plus'
  49. label={t('Add an existing code hash')}
  50. onClick={toggleIsOpen}
  51. />
  52. {isOpen && (
  53. <Modal header={t('Add an existing code hash')}>
  54. <Modal.Content>
  55. <Input
  56. autoFocus
  57. help={t('The code hash for the on-chain deployed code.')}
  58. isError={codeHash.length > 0 && !isCodeHashValid}
  59. label={t('code hash')}
  60. onChange={setCodeHash}
  61. value={codeHash}
  62. />
  63. <ValidateCode
  64. codeHash={codeHash}
  65. onChange={setIsCodeHashValid}
  66. />
  67. <InputName
  68. isError={!isNameValid}
  69. onChange={setName}
  70. value={name || undefined}
  71. />
  72. <ABI
  73. contractAbi={contractAbi}
  74. errorText={errorText}
  75. isError={isAbiError}
  76. isSupplied={isAbiSupplied}
  77. isValid={isAbiValid}
  78. onChange={onChangeAbi}
  79. onRemove={onRemoveAbi}
  80. withLabel
  81. />
  82. </Modal.Content>
  83. <Modal.Actions onCancel={toggleIsOpen}>
  84. <Button
  85. icon='save'
  86. isDisabled={!isValid}
  87. label={t('Save')}
  88. onClick={_onSave}
  89. />
  90. </Modal.Actions>
  91. </Modal>
  92. )}
  93. </>
  94. );
  95. }
  96. export default React.memo(Add);