From ca92e2e71b26cb855502b4d2bc90de83687f2a9a Mon Sep 17 00:00:00 2001 From: Maria Rushkova <66270386+mrushkova@users.noreply.github.com> Date: Thu, 1 Jul 2021 15:25:09 +0200 Subject: [PATCH] Add confirm animation (#76) --- packages/DApp/src/components/Card.tsx | 17 +++- .../src/components/card/VoteAnimatedModal.tsx | 76 ++++++++++++++++++ .../DApp/src/components/card/VoteModal.tsx | 21 +++-- .../DApp/src/components/votes/VoteChart.tsx | 25 ++++-- .../src/components/votes/VoteGraphBar.tsx | 79 ++++++++++--------- packages/DApp/src/hooks/useSendWakuVote.ts | 2 +- 6 files changed, 166 insertions(+), 54 deletions(-) create mode 100644 packages/DApp/src/components/card/VoteAnimatedModal.tsx diff --git a/packages/DApp/src/components/Card.tsx b/packages/DApp/src/components/Card.tsx index 6b6f3e9..7497264 100644 --- a/packages/DApp/src/components/Card.tsx +++ b/packages/DApp/src/components/Card.tsx @@ -17,6 +17,7 @@ import { useContracts } from '../hooks/useContracts' import { getVotingWinner } from '../helpers/voting' import { useVotesAggregate } from '../hooks/useVotesAggregate' import rightIcon from '../assets/images/arrowRight.svg' +import { VoteAnimatedModal } from './card/VoteAnimatedModal' interface CardCommunityProps { community: CommunityDetail @@ -142,6 +143,10 @@ export const CardVote = ({ community, room, hideModalFunction }: CardVoteProps) const voteConstants = voteTypes[vote.type] const winner = getVotingWinner(vote) + const availableAmount = 65800076 + + const initialProposing = vote?.type === 'Remove' && availableAmount > 2000000 ? 2000000 : 0 + const [proposingAmount, setProposingAmount] = useState(initialProposing) return ( @@ -151,14 +156,22 @@ export const CardVote = ({ community, room, hideModalFunction }: CardVoteProps) vote={vote} room={room} selectedVote={selectedVoted} - availableAmount={65245346} + availableAmount={availableAmount} + proposingAmount={proposingAmount} setShowConfirmModal={setNext} + setProposingAmount={setProposingAmount} />{' '} )} {showConfirmModal && ( - + )} {winner ? ( diff --git a/packages/DApp/src/components/card/VoteAnimatedModal.tsx b/packages/DApp/src/components/card/VoteAnimatedModal.tsx new file mode 100644 index 0000000..748de44 --- /dev/null +++ b/packages/DApp/src/components/card/VoteAnimatedModal.tsx @@ -0,0 +1,76 @@ +import React from 'react' +import styled from 'styled-components' +import { VoteType } from '../../constants/voteTypes' +import { CommunityDetail, CurrentVoting } from '../../models/community' +import { ButtonSecondary } from '../Button' +import { VoteChart } from '../votes/VoteChart' + +interface VoteAnimatedModalProps { + community: CommunityDetail + vote: CurrentVoting + selectedVote: VoteType + proposingAmount: number + setShowModal: (val: boolean) => void +} + +export function VoteAnimatedModal({ + community, + vote, + proposingAmount, + selectedVote, + setShowModal, +}: VoteAnimatedModalProps) { + return ( + + + + Your vote{' '} + + {selectedVote.verb} {community.name} + {' '} + has been cast! + + + + setShowModal(false)}> + OK, let’s move on! 🤙 + + + ) +} + +const VoteConfirm = styled.div` + display: flex; + flex-direction: column; + align-items: center; +` + +const ConfirmLogo = styled.img` + width: 64px !important; + height: 64px !important; + border-radius: 50%; + margin-bottom: 32px; +` + +const ConfirmText = styled.div` + max-width: 272px; + margin-bottom: 32px; + text-align: center; + line-height: 22px; + + & > span { + font-weight: bold; + } +` + +export const ConfirmBtn = styled(ButtonSecondary)` + width: 100%; + padding: 11px 0; + font-weight: 500; + font-size: 15px; + line-height: 22px; + + & > span { + font-size: 20px; + } +` diff --git a/packages/DApp/src/components/card/VoteModal.tsx b/packages/DApp/src/components/card/VoteModal.tsx index 40b133d..f07057b 100644 --- a/packages/DApp/src/components/card/VoteModal.tsx +++ b/packages/DApp/src/components/card/VoteModal.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react' +import React from 'react' import styled from 'styled-components' import { VoteChart } from '../votes/VoteChart' import { ButtonSecondary } from '../Button' @@ -12,14 +12,21 @@ export interface VoteModalProps { vote: CurrentVoting selectedVote: VoteType availableAmount: number + proposingAmount: number room: number setShowConfirmModal: (show: boolean) => void + setProposingAmount: (val: number) => void } -export function VoteModal({ vote, room, selectedVote, availableAmount, setShowConfirmModal }: VoteModalProps) { - const initialProposing = vote?.type === 'Remove' && availableAmount > 2000000 ? 2000000 : 0 - - const [proposingAmount, setProposingAmount] = useState(initialProposing) +export function VoteModal({ + vote, + room, + selectedVote, + availableAmount, + proposingAmount, + setShowConfirmModal, + setProposingAmount, +}: VoteModalProps) { const disabled = proposingAmount === 0 const sendWakuVote = useSendWakuVote() @@ -36,8 +43,8 @@ export function VoteModal({ vote, room, selectedVote, availableAmount, setShowCo /> { - sendWakuVote(proposingAmount, room, selectedVote.type) + onClick={async () => { + await sendWakuVote(proposingAmount, room, selectedVote.type) setShowConfirmModal(true) }} disabled={disabled} diff --git a/packages/DApp/src/components/votes/VoteChart.tsx b/packages/DApp/src/components/votes/VoteChart.tsx index c99098e..b59f47d 100644 --- a/packages/DApp/src/components/votes/VoteChart.tsx +++ b/packages/DApp/src/components/votes/VoteChart.tsx @@ -13,10 +13,26 @@ export interface VoteChartProps { voteWinner?: number proposingAmount?: number selectedVote?: VoteType + isAnimation?: boolean } -export function VoteChart({ vote, voteWinner, proposingAmount, selectedVote }: VoteChartProps) { +export function VoteChart({ vote, voteWinner, proposingAmount, selectedVote, isAnimation }: VoteChartProps) { const voteConstants = voteTypes[vote.type] + + const votesFor = vote.voteFor.toNumber() + const votesAgainst = vote.voteAgainst.toNumber() + const voteSum = votesFor + votesAgainst + const graphWidth = (100 * votesAgainst) / voteSum + + let balanceWidth = graphWidth + + if (proposingAmount && selectedVote) { + balanceWidth = + selectedVote.type === 0 + ? (100 * (votesAgainst + proposingAmount)) / (voteSum + proposingAmount) + : (100 * votesAgainst) / (voteSum + proposingAmount) + } + return ( @@ -42,11 +58,10 @@ export function VoteChart({ vote, voteWinner, proposingAmount, selectedVote }: V ) diff --git a/packages/DApp/src/components/votes/VoteGraphBar.tsx b/packages/DApp/src/components/votes/VoteGraphBar.tsx index 4337009..f5d488b 100644 --- a/packages/DApp/src/components/votes/VoteGraphBar.tsx +++ b/packages/DApp/src/components/votes/VoteGraphBar.tsx @@ -1,56 +1,56 @@ -import React from 'react' +import React, { useEffect, useState } from 'react' import styled from 'styled-components' import { Colors } from '../../constants/styles' import indicatorIcon from '../../assets/images/indicator.svg' -import { VoteType } from '../../constants/voteTypes' +function createKeyFrames(votesWidth: number, markerWidth: number) { + return ` + @keyframes fade-in { + 0% { + width: ${votesWidth}%; + } + 100% { + width: ${markerWidth}%; + } + } + ` +} export interface VoteGraphBarProps { - votesAgainst: number - votesFor: number + balanceWidth?: number + graphWidth?: number voteWinner?: number - proposingAmount?: number - selectedVote?: VoteType + isAnimation?: boolean } -export function VoteGraphBar({ votesFor, votesAgainst, voteWinner, proposingAmount, selectedVote }: VoteGraphBarProps) { - const voteSum = votesFor + votesAgainst - const graphWidth = (100 * votesAgainst) / voteSum +export function VoteGraphBar({ graphWidth, balanceWidth, isAnimation }: VoteGraphBarProps) { + const markerWidth: number = balanceWidth ? balanceWidth : 3 + const votesWidth: number = graphWidth ? graphWidth : 3 + const [keyFrames, setKeyFrames] = useState('') + const [style, setStyle] = useState({ width: `${votesWidth}%` }) - let balanceWidth = graphWidth - - if (proposingAmount && selectedVote) { - balanceWidth = - selectedVote.type === 0 - ? (100 * (votesAgainst + proposingAmount)) / (voteSum + proposingAmount) - : (100 * votesAgainst) / (voteSum + proposingAmount) - } + useEffect(() => { + if (isAnimation) { + setStyle({ width: `${markerWidth}%`, animation: 'fade-in 2s ease' }) + setKeyFrames(createKeyFrames(votesWidth, markerWidth)) + } else { + setStyle({ width: `${votesWidth}%` }) + } + }, [isAnimation, votesWidth]) return ( - - - + +