TipCreate.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2017-2020 @polkadot/app-treasury 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 BN from 'bn.js';
  5. import React, { useEffect, useState } from 'react';
  6. import { Button, Input, InputAddress, InputBalance, Modal, TxButton } from '@polkadot/react-components';
  7. import { useToggle } from '@polkadot/react-hooks';
  8. import { useTranslation } from '../translate';
  9. interface Props {
  10. members: string[];
  11. refresh: () => void;
  12. }
  13. const MAX_REASON_LEN = 128;
  14. const MIN_REASON_LEN = 5;
  15. function TipCreate ({ members, refresh }: Props): React.ReactElement<Props> {
  16. const { t } = useTranslation();
  17. const [isOpen, toggleOpen] = useToggle();
  18. const [accountId, setAccountId] = useState<string | null>(null);
  19. const [beneficiary, setBeneficiary] = useState<string | null>(null);
  20. const [isMember, setIsMember] = useState(false);
  21. const [reason, setReason] = useState('');
  22. const [value, setValue] = useState<BN | undefined>();
  23. const hasValue = value?.gtn(0);
  24. const hasReason = reason?.length >= MIN_REASON_LEN && reason?.length <= MAX_REASON_LEN;
  25. useEffect((): void => {
  26. setIsMember(
  27. accountId
  28. ? members.includes(accountId)
  29. : false
  30. );
  31. }, [accountId, members]);
  32. return (
  33. <>
  34. <Button
  35. icon='plus'
  36. label={t<string>('Propose tip')}
  37. onClick={toggleOpen}
  38. />
  39. {isOpen && (
  40. <Modal
  41. header={t<string>('Submit tip request')}
  42. size='large'
  43. >
  44. <Modal.Content>
  45. <Modal.Columns>
  46. <Modal.Column>
  47. <InputAddress
  48. help={t<string>('Select the account you wish to submit the tip from.')}
  49. label={t<string>('submit with account')}
  50. onChange={setAccountId}
  51. type='account'
  52. withLabel
  53. />
  54. </Modal.Column>
  55. <Modal.Column>
  56. <p>{t<string>('Use this account to request the tip from. This can be a normal or council account.')}</p>
  57. </Modal.Column>
  58. </Modal.Columns>
  59. <Modal.Columns>
  60. <Modal.Column>
  61. <InputAddress
  62. help={t<string>('The account to which the tip will be transferred if approved')}
  63. label={t<string>('beneficiary')}
  64. onChange={setBeneficiary}
  65. type='allPlus'
  66. />
  67. </Modal.Column>
  68. <Modal.Column>
  69. <p>{t<string>('The beneficiary will received the tip as approved by council members.')}</p>
  70. </Modal.Column>
  71. </Modal.Columns>
  72. <Modal.Columns>
  73. <Modal.Column>
  74. <Input
  75. autoFocus
  76. help={t<string>('The reason why this tip should be paid.')}
  77. isError={!hasReason}
  78. label={t<string>('tip reason')}
  79. onChange={setReason}
  80. />
  81. </Modal.Column>
  82. <Modal.Column>
  83. <p>{t<string>('A reason (to be stored-on-chain) as to why the recipient deserves a tip payout.')}</p>
  84. </Modal.Column>
  85. </Modal.Columns>
  86. {isMember && (
  87. <Modal.Columns>
  88. <Modal.Column>
  89. <InputBalance
  90. help={t<string>('The suggested value for this tip')}
  91. isError={!hasValue}
  92. label={t<string>('tip value')}
  93. onChange={setValue}
  94. />
  95. </Modal.Column>
  96. <Modal.Column>
  97. <p>{t<string>('As a council member, you can suggest an initial value for the tip, each other council member can suggest their own.')}</p>
  98. </Modal.Column>
  99. </Modal.Columns>
  100. )}
  101. </Modal.Content>
  102. <Modal.Actions onCancel={toggleOpen}>
  103. <TxButton
  104. accountId={accountId}
  105. icon='plus'
  106. isDisabled={!accountId || (isMember ? !hasValue : false) || !hasReason}
  107. label={t<string>('Propose tip')}
  108. onStart={toggleOpen}
  109. onSuccess={refresh}
  110. params={
  111. isMember
  112. ? [reason, beneficiary, value]
  113. : [reason, beneficiary]
  114. }
  115. tx={
  116. isMember
  117. ? 'treasury.tipNew'
  118. : 'treasury.reportAwesome'
  119. }
  120. />
  121. </Modal.Actions>
  122. </Modal>
  123. )}
  124. </>
  125. );
  126. }
  127. export default React.memo(TipCreate);