Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Voting: use aragonAPI for React + React hooks #717

Merged
merged 38 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b3e8913
Use label prop on ButtonIcon elements
bpierre Mar 15, 2019
9f2f822
Voting: use hooks + @aragon/api-react
bpierre Mar 18, 2019
cf11c57
Remove useless .pipe( first() ) calls
bpierre Mar 19, 2019
d82227f
Merge branch 'master' into voting-react-api
bpierre Mar 20, 2019
7ce65a3
Fix infinite calls when opening a vote
bpierre Mar 20, 2019
3d72b24
Use the new @aragon/api-react API
bpierre Mar 21, 2019
42b2ea0
Optimize rendering
bpierre Mar 21, 2019
58d35b7
@aragon/api-react API change
bpierre Mar 21, 2019
3bfb5ab
Merge branch 'master' into voting-react-api
bpierre Mar 21, 2019
5f9855f
Merge master into voting-react-api
bpierre Apr 3, 2019
c03bffe
shorthand properties first
bpierre Apr 3, 2019
49b0a20
useNow(): update if updateEvery changes
bpierre Apr 3, 2019
49cdbbd
VotePanelContent: make use of useCurrentVoteData()
bpierre Apr 3, 2019
99b9672
Make VotePanelContent a function component
bpierre Apr 4, 2019
d86b477
styled-component css transform doesn’t like attached components
bpierre Apr 4, 2019
e361906
Use displayMenuButton
bpierre Apr 4, 2019
1481296
Use IconMenu from @aragon/ui
bpierre Apr 5, 2019
111952f
ESLint fixes
bpierre Apr 5, 2019
5c9b40c
Refactor App.js to move the logic into custom hooks
bpierre Apr 8, 2019
58d76e8
Make IdentityManager.js a JS module
bpierre Apr 8, 2019
7324e55
LocalIdentityBadge: consume useNetwork() directly
bpierre Apr 8, 2019
5679af8
Add the React Hooks linter plugin
bpierre Apr 8, 2019
bff5a55
Add a comment about why we keep the selected vote until the panel get…
bpierre Apr 8, 2019
61df0bf
Render identity badges
bpierre Apr 9, 2019
a5bae27
Merge branch 'master' into voting-react-api
bpierre Apr 9, 2019
07baf36
Stop exporting SettingsContext
bpierre Apr 9, 2019
b6e08b1
useVotingApp() => useAppLogic()
bpierre Apr 9, 2019
d32c6ad
open() / close() => requestOpen() / requestClose()
bpierre Apr 10, 2019
bd0f415
Group aragonAPI hooks using useAragonApi()
bpierre Apr 10, 2019
940647c
User balance: use -1 for unknown balances
bpierre Apr 10, 2019
54f6f2a
Identity: remove references to a modal
bpierre Apr 10, 2019
2af4569
getUserBalance(): check if vote exists
bpierre Apr 10, 2019
7f181a9
Refactoring: voting text rendering
bpierre Apr 10, 2019
641137e
Skip useless VotingCard renderings
bpierre Apr 10, 2019
3645dc5
Remove console.log() calls
bpierre Apr 10, 2019
c09b088
ESLint fixes
bpierre Apr 10, 2019
98b22b4
fix: use new API for usePanelState()
sohkai Apr 10, 2019
c408c21
VotingCard: remove unnecessary default prop
sohkai Apr 10, 2019
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: 0 additions & 1 deletion apps/voting/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"react-linkify": "^0.2.2",
"react-spring": "^5.7.2",
"rxjs": "^6.2.1",
"rxjs-compat": "^6.2.1",
"seed-random": "^2.2.0",
"styled-components": "4.1.3"
},
Expand Down
12 changes: 5 additions & 7 deletions apps/voting/app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,11 @@ function App() {

let cancelled = false
Promise.all(
votes.map(
vote =>
new Promise((resolve, reject) => {
api
.call('getVoterState', vote.voteId, userAccount)
.subscribe(result => resolve([vote.voteId, result]), reject)
})
votes.map(vote =>
api
.call('getVoterState', vote.voteId, userAccount)
.toPromise()
.then(result => [vote.voteId, result])
)
).then(voteStates => {
if (!cancelled) {
Expand Down
2 changes: 1 addition & 1 deletion apps/voting/app/src/components/VotePanelContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class VotePanelContent extends React.Component {

this.setState({ loadingCanExecute: true })
const canExecute = await api.call('canExecute', vote.voteId).toPromise()
this.setState({ canExecute, loadingCanExecute: false })
this.setState({ loadingCanExecute: false, canExecute })
bpierre marked this conversation as resolved.
Show resolved Hide resolved
}
renderBlockLink = snapshotBlock => {
const {
Expand Down
53 changes: 22 additions & 31 deletions apps/voting/app/src/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Aragon from '@aragon/api'
import { of } from 'rxjs'
import { map } from 'rxjs/operators'
import voteSettings, { hasLoadedVoteSettings } from './vote-settings'
import { EMPTY_CALLSCRIPT } from './evmscript-utils'
import tokenDecimalsAbi from './abi/token-decimals.json'
Expand Down Expand Up @@ -42,13 +41,17 @@ const retryEvery = (callback, initialRetryTimer = 1000, increaseFactor = 5) => {

// Get the token address to initialize ourselves
retryEvery(retry => {
app.call('token').subscribe(initialize, err => {
console.error(
'Could not start background script execution due to the contract not loading the token:',
err
)
retry()
})
app
.call('token')
.toPromise()
.then(initialize)
.catch(err => {
console.error(
'Could not start background script execution due to the contract not loading the token:',
err
)
retry()
})
})

async function initialize(tokenAddr) {
Expand All @@ -73,7 +76,7 @@ async function initialize(tokenAddr) {

let tokenDecimals
try {
tokenDecimals = await token.decimals().toPromise()
tokenDecimals = (await token.decimals().toPromise()) || '0'
} catch (err) {
console.err(
`Failed to load token decimals for token at ${tokenAddr} due to:`,
Expand Down Expand Up @@ -193,11 +196,10 @@ async function loadVoteDescription(vote) {
}

function loadVoteData(voteId) {
return new Promise(resolve => {
app
.call('getVote', voteId)
.subscribe(vote => resolve(loadVoteDescription(marshallVote(vote))))
})
return app
.call('getVote', voteId)
.toPromise()
.then(vote => loadVoteDescription(marshallVote(vote)))
}

async function updateVotes(votes, voteId, transform) {
Expand Down Expand Up @@ -229,23 +231,12 @@ async function updateState(state, voteId, transform) {

function loadVoteSettings() {
return Promise.all(
voteSettings.map(
([name, key, type = 'string']) =>
new Promise((resolve, reject) =>
app
.call(name)
.pipe(
map(val => {
if (type === 'time') {
return marshallDate(val)
}
return val
})
)
.subscribe(value => {
resolve({ [key]: value })
}, reject)
)
voteSettings.map(([name, key, type = 'string']) =>
app
.call(name)
.toPromise()
.then(val => (type === 'time' ? marshallDate(val) : val))
.then(value => ({ [key]: value }))
)
)
.then(settings =>
Expand Down
41 changes: 18 additions & 23 deletions apps/voting/app/src/vote-hooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect } from 'react'

// when `params` are updated, call `fn` and pass the result
function usePromiseResult(fn, params, defaultValue) {
function usePromiseResult(fn, params, memoParams, defaultValue) {
const [result, setResult] = useState(defaultValue)
useEffect(() => {
let cancelled = false
Expand All @@ -13,7 +13,7 @@ function usePromiseResult(fn, params, defaultValue) {
return () => {
bpierre marked this conversation as resolved.
Show resolved Hide resolved
cancelled = true
}
}, params)
}, memoParams)
return result
}

Expand Down Expand Up @@ -49,15 +49,6 @@ async function getCanExecute(vote, api) {
return api.call('canExecute', vote.voteId).toPromise()
}

export const useUserBalance = (...params) =>
usePromiseResult(getUserBalance, params, -1)

export const useCanVote = (...params) =>
usePromiseResult(getCanVote, params, false)

export const useCanExecute = (...params) =>
usePromiseResult(getCanVote, params, false)

export const useCurrentVoteData = (
vote,
userAccount,
Expand All @@ -66,19 +57,23 @@ export const useCurrentVoteData = (
tokenDecimals
) => {
bpierre marked this conversation as resolved.
Show resolved Hide resolved
return {
bpierre marked this conversation as resolved.
Show resolved Hide resolved
canUserVote: useCanVote(
vote,
userAccount,
api,
tokenContract,
tokenDecimals
canUserVote: usePromiseResult(
getCanVote,
[vote, userAccount, api],
[vote && vote.voteId, userAccount, api],
false
),
canExecute: usePromiseResult(
getCanExecute,
[vote, api],
[vote && vote.voteId, api],
false
),
canExecute: useCanExecute(vote, api),
userBalance: useUserBalance(
vote,
userAccount,
tokenContract,
tokenDecimals
userBalance: usePromiseResult(
getUserBalance,
[vote, userAccount, tokenContract, tokenDecimals],
[vote && vote.voteId, userAccount, tokenContract, tokenDecimals],
-1
),
}
}