Reveals.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React from 'react';
  2. import { AppProps, I18nProps } from '@polkadot/react-components/types';
  3. import { ApiProps } from '@polkadot/react-api/types';
  4. import { withCalls, withMulti } from '@polkadot/react-api/with';
  5. import { AccountId } from '@polkadot/types/interfaces';
  6. import { Input, Labelled, InputAddress } from '@polkadot/react-components/index';
  7. import translate from './translate';
  8. import { nonEmptyStr, queryToProp, getUrlParam } from '@polkadot/joy-utils/index';
  9. import { accountIdsToOptions, hashVote } from './utils';
  10. import TxButton from '@polkadot/joy-utils/TxButton';
  11. import { findVoteByHash } from './myVotesStore';
  12. import { withOnlyMembers } from '@polkadot/joy-utils/MyAccount';
  13. // AppsProps is needed to get a location from the route.
  14. type Props = AppProps & ApiProps & I18nProps & {
  15. applicantId?: string | null;
  16. applicants?: AccountId[];
  17. location: any;
  18. };
  19. type State = {
  20. applicantId?: string | null;
  21. salt?: string;
  22. hashedVote?: string | null;
  23. };
  24. class RevealVoteForm extends React.PureComponent<Props, State> {
  25. constructor (props: Props) {
  26. super(props);
  27. let { applicantId, location } = this.props;
  28. applicantId = applicantId || getUrlParam(location, 'applicantId');
  29. const hashedVote = getUrlParam(location, 'hashedVote');
  30. this.state = {
  31. applicantId,
  32. salt: '', // TODO show salts from local storage in a dropdown
  33. hashedVote
  34. };
  35. }
  36. render () {
  37. let { applicantId, salt, hashedVote } = this.state;
  38. const applicantOpts = accountIdsToOptions(this.props.applicants || []);
  39. const myVote = hashedVote ? findVoteByHash(hashedVote) : undefined;
  40. if (myVote) {
  41. // Try to substitue applicantId and salt from local sotre:
  42. if (!applicantId) applicantId = myVote.applicantId;
  43. if (!salt) salt = myVote.salt;
  44. }
  45. const hasHash = nonEmptyStr(hashedVote);
  46. const isVoteRevealed = hasHash && hashedVote === hashVote(applicantId, salt);
  47. return (
  48. <div>
  49. <div className='ui--row'>
  50. <Input
  51. label='Hashed vote (hex):'
  52. value={hashedVote}
  53. onChange={this.onChangeHash}
  54. />
  55. </div>
  56. {hasHash && <div className='ui--row'>
  57. <InputAddress
  58. label='Applicant I voted for:'
  59. onChange={this.onChangeApplicant}
  60. type='address'
  61. options={applicantOpts}
  62. value={applicantId}
  63. placeholder='Select an applicant you voted for'
  64. />
  65. </div>}
  66. {hasHash && <div className='ui--row'>
  67. <Input
  68. className='large'
  69. label='The salt used to vote for this applicant:'
  70. value={salt}
  71. onChange={this.onChangeSalt}
  72. />
  73. </div>}
  74. <Labelled style={{ marginTop: '.5rem' }}>
  75. <TxButton
  76. size='large'
  77. isDisabled={!isVoteRevealed}
  78. label='Reveal this vote'
  79. params={[hashedVote, applicantId, salt]}
  80. tx='councilElection.reveal'
  81. />
  82. </Labelled>
  83. </div>
  84. );
  85. }
  86. private onChangeApplicant = (applicantId: string | null) => {
  87. this.setState({ applicantId });
  88. }
  89. private onChangeSalt = (salt?: string) => {
  90. this.setState({ salt });
  91. }
  92. private onChangeHash = (hashedVote?: string) => {
  93. this.setState({ hashedVote });
  94. }
  95. }
  96. export default withMulti(
  97. RevealVoteForm,
  98. translate,
  99. withOnlyMembers,
  100. withCalls<Props>(
  101. queryToProp('query.councilElection.applicants')
  102. )
  103. );