Skip to content
This repository has been archived by the owner on Nov 4, 2020. It is now read-only.

Store voting history in local storage #64

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ export default class App extends Component {
favorites={favorites}
voteStartTime={voteStartTime}
voteEndTime={voteEndTime}
account={account}
/>
)} />

Expand Down
9 changes: 3 additions & 6 deletions src/MainPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function MainPage({
favorites,
voteStartTime,
voteEndTime,
account
}) {
const filteredList = React.useMemo(() => {
const sortFunctions = {
Expand Down Expand Up @@ -60,12 +61,8 @@ export default function MainPage({
<Footer
voteStartTime={voteStartTime}
voteEndTime={voteEndTime}
history={[
{ id: "EA001", votes: 2 },
{ id: "EA003", votes: 4 },
{ id: "EA002", votes: 1 }
]}
account={account}
/>
</>
);
}
}
10 changes: 8 additions & 2 deletions src/volt/components/Footer/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import React from "react";
import { FooterContainer, History, Timer } from "./styles";
import {getStoredValue} from "../../../services/localStorage";

const Footer = (props) => {
const { history } = props;
const { account } = props;

// TODO: Need optimization with use of state or context
const prevValues = getStoredValue("votesHistory", account);
const parsedHistory = prevValues ? JSON.parse(prevValues) : [];
const history = parsedHistory.reverse().slice(0, 3);

return (
<FooterContainer>
<History history={history} />
<Timer {...props} />
</FooterContainer>
);
};
Expand Down
33 changes: 18 additions & 15 deletions src/volt/components/Footer/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export const HistoryItem = styled(Text).attrs(() => ({
}))`
& span {
font-weight: bold;
&:before {
content: ">";
i {
margin-right: 4px;
}
&:after {
Expand All @@ -45,19 +44,23 @@ export const History = ({ history }) => {
return (
<HistoryContainer>
<HistoryTitle>Voice History</HistoryTitle>
{history.map(item => {
let suffix = "Voice Credit";
if (item.votes !== 1) {
suffix += "s";
}
const t = `${item.votes} ${suffix}`;

return (
<HistoryItem key={item.id}>
<span>{item.id}</span> {t}
</HistoryItem>
);
})}
{history.length > 0 ?
history.map(item => {
const {type, votes, proposalId, timestamp} = item;
let suffix = "Voice Credit";
if (Math.abs(votes) !== 1 ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always use BN

suffix += "s";
}
const t = `${item.votes} ${suffix}`;
const prefix = type === "cast" ? ">" : "<";
return (
<HistoryItem key={timestamp}>
<span><i>{prefix}</i>{proposalId}</span> {t}
</HistoryItem>
);
})
: <Text color={"voltBrandWhite"} opacity={0.6}>No votes casted</Text>
}
</HistoryContainer>
);
};
Expand Down
42 changes: 41 additions & 1 deletion src/volt/components/VoteControls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class VoteControls extends Component {
this.submitVote = this.submitVote.bind(this);
this.withdrawVote = this.withdrawVote.bind(this);

this.storeVotingAction = this.storeVotingAction.bind(this);

const treeData = this.getDataFromTree();
this.state = {
expanded: false,
Expand Down Expand Up @@ -404,6 +406,13 @@ class VoteControls extends Component {
console.log({receipt});
this.writeDataToTree(newVotesTotal, newNumOfVotes);

// Store voting action in history
this.storeVotingAction({
type: "cast",
votes: sign * newVotesTotal,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always use BN

balanceCard,
});

this.setProgressState(false);
this.setReceiptState(true);
} catch (error) {
Expand Down Expand Up @@ -519,7 +528,8 @@ class VoteControls extends Component {
const treeData = this.getDataFromTree();
console.log({treeData});

const currentVotes = utils.parseEther(castedVotes.toString());
const votes = castedVotes.toString();
const currentVotes = utils.parseEther(votes);

const data = this.cookWithdrawParams(balanceCard.id, currentVotes);

Expand Down Expand Up @@ -547,6 +557,13 @@ class VoteControls extends Component {

this.writeDataToTree(0, zeroVotes);

// Store voting action in history
this.storeVotingAction({
type: "withdraw",
votes: parseInt(votes),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only use BN

balanceCard,
});

this.setProgressState(false);
this.setReceiptState(true);

Expand Down Expand Up @@ -601,6 +618,29 @@ class VoteControls extends Component {
});
}

storeVotingAction(params){
const { account , proposal } = this.props;
const { proposalId } = proposal;
const { type, votes, balanceCard } = params;
const timestamp = new Date();

// TODO: We shall use balance card id, but needed quick solution
// const { id } = balanceCard;
const id = account;

const prevValues = getStoredValue("votesHistory", id);
const parsedHistory = prevValues ? JSON.parse(prevValues) : [];
parsedHistory.push({
type,
votes,
proposalId,
timestamp
});
const votesHistory = JSON.stringify(parsedHistory);

storeValues({votesHistory}, id);
}

render() {
const { expanded, votes, choice } = this.state;
const { showReceipt, showProgress } = this.state;
Expand Down