import * as React from "react"; import { BigNumber } from "@joincivil/typescript-types"; import { getFormattedParameterValue, POLL_QUERY } from "@joincivil/utils"; import { CivilContext, Tr, Td, StyledTableAccentText, TextCountdownTimer, ICivilContext } from "@joincivil/components"; import { StyledHiddenOnMobile } from "./Parameter"; import { Query } from "react-apollo"; export interface ProposalProps { proposal: any; parameterName: string; parameterValue: BigNumber; handleProposalAction(paramName: string, currentValue: string, newValue: string, proposal: any): void; } export class Proposal extends React.Component { public static contextType = CivilContext; public context: ICivilContext; public render(): JSX.Element { return ( {this.getFormattedValue(this.props.proposal.propValue)} {this.renderProposalStageActions()} ); } private renderProposalStageActions = (): JSX.Element => { const { pollID, applicationExpiry } = this.props.proposal; if (!pollID || pollID.eq(0)) { if (new Date(applicationExpiry * 1000).valueOf() < Date.now().valueOf()) { return this.renderUpdateParam(); } else { return this.renderCanBeChallenged(); } } else { return this.renderChallenge(); } }; private renderCanBeChallenged = (): JSX.Element => { return ( <> this.onProposalAction("challenge")}>Challenge Proposal ); }; private renderUpdateParam = (): JSX.Element => { return ( <> this.onProposalAction("update")}>Update Parameter ); }; private renderChallenge = (): JSX.Element => { const pollID = this.props.proposal.pollID.toNumber(); return ( {({ loading, error, data }) => { if (loading || error) { return <>; } const nowTimestamp = Date.now().valueOf(); const commitEndTimestamp = new Date(data.poll.commitEndDate * 1000).valueOf(); const revealEndTimestamp = new Date(data.poll.revealEndDate * 1000).valueOf(); if (nowTimestamp < commitEndTimestamp) { return this.renderCommitState(commitEndTimestamp); } else if (nowTimestamp < revealEndTimestamp) { return this.renderRevealState(revealEndTimestamp); } else { return this.renderResolveChallenge(revealEndTimestamp); } }} ); }; private renderResolveChallenge = (endTimestamp: number): JSX.Element => { return ( <> this.onProposalAction("resolve")}>Resolve Challenge ); }; private renderCommitState = (endTimestamp: number): JSX.Element => { return ( <> this.onProposalAction("commit")}>Commit Vote ); }; private renderRevealState = (endTimestamp: number): JSX.Element => { return ( <> this.onProposalAction("reveal")}>Reveal Vote ); }; private onProposalAction = (actionType: string) => { this.props.handleProposalAction( this.props.parameterName, this.getFormattedValue(this.props.parameterValue), this.getFormattedValue(this.props.proposal!.propValue), this.props.proposal, actionType, ); }; private getFormattedValue = (parameterValue: BigNumber): string => { const { civil } = this.context; return getFormattedParameterValue(this.props.parameterName, civil.toBigNumber(parameterValue.toString())); }; }