SealedVotes.tsx 1.9 KB

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