SealedVotes.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import { Button } from 'semantic-ui-react';
  4. import { I18nProps } from '@polkadot/react-components/types';
  5. import { ApiProps } from '@polkadot/react-api/types';
  6. import { withCalls } from '@polkadot/react-api/with';
  7. import { Hash } from '@polkadot/types/interfaces';
  8. import translate from './translate';
  9. import SealedVote from './SealedVote';
  10. import { queryToProp } from '@polkadot/joy-utils/index';
  11. import { MyAddressProps } from '@polkadot/joy-utils/MyAccount';
  12. import { SavedVote } from './myVotesStore';
  13. import Section from '@polkadot/joy-utils/Section';
  14. type Props = ApiProps & I18nProps & MyAddressProps & {
  15. myVotes?: SavedVote[];
  16. commitments?: Hash[];
  17. };
  18. class Comp extends React.PureComponent<Props> {
  19. private filterVotes = (myVotesOnly: boolean): Hash[] => {
  20. const { myVotes = [], commitments = [] } = this.props;
  21. const isMyVote = (hash: string): boolean => {
  22. return myVotes.find(x => x.hash === hash) !== undefined;
  23. };
  24. return commitments.filter(x => myVotesOnly === isMyVote(x.toHex()));
  25. }
  26. private renderVotes = (votes: Hash[]) => {
  27. return votes.map((hash, index) =>
  28. <SealedVote key={index} hash={hash} />
  29. );
  30. }
  31. render () {
  32. const myVotes = this.filterVotes(true);
  33. const otherVotes = this.filterVotes(false);
  34. return <>
  35. <Section title={`My previous votes (${myVotes.length})`}>{
  36. !myVotes.length
  37. ? <em>No votes by the current account found on the current browser.</em>
  38. : this.renderVotes(myVotes)
  39. }</Section>
  40. <Section title={`Other votes (${otherVotes.length})`}>
  41. <Button primary as={Link} to="reveals">Reveal a vote</Button>
  42. {
  43. !otherVotes.length
  44. ? <em>No votes submitted by other accounts yet.</em>
  45. : this.renderVotes(otherVotes)
  46. }
  47. </Section>
  48. </>;
  49. }
  50. }
  51. // inject the actual API calls automatically into props
  52. export default translate(
  53. withCalls<Props>(
  54. queryToProp('query.councilElection.commitments')
  55. )(Comp)
  56. );