-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa26797
commit a383c34
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.box { | ||
composes: box from 'chora/styles/box.css'; | ||
} | ||
|
||
.boxItem { | ||
composes: box-item from 'chora/styles/box.css'; | ||
} | ||
|
||
.boxText { | ||
composes: box-text from 'chora/styles/box.css'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use client' | ||
|
||
import { Result } from 'chora/components' | ||
import { PaginationNav } from 'chora/components/tables' | ||
import { WalletContext } from 'chora/contexts' | ||
import { useParams } from 'next/navigation' | ||
import { useContext, useState } from 'react' | ||
|
||
import { useProposalVotes } from '@hooks/useProposalVotes' | ||
|
||
import styles from './ProposalVotes.module.css' | ||
|
||
const ProposalVotes = () => { | ||
const { id } = useParams() | ||
|
||
const { chainInfo } = useContext(WalletContext) | ||
|
||
const limit = 5 | ||
|
||
const [offset, setOffset] = useState(0) | ||
|
||
// fetch proposal votes from selected network | ||
const [votes, votesError] = useProposalVotes( | ||
chainInfo, | ||
id.toString(), | ||
limit, | ||
offset, | ||
) | ||
|
||
return ( | ||
<div className={styles.box}> | ||
{votes && votes.length > 0 && ( | ||
<> | ||
{votes.map((vote: any, i: number) => ( | ||
<div key={i} className={styles.boxItem}> | ||
<div className={styles.boxText}> | ||
<h3>{'voter'}</h3> | ||
<p>{vote.voter}</p> | ||
</div> | ||
<div className={styles.boxText}> | ||
<h3>{'option'}</h3> | ||
<p>{vote.options[0].option}</p> | ||
</div> | ||
<div className={styles.boxText}> | ||
<h3>{'metadata'}</h3> | ||
<p>{vote.metadata || 'NA'}</p> | ||
</div> | ||
</div> | ||
))} | ||
{limit && ( | ||
<PaginationNav | ||
length={votes ? votes.length : 0} | ||
limit={limit} | ||
offset={offset} | ||
setOffset={setOffset} | ||
/> | ||
)} | ||
</> | ||
)} | ||
<Result error={votesError} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default ProposalVotes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
const queryProposals = 'cosmos/gov/v1/proposals' | ||
|
||
// fetch proposal votes by proposal id from selected network | ||
export const useProposalVotes = ( | ||
chainInfo: any, | ||
proposalId: string, | ||
limit: number, | ||
offset: number, | ||
) => { | ||
// fetch error and results | ||
const [error, setError] = useState<string | null>(null) | ||
const [votes, setVotes] = useState<any>(null) | ||
|
||
// reset state on param change | ||
useEffect(() => { | ||
setError(null) | ||
setVotes(null) | ||
}, [chainInfo?.rest, proposalId, limit, offset]) | ||
|
||
// fetch on load and param change | ||
useEffect(() => { | ||
// fetch proposal votes from selected network | ||
const fetchProposalVotes = async () => { | ||
const queryParams = `?pagination.limit=${limit}&pagination.offset=${offset}` | ||
|
||
// fetch proposal votes by proposal id from selected network | ||
await fetch( | ||
chainInfo.rest + | ||
'/' + | ||
queryProposals + | ||
'/' + | ||
proposalId + | ||
'/votes' + | ||
queryParams, | ||
) | ||
.then((res) => res.json()) | ||
.then((res) => { | ||
if (res.code) { | ||
setError(res.message) | ||
} else { | ||
setVotes(res.votes) | ||
} | ||
}) | ||
} | ||
|
||
// only fetch if params available | ||
if (chainInfo?.rest && proposalId) { | ||
fetchProposalVotes().catch((err) => { | ||
setError(err.message) | ||
}) | ||
} | ||
}, [chainInfo?.rest, proposalId, limit, offset]) | ||
|
||
return [votes, error] | ||
} |