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: app paths #1029

Merged
merged 5 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions apps/voting/app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ const App = React.memo(function App() {
selectVote,
votes,
} = useAppLogic()

const { layoutName } = useLayout()
const compactMode = layoutName === 'small'
const handleBack = useCallback(() => selectVote(-1), [selectVote])

const {
filteredVotes,
voteStatusFilter,
Expand Down
39 changes: 25 additions & 14 deletions apps/voting/app/src/app-logic.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
import React, { useCallback, useMemo, useState } from 'react'
import { AragonApi, useApi, useAppState } from '@aragon/api-react'
import { AragonApi, useApi, useAppState, usePath } from '@aragon/api-react'
import appStateReducer from './app-state-reducer'
import { EMPTY_CALLSCRIPT } from './evmscript-utils'
import usePanelState from './hooks/usePanelState'
import useVotes from './hooks/useVotes'
import { noop } from './utils'
import { VOTE_YEA } from './vote-types'

const VOTE_ID_PATH_RE = /^\/vote\/([0-9]+)\/?$/
const NO_VOTE_ID = '-1'

function voteIdFromPath(path) {
const matches = path.match(VOTE_ID_PATH_RE)
return matches ? matches[1] : NO_VOTE_ID
}

// Get the vote currently selected, or null otherwise.
export function useSelectedVote(votes) {
const [selectedVoteId, setSelectedVoteId] = useState('-1')
const [path, requestPath] = usePath()
const { ready } = useAppState()

// The memoized vote currently selected.
const selectedVote = useMemo(() => {
// The `ready` check prevents a vote to be selected
// until the app state is fully ready.
if (!ready || selectedVoteId === '-1') {
const voteId = path ? voteIdFromPath(path) : NO_VOTE_ID
Copy link
Contributor

Choose a reason for hiding this comment

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

Thoughts on rolling up the path check directly into voteIdFromPath()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That’s better yes!


// The `ready` check prevents a vote to be
// selected until the app state is fully ready.
if (!ready || voteId === NO_VOTE_ID) {
return null
}
return votes.find(vote => vote.voteId === selectedVoteId) || null
}, [selectedVoteId, votes, ready])

return [
selectedVote,
return votes.find(vote => vote.voteId === voteId) || null
}, [path, ready, votes])

const selectVote = useCallback(
voteId => {
requestPath(String(voteId) === NO_VOTE_ID ? '' : `/vote/${voteId}/`)
},
[requestPath]
)

// setSelectedVoteId() is exported directly: since `selectedVoteId` is
// set in the `selectedVote` dependencies, it means that the useMemo()
// will be updated every time `selectedVoteId` changes.
setSelectedVoteId,
]
return [selectedVote, selectVote]
}

// Create a new vote
Expand Down