Voting.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2017-2019 @polkadot/app-democracy 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 { I18nProps } from '@polkadot/react-components/types';
  5. import BN from 'bn.js';
  6. import React, { useState } from 'react';
  7. import { Button, Dropdown, InputAddress, Modal, TxButton } from '@polkadot/react-components';
  8. import { useAccounts } from '@polkadot/react-hooks';
  9. import { isBoolean } from '@polkadot/util';
  10. import translate from '../translate';
  11. interface Props extends I18nProps {
  12. referendumId: BN | number;
  13. }
  14. function Voting ({ referendumId, t }: Props): React.ReactElement<Props> | null {
  15. const { hasAccounts } = useAccounts();
  16. const [accountId, setAccountId] = useState<string | null>(null);
  17. const [isVotingOpen, setIsVotingOpen] = useState(false);
  18. const [voteValue, setVoteValue] = useState(true);
  19. if (!hasAccounts) {
  20. return null;
  21. }
  22. const _toggleVoting = (): void => setIsVotingOpen(!isVotingOpen);
  23. const _onChangeVote = (vote?: boolean): void => setVoteValue(isBoolean(vote) ? vote : true);
  24. return (
  25. <>
  26. {isVotingOpen && (
  27. <Modal
  28. dimmer='inverted'
  29. open
  30. size='small'
  31. >
  32. <Modal.Header>{t('Vote on proposal')}</Modal.Header>
  33. <Modal.Content>
  34. <InputAddress
  35. help={t('Select the account you wish to vote with. You can approve "aye" or deny "nay" the proposal.')}
  36. label={t('vote with account')}
  37. onChange={setAccountId}
  38. type='account'
  39. withLabel
  40. />
  41. <Dropdown
  42. help={t('Select your vote preferences for this proposal, either to approve or disapprove')}
  43. label={t('record my vote as')}
  44. options={[
  45. { text: t('Aye, I approve'), value: true },
  46. { text: t('Nay, I do not approve'), value: false }
  47. ]}
  48. onChange={_onChangeVote}
  49. value={voteValue}
  50. />
  51. </Modal.Content>
  52. <Modal.Actions>
  53. <Button.Group>
  54. <Button
  55. icon='cancel'
  56. isNegative
  57. label={t('Cancel')}
  58. onClick={_toggleVoting}
  59. />
  60. <Button.Or />
  61. <TxButton
  62. accountId={accountId}
  63. icon='check'
  64. isDisabled={!accountId}
  65. isPrimary
  66. label={t('Vote')}
  67. onClick={_toggleVoting}
  68. params={[referendumId, voteValue]}
  69. tx='democracy.vote'
  70. />
  71. </Button.Group>
  72. </Modal.Actions>
  73. </Modal>
  74. )}
  75. <div className='ui--Row-buttons'>
  76. <Button
  77. icon='check'
  78. isPrimary
  79. label={t('Vote')}
  80. onClick={_toggleVoting}
  81. />
  82. </div>
  83. </>
  84. );
  85. }
  86. export default translate(Voting);