Skip to content

Commit 8eb05d0

Browse files
authored
Merge pull request #169 from AElfProject/hotfix/vote-table
feat: vote node table
2 parents 1b2f376 + a04bb1b commit 8eb05d0

File tree

2 files changed

+96
-71
lines changed

2 files changed

+96
-71
lines changed

src/pages/Vote/ElectionNotification/NodeTable/index.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { getPublicKeyFromObject } from "../../../../utils/getPublicKey";
3434
import TableLayer from "../../../../components/TableLayer/TableLayer";
3535

3636
const clsPrefix = "node-table";
37-
37+
const TableItemCount = 20;
3838
class NodeTable extends PureComponent {
3939
constructor(props) {
4040
super(props);
@@ -50,10 +50,6 @@ class NodeTable extends PureComponent {
5050
showTotal: (total) => `Total ${total} items`,
5151
pageSize: 20,
5252
showSizeChanger: false,
53-
onChange: (e) => {
54-
const informationStart = (e - 1) * 20;
55-
this.fetchNodes({}, informationStart);
56-
},
5753
},
5854
};
5955
this.socket = io({
@@ -67,7 +63,6 @@ class NodeTable extends PureComponent {
6763
componentDidMount() {
6864
this.wsProducedBlocks();
6965
if (this.props.electionContract && this.props.consensusContract) {
70-
this.fetchTotal();
7166
this.fetchNodes({});
7267
}
7368
}
@@ -82,11 +77,9 @@ class NodeTable extends PureComponent {
8277
this.props.electionContract &&
8378
this.props.consensusContract
8479
) {
85-
this.fetchTotal();
8680
this.fetchNodes({});
8781
}
8882
if (this.props.nodeTableRefreshTime !== prevProps.nodeTableRefreshTime) {
89-
this.fetchTotal();
9083
this.fetchNodes({});
9184
}
9285
if (this.props.electionContract && this.props.consensusContract) {
@@ -95,7 +88,6 @@ class NodeTable extends PureComponent {
9588
(this.props.currentWallet &&
9689
this.props.currentWallet.address !== prevProps.currentWallet.address)
9790
) {
98-
this.fetchTotal();
9991
this.fetchNodes({});
10092
}
10193
}
@@ -364,21 +356,38 @@ class NodeTable extends PureComponent {
364356
// }
365357
// }
366358

367-
fetchTotal() {
368-
fetchCount(this.props.electionContract, "").then((res) => {
369-
const total = res.value?.length || 0;
370-
const pagination = {
371-
...this.state.pagination,
372-
total,
373-
};
374-
this.setState({
375-
pagination,
376-
});
359+
async fetchTotal() {
360+
const res = await fetchCount(this.props.electionContract, "");
361+
const total = res.value?.length || 0;
362+
const pagination = {
363+
...this.state.pagination,
364+
total,
365+
};
366+
this.setState({
367+
pagination,
377368
});
369+
return total;
378370
}
371+
372+
async fetchAllCandidateInfo() {
373+
const total = await this.fetchTotal();
374+
const { electionContract } = this.props;
375+
let start = 0;
376+
let result = [];
377+
while (start <= total) {
378+
const res = await fetchPageableCandidateInformation(electionContract, {
379+
start,
380+
length: TableItemCount,
381+
});
382+
result = result.concat(res.value);
383+
start += 20;
384+
}
385+
return result;
386+
}
387+
379388
// todo: the comment as follows maybe wrong, the data needs to share is the user's vote records
380389
// todo: consider to move the method to Vote comonent, because that also NodeTable and Redeem Modal needs the data;
381-
fetchNodes(currentWalletInput, informationStart = 0) {
390+
fetchNodes(currentWalletInput) {
382391
this.setState({
383392
isLoading: true,
384393
});
@@ -387,10 +396,7 @@ class NodeTable extends PureComponent {
387396
(Object.keys(currentWalletInput).length && currentWalletInput) ||
388397
this.props.currentWallet;
389398
Promise.all([
390-
fetchPageableCandidateInformation(electionContract, {
391-
start: informationStart,
392-
length: 20, // A_NUMBER_LARGE_ENOUGH_TO_GET_ALL
393-
}),
399+
this.fetchAllCandidateInfo(),
394400
getAllTeamDesc(),
395401
currentWallet && currentWallet.publicKey
396402
? fetchElectorVoteWithRecords(electionContract, {
@@ -432,7 +438,7 @@ class NodeTable extends PureComponent {
432438
const { producedBlocks } = this.state;
433439

434440
let totalActiveVotesAmount = 0;
435-
const nodeInfos = resArr[0] ? resArr[0].value : [];
441+
const nodeInfos = resArr[0] || [];
436442
const { activeVotingRecords } = resArr[2] || {};
437443
let teamInfos = null;
438444
if (resArr[1].code === 0) {

src/pages/Vote/TeamDetail/index.js

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import React, { PureComponent } from "react";
22
import { Link } from "react-router-dom";
33
import { If, Then, Else } from "react-if";
4-
import { Row, Col, Button, Avatar, Tag, Typography, message, Icon } from "antd";
4+
import { Row, Col, Button, Avatar, Tag, Typography, message } from "antd";
55
import queryString from "query-string";
66
import { EditOutlined, TeamOutlined } from "@ant-design/icons";
77

88
import StatisticalData from "@components/StatisticalData/";
9-
import { getTeamDesc, fetchElectorVoteWithRecords } from "@api/vote";
9+
import {
10+
getTeamDesc,
11+
fetchElectorVoteWithRecords,
12+
fetchPageableCandidateInformation,
13+
fetchCount,
14+
} from "@api/vote";
1015
import { fetchCurrentMinerPubkeyList } from "@api/consensus";
1116
import {
1217
FROM_WALLET,
@@ -28,6 +33,8 @@ const clsPrefix = "team-detail";
2833

2934
const ellipsis = { rows: 1 };
3035

36+
const TableItemCount = 20;
37+
3138
class TeamDetail extends PureComponent {
3239
constructor(props) {
3340
super(props);
@@ -106,18 +113,30 @@ class TeamDetail extends PureComponent {
106113
this.fetchTheUsersActiveVoteRecords();
107114
}
108115

109-
fetchAllCandidateInfo() {
110-
const { electionContract } = this.props;
116+
async fetchTotal() {
117+
const res = await fetchCount(this.props.electionContract, "");
118+
const total = res.value?.length || 0;
119+
return total;
120+
}
111121

112-
electionContract.GetPageableCandidateInformation.call({
113-
start: 0,
114-
length: A_NUMBER_LARGE_ENOUGH_TO_GET_ALL, // give a number large enough to make sure that we get all the nodes
115-
// FIXME: [unstable] sometimes any number large than 5 assign to length will cost error when fetch data
116-
})
117-
.then((res) => this.processAllCandidateInfo(res.value))
118-
.catch((err) => {
119-
console.error(err);
120-
});
122+
async fetchAllCandidateInfo() {
123+
try {
124+
const total = await this.fetchTotal();
125+
const { electionContract } = this.props;
126+
let start = 0;
127+
let result = [];
128+
while (start <= total) {
129+
const res = await fetchPageableCandidateInformation(electionContract, {
130+
start,
131+
length: TableItemCount,
132+
});
133+
result = result.concat(res.value);
134+
start += 20;
135+
}
136+
this.processAllCandidateInfo(result);
137+
} catch (e) {
138+
console.error(e);
139+
}
121140
}
122141

123142
processAllCandidateInfo(allCandidateInfo) {
@@ -257,21 +276,21 @@ class TeamDetail extends PureComponent {
257276
return (
258277
<section className={`${clsPrefix}-header card-container`}>
259278
<Row>
260-
<Col md={18} sm={24} xs={24} className='card-container-left'>
279+
<Col md={18} sm={24} xs={24} className="card-container-left">
261280
<Row className={`${clsPrefix}-team-avatar-info`}>
262-
<Col md={6} sm={6} xs={6} className='team-avatar-container'>
281+
<Col md={6} sm={6} xs={6} className="team-avatar-container">
263282
{data.avatar ? (
264-
<Avatar shape='square' size={avatarSize} src={data.avatar} />
283+
<Avatar shape="square" size={avatarSize} src={data.avatar} />
265284
) : (
266-
<Avatar shape='square' size={avatarSize}>
285+
<Avatar shape="square" size={avatarSize}>
267286
U
268287
</Avatar>
269288
)}
270289
</Col>
271290
<Col className={`${clsPrefix}-team-info`} md={18} sm={18} xs={18}>
272291
<h5 className={`${clsPrefix}-node-name ellipsis`}>
273292
{data.name ? data.name : formattedAddress}
274-
<Tag color='#f50'>
293+
<Tag color="#f50">
275294
{isBP ? "BP" : isCandidate ? "Candidate" : "Quited"}
276295
</Tag>
277296
</h5>
@@ -290,8 +309,8 @@ class TeamDetail extends PureComponent {
290309
Official Website:&nbsp;
291310
<a
292311
href={data.officialWebsite}
293-
target='_blank'
294-
rel='noreferrer noopener'
312+
target="_blank"
313+
rel="noreferrer noopener"
295314
>
296315
{data.officialWebsite}
297316
</a>
@@ -304,16 +323,16 @@ class TeamDetail extends PureComponent {
304323
Email:&nbsp;
305324
<a
306325
href={`mailto:${data.mail}`}
307-
target='_blank'
308-
rel='noreferrer noopener'
326+
target="_blank"
327+
rel="noreferrer noopener"
309328
>
310329
{data.mail}
311330
</a>
312331
</Paragraph>
313332
</Then>
314333
</If>
315334
{hasAuth ? (
316-
<Button type='primary' shape='round' className='edit-btn'>
335+
<Button type="primary" shape="round" className="edit-btn">
317336
<Link
318337
to={{
319338
pathname: "/vote/apply/keyin",
@@ -327,13 +346,13 @@ class TeamDetail extends PureComponent {
327346
</Col>
328347
</Row>
329348
</Col>
330-
<Col md={6} xs={0} className='card-container-right'>
349+
<Col md={6} xs={0} className="card-container-right">
331350
<Button
332-
className='table-btn vote-btn'
333-
type='primary'
334-
shape='round'
351+
className="table-btn vote-btn"
352+
type="primary"
353+
shape="round"
335354
disabled={!isCandidate}
336-
data-role='vote'
355+
data-role="vote"
337356
data-shoulddetectlock
338357
data-votetype={FROM_WALLET}
339358
data-nodeaddress={formattedAddress}
@@ -343,10 +362,10 @@ class TeamDetail extends PureComponent {
343362
Vote
344363
</Button>
345364
<Button
346-
className='table-btn redeem-btn'
347-
type='primary'
348-
shape='round'
349-
data-role='redeem'
365+
className="table-btn redeem-btn"
366+
type="primary"
367+
shape="round"
368+
data-role="redeem"
350369
data-shoulddetectlock
351370
data-nodeaddress={formattedAddress}
352371
data-targetpublickey={this.teamPubkey}
@@ -372,43 +391,43 @@ class TeamDetail extends PureComponent {
372391
{topTeamInfo}
373392
<StatisticalData data={staticsData} inline />
374393
<section className={`${clsPrefix}-intro card-container`}>
375-
<h5 className='card-header'>
376-
<EditOutlined className='card-header-icon' />
394+
<h5 className="card-header">
395+
<EditOutlined className="card-header-icon" />
377396
Introduction
378397
</h5>
379-
<div className='card-content'>
398+
<div className="card-content">
380399
<If condition={!!data.intro}>
381400
<Then>
382401
<p>{data.intro}</p>
383402
</Then>
384403
<Else>
385-
<div className='vote-team-detail-empty'>
404+
<div className="vote-team-detail-empty">
386405
The team didn't fill the introduction.
387406
</div>
388407
</Else>
389408
</If>
390409
</div>
391410
</section>
392411
<section className={`${clsPrefix}-social-network card-container`}>
393-
<h5 className='card-header'>
394-
<TeamOutlined className='card-header-icon' />
412+
<h5 className="card-header">
413+
<TeamOutlined className="card-header-icon" />
395414
Social Network
396415
</h5>
397-
<div className='card-content'>
416+
<div className="card-content">
398417
<If condition={!!(data.socials && data.socials.length > 0)}>
399418
<Then>
400-
<div className='vote-team-detail-social-network'>
419+
<div className="vote-team-detail-social-network">
401420
{(data.socials || []).map((item) => (
402-
<div className='vote-team-detail-social-network-item'>
403-
<span className='vote-team-detail-social-network-item-title'>
421+
<div className="vote-team-detail-social-network-item">
422+
<span className="vote-team-detail-social-network-item-title">
404423
{item.type}
405424
</span>
406-
<span className='vote-team-detail-social-network-item-url'>
425+
<span className="vote-team-detail-social-network-item-url">
407426
:&nbsp;
408427
<a
409428
href={item.url}
410-
target='_blank'
411-
rel='noreferrer noopener'
429+
target="_blank"
430+
rel="noreferrer noopener"
412431
>
413432
{item.url}
414433
</a>
@@ -418,7 +437,7 @@ class TeamDetail extends PureComponent {
418437
</div>
419438
</Then>
420439
<Else>
421-
<span className='vote-team-detail-empty'>
440+
<span className="vote-team-detail-empty">
422441
The team didn't fill the social contacts.
423442
</span>
424443
</Else>

0 commit comments

Comments
 (0)