Vote.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // Copyright 2017-2019 @polkadot/ui-staking 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 { VoteIndex } from '@polkadot/types/interfaces';
  5. import { DerivedVoterPositions } from '@polkadot/api-derive/types';
  6. import { ApiProps } from '@polkadot/react-api/types';
  7. import { ComponentProps } from './types';
  8. import BN from 'bn.js';
  9. import React from 'react';
  10. import styled from 'styled-components';
  11. import { createType } from '@polkadot/types';
  12. import { withApi, withCalls, withMulti } from '@polkadot/react-api';
  13. import { AddressRow, Button, Icon, Toggle, TxButton } from '@polkadot/react-components';
  14. import TxModal, { TxModalState, TxModalProps } from '@polkadot/react-components/TxModal';
  15. import translate from '../translate';
  16. interface Props extends ApiProps, ComponentProps, TxModalProps {
  17. voterPositions?: DerivedVoterPositions;
  18. }
  19. interface State extends TxModalState {
  20. approvals: boolean[] | null;
  21. oldApprovals: boolean[] | null;
  22. // voterPositions: DerivedVoterPositions;
  23. }
  24. const AlreadyVoted = styled.article`
  25. display: flex;
  26. align-items: center;
  27. margin: 0.5rem 0;
  28. & > :first-child {
  29. flex: 1 1;
  30. }
  31. & > :not(:first-child) {
  32. margin: 0;
  33. }
  34. `;
  35. const Candidates = styled.div`
  36. display: flex;
  37. flex-wrap: wrap;
  38. `;
  39. const Candidate = styled.div`
  40. cursor: pointer;
  41. width: 25rem;
  42. min-width: calc(50% - 1rem);
  43. border-radius: 0.5rem;
  44. border: 1px solid #eee;
  45. padding: 0.5rem;
  46. margin: 0.5rem;
  47. transition: all 0.2s;
  48. b {
  49. min-width: 5rem;
  50. }
  51. &.aye {
  52. background-color: rgba(0, 255, 0, 0.05);
  53. b {
  54. color: green;
  55. }
  56. }
  57. &.nay {
  58. background-color: rgba(0, 0, 0, 0.05);
  59. }
  60. `;
  61. class Vote extends TxModal<Props, State> {
  62. public static emptyApprovals (length: number): boolean[] {
  63. return [...new Array(length).keys()].map((): boolean => false);
  64. }
  65. public static getDerivedStateFromProps ({ electionsInfo: { candidateCount } }: Props, { approvals }: State): Partial<State> {
  66. const state: Partial<State> = {};
  67. // if (voterPositions) {
  68. // state.voters = Object.keys(voterSets).reduce(
  69. // (result: Record<string, VoterPosition>, accountId, globalIndex): Record<string, VoterPosition> => {
  70. // result[accountId] = {
  71. // setIndex: voterSets[accountId],
  72. // globalIndex: new BN(globalIndex)
  73. // };
  74. // return result;
  75. // },
  76. // {}
  77. // );
  78. // }
  79. if (candidateCount && !approvals) {
  80. state.approvals = state.oldApprovals || Vote.emptyApprovals(candidateCount.toNumber());
  81. }
  82. return state;
  83. }
  84. public constructor (props: Props) {
  85. super(props);
  86. this.defaultState = {
  87. ...this.defaultState,
  88. approvals: null,
  89. oldApprovals: null
  90. };
  91. this.state = {
  92. ...this.defaultState
  93. };
  94. }
  95. public componentDidMount (): void {
  96. this.fetchApprovals();
  97. }
  98. public componentDidUpdate (_: Props, prevState: State): void {
  99. const { accountId } = this.state;
  100. if (accountId !== prevState.accountId) {
  101. this.fetchApprovals();
  102. }
  103. }
  104. protected headerText = (): string => this.props.t('Vote for current candidates');
  105. protected accountLabel = (): string => this.props.t('Voting account');
  106. protected accountHelp = (): string => this.props.t('This account will be use to approve or disapprove each candidate.');
  107. protected txMethod = (): string => 'elections.setApprovals';
  108. protected txParams = (): [boolean[] | null, VoteIndex, BN | null] => {
  109. const { electionsInfo: { nextVoterSet, voteCount }, voterPositions } = this.props;
  110. const { accountId, approvals } = this.state;
  111. return [
  112. approvals ? approvals.slice(0, 1 + approvals.lastIndexOf(true)) : [],
  113. createType('VoteIndex', voteCount),
  114. voterPositions && accountId && voterPositions[accountId]
  115. ? voterPositions[accountId].setIndex
  116. : nextVoterSet
  117. ];
  118. }
  119. protected isDisabled = (): boolean => {
  120. const { accountId, oldApprovals } = this.state;
  121. return !accountId || !!oldApprovals;
  122. }
  123. protected renderTrigger = (): React.ReactNode => {
  124. const { electionsInfo: { candidates }, t } = this.props;
  125. return (
  126. <Button
  127. isDisabled={candidates.length === 0}
  128. isPrimary
  129. label={t('Vote')}
  130. icon='check'
  131. onClick={this.showModal}
  132. />
  133. );
  134. }
  135. protected renderContent = (): React.ReactNode => {
  136. const { electionsInfo: { candidates }, voterPositions, t } = this.props;
  137. const { accountId, approvals, oldApprovals } = this.state;
  138. return (
  139. <>
  140. {
  141. (oldApprovals && accountId && voterPositions && voterPositions[accountId]) && (
  142. <AlreadyVoted className='warning padded'>
  143. <div>
  144. <Icon name='warning sign' />
  145. {t('You have already voted in this round')}
  146. </div>
  147. <Button.Group>
  148. <TxButton
  149. accountId={accountId}
  150. isNegative
  151. label={t('Retract vote')}
  152. icon='delete'
  153. onSuccess={this.onRetractVote}
  154. params={[voterPositions[accountId].globalIndex]}
  155. tx='elections.retractVoter'
  156. />
  157. </Button.Group>
  158. </AlreadyVoted>
  159. )
  160. }
  161. <Candidates>
  162. {
  163. candidates.map((accountId, index): React.ReactNode => {
  164. if (!approvals) {
  165. return null;
  166. }
  167. const { [index]: isAye } = approvals;
  168. return (
  169. <Candidate
  170. className={isAye ? 'aye' : 'nay'}
  171. key={accountId.toString()}
  172. {...(
  173. !oldApprovals
  174. ? { onClick: (): void => this.onChangeVote(index)() }
  175. : {}
  176. )}
  177. >
  178. <AddressRow
  179. isInline
  180. value={accountId}
  181. >
  182. {this.renderToggle(index)}
  183. </AddressRow>
  184. </Candidate>
  185. );
  186. })
  187. }
  188. </Candidates>
  189. </>
  190. );
  191. }
  192. private renderToggle = (index: number): React.ReactNode => {
  193. const { t } = this.props;
  194. const { approvals, oldApprovals } = this.state;
  195. if (!approvals) {
  196. return null;
  197. }
  198. const { [index]: bool } = approvals;
  199. return (
  200. <Toggle
  201. isDisabled={!!oldApprovals}
  202. label={
  203. bool
  204. ? (
  205. <b>{t('Aye')}</b>
  206. )
  207. : (
  208. <b>{t('No vote')}</b>
  209. )
  210. }
  211. value={bool}
  212. />
  213. );
  214. }
  215. private emptyApprovals = (): boolean[] => {
  216. const { electionsInfo: { candidateCount } } = this.props;
  217. return Vote.emptyApprovals(candidateCount.toNumber());
  218. }
  219. private fetchApprovals = (): void => {
  220. const { api, electionsInfo: { voteCount }, voterPositions } = this.props;
  221. const { accountId } = this.state;
  222. if (!accountId) {
  223. return;
  224. }
  225. api.derive.elections.approvalsOfAt(accountId, voteCount)
  226. .then((approvals: boolean[]): void => {
  227. if ((voterPositions && voterPositions[accountId.toString()]) && approvals && approvals.length && approvals !== this.state.approvals) {
  228. this.setState({
  229. approvals,
  230. oldApprovals: approvals
  231. });
  232. } else {
  233. this.setState({
  234. approvals: this.emptyApprovals()
  235. });
  236. }
  237. });
  238. }
  239. protected onChangeAccount = (accountId: string | null): void => {
  240. this.setState({
  241. accountId,
  242. oldApprovals: null
  243. });
  244. }
  245. private onChangeVote = (index: number): (isChecked?: boolean) => void =>
  246. (isChecked?: boolean): void => {
  247. this.setState(({ approvals }: State): Pick<State, never> => {
  248. if (!approvals) {
  249. return {};
  250. }
  251. return {
  252. approvals: approvals.map((b, i): boolean => i === index ? isChecked || !approvals[index] : b)
  253. };
  254. });
  255. }
  256. private onRetractVote = (): void => {
  257. this.setState({
  258. approvals: this.emptyApprovals(),
  259. oldApprovals: null
  260. });
  261. }
  262. }
  263. export default withMulti(
  264. Vote,
  265. translate,
  266. withApi,
  267. withCalls<Props>(
  268. ['derive.elections.voterPositions', { propName: 'voterPositions' }]
  269. )
  270. );