-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.js
More file actions
272 lines (251 loc) · 9.02 KB
/
Copy pathindex.js
File metadata and controls
272 lines (251 loc) · 9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import axios from "axios"; // Requests wrapper
import dayjs from "dayjs"; // Dayjs
import { useContext, useState } from "react"; // State management
import Layout from "components/layout"; // Layout wrapper
import APICTA from "components/api_cta"; // API CTA
import { web3p, vote } from "containers"; // Context
import styles from "styles/page.module.scss"; // Page styles
import { BeatLoader } from "react-spinners"; // Loading state
import { Embedded } from "containers"; // Embedded
export default function Home(props) {
const [pages, setPages] = useState(props.defaultPages); // Proposal pagination
const embedded = useContext(Embedded);
const proposalsContent = (
<ProposalsContent {...props} pages={pages} setPages={setPages} />
);
if (embedded) {
// Don't wrap the content when embedded.
return proposalsContent;
}
return (
<Layout>
{/* Page head */}
<div className={styles.head}>
<div>
{/* Description of voting by signature */}
<h1>Vote By Signature</h1>
<div>
<p>
Voting by signature lets you place votes across Compound
Governance proposals, without having to send your transactions
on-chain, saving fees.
</p>
</div>
{/* Number of voteable proposals */}
<div>
<h2>{pages.entries}</h2>
<h3>Total Proposals</h3>
</div>
</div>
</div>
<div className={styles.body}>
{proposalsContent}
{/* Swagger API CTA card */}
<APICTA />
</div>
</Layout>
);
}
function ProposalsContent({ defaultProposals, pages, setPages }) {
const [loading, setLoading] = useState(false); // Proposal loading state
const [proposals, setProposals] = useState(defaultProposals); // Proposals array
const [buttonLoading, setButtonLoading] = useState({ id: null, type: null }); // Current button loading state
// Web3 + Authenticate function from context
const { web3, authenticate } = web3p.useContainer();
const { voteFor, voteAgainst, voteAbstain } = vote.useContainer();
/**
* Util: Uppercase first letter of word
* @param {string} word to uppercase first letter of
*/
const firstUppercase = (word) => {
if (typeof word !== "string") return "";
return word.charAt(0).toUpperCase() + word.slice(1);
};
/**
* Pagination handler
*/
const getNextPage = async () => {
// Toggle loading state
setLoading(true);
// Collect next page request string and request
const nextPage = `/api/governance/proposals?page_size=10&get_state_times=true&page_number=${
pages.current + 1
}`;
const response = await axios.get(nextPage);
// Update proposals array with new proposals
setProposals([...proposals, ...response.data.proposals]);
// Increment current page number in pages object
setPages((pages) => ({
// Destructure keys
...pages,
// Update current key with incremented value
current: pages.current + 1,
}));
// Toggle loading state
setLoading(false);
};
/**
* voteFor, voteAgainst, or abstain with loading states
* @param {number} proposalId to cast a vote on
* @param {number} type 0 === voteFor, 1 === voteAgainst, 2 === abstain
*/
const voteWithLoading = async (proposalId, type) => {
// Toggle button loading to true
setButtonLoading({ id: proposalId, type: type });
try {
// Call voteFor or voteAgainst based on type
switch (Number(type)) {
case 0:
await voteFor(proposalId);
break;
case 1:
await voteAgainst(proposalId);
break;
default:
await voteAbstain(proposalId);
}
} catch {
// If MetaMask cancellation, toggle button loading to false
setButtonLoading({ id: null, type: null });
}
// Else, toggle loading to false on success
setButtonLoading({ id: null, type: null });
};
return (
<div>
{/* Recent proposals card */}
<div className={styles.card}>
{/* Card header */}
<div>
<h4>Recent Proposals</h4>
</div>
{/* Card proposals */}
<div>
{proposals.map((proposal, i) => {
// For each proposal in proposals array return:
return (
<div className={styles.proposal} key={i}>
{/* Proposal info */}
<div>
{/* Truncated proposal name */}
<h4>
{proposal.title.split(" ").splice(0, 9).join(" ")}
{proposal.title.split(" ").length > 10 ? "..." : ""}
</h4>
{/* Proposal ID + Status + Status update date */}
<span>
{proposal.id} • {firstUppercase(proposal.state.value)}{" "}
{dayjs
.unix(proposal.state.start_time)
.format("MMMM D, YYYY")}
</span>
</div>
{/* Proposal actions */}
<div>
<button
onClick={() => {
console.log(proposal);
window.open(
// With target set to Compound governance proposal on tally
proposal.tally_url,
// In new tab
"_blank"
);
}}
className={styles.info}
>
Info
</button>
{proposal.state.value === "Active" ||
proposal.state.value === "Pending" ? (
// Check if proposal is active
web3 ? (
// If authenticated and proposal active, return voting + info buttons
<>
<button
onClick={() => voteWithLoading(proposal.id, 0)}
className={styles.for}
>
{buttonLoading.id === proposal.id &&
buttonLoading.type === 0 ? (
<BeatLoader size={9} />
) : (
"Vote For"
)}
</button>
<button
onClick={() => voteWithLoading(proposal.id, 1)}
className={styles.against}
>
{buttonLoading.id === proposal.id &&
buttonLoading.type === 1 ? (
<BeatLoader size={9} />
) : (
"Vote Against"
)}
</button>
<button
onClick={() => voteWithLoading(proposal.id, 2)}
className={styles.abstain}
>
{buttonLoading.id === proposal.id &&
buttonLoading.type === 2 ? (
<BeatLoader size={9} />
) : (
"Abstain"
)}
</button>
</>
) : (
// Else, return button to authenticate for active proposals
<button className={styles.info} onClick={authenticate}>
Authenticate to vote
</button>
)
) : // Else, return only Info button
null}
</div>
</div>
);
})}
</div>
{/* More proposals loading button */}
{pages.current < pages.max ? (
// If current number of pages < max, show:
<div className={styles.cardMore}>
{/* Load more proposals button */}
<button onClick={getNextPage} disabled={loading}>
{loading ? "Loading..." : "Load More Proposals"}
</button>
</div>
) : null}
</div>
</div>
);
}
export async function getServerSideProps() {
// Collect first page data
const deployedUrl =
process.env.VERCEL_URL == "localhost:3000"
? `http://${process.env.VERCEL_URL}`
: `https://${process.env.VERCEL_URL}`;
const firstPage =
deployedUrl +
"/api/governance/proposals?page_size=10&get_state_times=true&page_number=1";
const response = await axios.get(firstPage);
// Return:
return {
props: {
// First 10 proposals
defaultProposals: response.data.proposals,
defaultPages: {
// Number of total proposals
entries: response.data.pagination_summary.total_entries,
// Current paginated proposal page (default: 1)
current: response.data.pagination_summary.page_number,
// Maximum number of paginated proposal pages
max: response.data.pagination_summary.total_pages,
},
},
};
}