TxModalNew.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 { TxModalProps as Props } from './types';
  5. import React, { useState, useEffect } from 'react';
  6. import { Button, InputAddress, Modal } from '@polkadot/react-components';
  7. // import { useTx } from '@polkadot/react-hooks';
  8. import { isUndefined } from '@polkadot/util';
  9. import translate from './translate';
  10. function TxModal<P extends Props> ({
  11. t,
  12. accountId,
  13. onChangeAccountId,
  14. sendTx,
  15. isSending,
  16. trigger: Trigger,
  17. header = t<string>('Submit signed extrinsic'),
  18. children,
  19. preContent,
  20. isDisabled = false,
  21. isSubmittable = true,
  22. modalProps = {},
  23. inputAddressExtra,
  24. inputAddressLabel = t<string>('using my account'),
  25. inputAddressHelp = t<string>('Select the account to use for this action.'),
  26. inputAddressProps = {},
  27. submitButtonIcon = 'sign-in-alt',
  28. submitButtonLabel = t<string>('Submit'),
  29. submitButtonProps = {},
  30. ...props
  31. }: P): React.ReactElement<P> | null {
  32. const isControlled = !isUndefined(props.isOpen);
  33. const [isOpen, setIsOpen] = useState(isControlled ? props.isOpen : false);
  34. const onOpen = (): void => {
  35. !isControlled && setIsOpen(true);
  36. props.onOpen && props.onOpen();
  37. };
  38. const onClose = (): void => {
  39. !isControlled && setIsOpen(false);
  40. props.onClose && props.onClose();
  41. };
  42. const onSend = (): void => {
  43. sendTx();
  44. onClose();
  45. };
  46. // const onStart = (): void => {
  47. // props.onSubmit && props.onSubmit();
  48. // };
  49. // const onFailed = (): void => {
  50. // props.onFailed && props.onFailed();
  51. // };
  52. // const onSuccess = (): void => {
  53. // !isControlled && setIsOpen(false);
  54. // props.onSuccess && props.onSuccess();
  55. // };
  56. // const txState = useTx(
  57. // txSource,
  58. // {
  59. // ...(props.accountId ? { accountId: props.accountId } : {}),
  60. // onChangeAccountId: props.onChangeAccountId,
  61. // onStart,
  62. // onFailed,
  63. // onSuccess
  64. // }
  65. // );
  66. // const { accountId, onChangeAccountId, isSending, sendTx } = th
  67. useEffect((): void => {
  68. !isUndefined(props.isOpen) && setIsOpen(props.isOpen);
  69. }, [props.isOpen]);
  70. const allModalProps = {
  71. className: ['ui--Modal', modalProps.className || ''].join(' '),
  72. dimmer: 'inverted',
  73. header,
  74. ...modalProps,
  75. onClose,
  76. open: isOpen
  77. };
  78. return (
  79. <>
  80. {Trigger && <Trigger onOpen={onOpen} />}
  81. <Modal {...allModalProps}>
  82. <Modal.Content>
  83. {preContent}
  84. <InputAddress
  85. defaultValue={accountId}
  86. help={inputAddressHelp}
  87. isDisabled={isDisabled || isSending}
  88. isInput={false}
  89. label={inputAddressLabel}
  90. labelExtra={inputAddressExtra}
  91. onChange={onChangeAccountId}
  92. type='account'
  93. {...inputAddressProps}
  94. />
  95. {children}
  96. </Modal.Content>
  97. <Modal.Actions onCancel={onClose}>
  98. <Button
  99. icon={submitButtonIcon}
  100. isDisabled={isDisabled || isSending || !accountId || !isSubmittable}
  101. label={submitButtonLabel}
  102. onClick={onSend}
  103. {...submitButtonProps}
  104. />
  105. </Modal.Actions>
  106. </Modal>
  107. </>
  108. );
  109. }
  110. export default translate(TxModal);