Skip to content

Commit

Permalink
Merge pull request #125 from makerdao/minimize-db-fetches
Browse files Browse the repository at this point in the history
Only make spock fetches if the data is needed to display the active page
  • Loading branch information
jparklev authored Aug 17, 2019
2 parents b30cb24 + a0c4b48 commit 438ece9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@craco/craco": "^3.5.0",
"@makerdao/dai": "^0.16.1",
"@makerdao/dai-plugin-config": "^0.2.7-rc.3",
"@makerdao/dai-plugin-governance": "^0.7.0",
"@makerdao/dai-plugin-governance": "0.7.2",
"@makerdao/dai-plugin-ledger-web": "^0.9.7",
"@makerdao/dai-plugin-trezor-web": "^0.9.6",
"@makerdao/ui-components": "1.0.0-alpha.29",
Expand Down
12 changes: 8 additions & 4 deletions src/pages/Polling.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { VotingWeightBanner } from './PollingList';
import { activeCanVote, getActiveVotingFor } from '../reducers/accounts';
import { modalOpen } from '../reducers/modal';
import { getWinningProp } from '../reducers/proposals';
import { getOptionVotingFor } from '../reducers/polling';
import { getOptionVotingFor, pollDataInit } from '../reducers/polling';
import theme, { colors } from '../theme';
import { ethScanLink } from '../utils/ethereum';
import { MIN_MKR_PERCENTAGE } from '../utils/constants';
Expand Down Expand Up @@ -340,11 +340,13 @@ class Polling extends React.Component {
this.state.activeAccount !== prevProps.activeAccount ||
(this.props.poll && prevProps.poll === undefined)
) {
await this.updateVotedPollOption();
this.props.pollDataInit(this.props.poll);
this.updateVotedPollOption();
}
}

componentDidMount() {
if (!!this.props.poll) this.props.pollDataInit(this.props.poll);
this.updateVotedPollOption();
}

Expand Down Expand Up @@ -374,7 +376,8 @@ class Polling extends React.Component {
modalOpen,
pollsFetching
} = this.props;
if (pollsFetching && !poll) return null;
if (pollsFetching && !poll)
return <Loader mt={34} mb={34} color="header" background="background" />;
if (isNil(poll) || isEmpty(poll) || !isValidRoute) return <NotFound />;
const {
discussion_link,
Expand Down Expand Up @@ -602,6 +605,7 @@ export default connect(
reduxProps,
{
modalOpen,
getOptionVotingFor
getOptionVotingFor,
pollDataInit
}
)(Polling);
30 changes: 17 additions & 13 deletions src/pages/PollingList.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,8 @@ const Strong = styled(Black)`
font-weight: bold;
`;

export const VotingWeightBanner = ({ fetching, activeAccount }) => {
if (fetching) {
return (
<Padding>
<Loader mt={34} mb={34} color="header" background="background" />
</Padding>
);
}
if (!activeAccount) return <Padding />;
export const VotingWeightBanner = ({ accountsFetching, activeAccount }) => {
if (accountsFetching || !activeAccount) return <Padding />;

// mkr in wallet + mkr locked in chief (including mkr locked via a vote proxy)
const pollVotingPower = add(
Expand All @@ -220,11 +213,12 @@ export const VotingWeightBanner = ({ fetching, activeAccount }) => {
};

const PollingList = ({
fetching,
accountsFetching,
polling: { polls },
activeAccount,
proposals,
approvals
approvals,
pollsFetching
}) => {
polls.sort((a, b) => b.startDate - a.startDate);
const winningProposal = poll => {
Expand All @@ -240,7 +234,15 @@ const PollingList = ({
return (
<Fragment>
<MigrationNotificationBanner />
<VotingWeightBanner fetching={fetching} activeAccount={activeAccount} />
<VotingWeightBanner
accountsFetching={accountsFetching}
activeAccount={activeAccount}
/>
{accountsFetching || pollsFetching ? (
<Padding>
<Loader mt={34} mb={34} color="header" background="background" />
</Padding>
) : null}
<RiseUp key={polls.toString()}>
{polls.map(poll => (
<StyledCard key={poll.pollId}>
Expand Down Expand Up @@ -317,11 +319,13 @@ const PollingList = ({
};

const reduxProps = ({ accounts, polling, approvals, proposals }) => {
const { pollsFetching } = polling;
const activeAccount = accounts.activeAccount
? accounts.allAccounts.find(a => eq(a.address, accounts.activeAccount))
: null;
return {
fetching: accounts.fetching,
pollsFetching,
accountsFetching: accounts.fetching,
polling,
activeAccount,
approvals,
Expand Down
10 changes: 3 additions & 7 deletions src/reducers/polling.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ export const pollsInit = () => async dispatch => {
.service('smartContract')
.getContract('POLLING').address;
dispatch(addPoll(pollData));
dispatch(pollDataInit(pollData));
} catch (e) {
throw e;
}
Expand All @@ -326,12 +325,9 @@ export const pollsInit = () => async dispatch => {
}
};

export const pollDataInit = ({
pollId,
options,
endDate,
active
}) => dispatch => {
export const pollDataInit = poll => dispatch => {
if (!poll) return;
const { pollId, options, endDate, active } = poll;
getTotalVotes(pollId).then(totalVotes =>
dispatch(updatePoll(pollId, { totalVotes }))
);
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1087,10 +1087,10 @@
babel-runtime "^6.26.0"
ramda "^0.26.1"

"@makerdao/dai-plugin-governance@^0.7.0":
version "0.7.0"
resolved "https://registry.yarnpkg.com/@makerdao/dai-plugin-governance/-/dai-plugin-governance-0.7.0.tgz#e838627aa54fa5650894e23b09fbac174be5ea73"
integrity sha512-Fjw3UTgXPqSYxwcC2qSLXT9Y8kKmL9JwKpdh0kCoR3TKyOI7EwSY7rbhBMVLXstKcZ8Z8Xl+J8r2QhnFIOoMlg==
"@makerdao/[email protected].2":
version "0.7.2"
resolved "https://registry.yarnpkg.com/@makerdao/dai-plugin-governance/-/dai-plugin-governance-0.7.2.tgz#b84fb0a35eace3e29f2795bbf7ebf44f656314b9"
integrity sha512-foNjsTbPhpReYnnuGWXafdfcKIAMgBq2GZeSG+70gJ6mFK++Ez1D4oaSMhMtXZqHcfUHFiduYIY+aD0vKipk3g==
dependencies:
"@babel/runtime" "^7.4.3"
"@makerdao/currency" "^0.9.5"
Expand Down

1 comment on commit 438ece9

@vercel
Copy link

@vercel vercel bot commented on 438ece9 Aug 17, 2019

Choose a reason for hiding this comment

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

Please sign in to comment.