Skip to content

Commit

Permalink
Update proposals every new block (#3295)
Browse files Browse the repository at this point in the history
* Update proposals every 10 blocks

* changelog

* istanbul ignore next

* Trigger CI

* istanbul ignore next

* Use loaded

* Update snap

* istanbul ignore next

* Update single proposal page every new block

* Job is done!

* Comment

* Remove comment

* istanbul ignore next

* Improve coverage

* Update all proposal data

* Simplify

* Revert

* Fix watcher func testing

* Fix, cleanup, optimization

* Update snap

* Cleanup
  • Loading branch information
mariopino authored and faboweb committed Dec 17, 2019
1 parent d46c128 commit c8ab4eb
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 23 deletions.
1 change: 1 addition & 0 deletions changes/mario_3277-proposals-page-never-refreshes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Fixed] [#3277](https://github.com/cosmos/lunie/issues/3277) Update proposals every 10 blocks @mariopino
58 changes: 50 additions & 8 deletions src/components/governance/PageProposal.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<template>
<TmPage data-title="Proposal" hide-header class="small">
<TmDataLoading
v-if="
$apollo.queries.proposals.loading ||
$apollo.queries.proposal.loading ||
$apollo.queries.parameters.loading
"
/>
<TmDataLoading v-if="$apollo.loading && !loaded" />
<TmDataNotFound v-else-if="!found" />
<TmDataError v-else-if="error" />
<template v-else>
Expand Down Expand Up @@ -202,6 +196,7 @@ import { ProposalItem, GovernanceParameters, Vote } from "src/gql"
import BigNumber from "bignumber.js"
import Bech32 from "common/Bech32"
import gql from "graphql-tag"
import refetchNetworkOnly from "scripts/refetch-network-only"
export default {
name: `page-proposal`,
Expand Down Expand Up @@ -243,7 +238,8 @@ export default {
depositDenom: "TESTCOIN"
},
error: undefined,
found: false
found: false,
loaded: false
}),
computed: {
...mapGetters([`address`, `network`]),
Expand All @@ -263,6 +259,12 @@ export default {
return id
}
},
watch: {
// Needed to show data loading component when you are browsing from one proposal to another
$route: function() {
this.loaded = false
}
},
methods: {
onVote() {
this.$refs.modalVote.open()
Expand Down Expand Up @@ -319,6 +321,7 @@ export default {
) {
this.found = true
}
/* istanbul ignore next */
return data.proposals
}
},
Expand All @@ -328,6 +331,8 @@ export default {
return ProposalItem(this.network)
},
update(data) {
/* istanbul ignore next */
this.loaded = true
/* istanbul ignore next */
return data.proposal
},
Expand Down Expand Up @@ -388,6 +393,43 @@ export default {
/* istanbul ignore next */
this.error = data.error
}
},
$subscribe: {
blockAdded: {
variables() {
/* istanbul ignore next */
return {
networkId: this.network
}
},
query() {
/* istanbul ignore next */
return gql`
subscription($networkId: String!) {
blockAdded(networkId: $networkId) {
height
}
}
`
},
skip() {
/* istanbul ignore next */
return !this.found
},
result() {
/* istanbul ignore next */
if (
// Don't update passed or rejected proposals
this.proposal.status !== "Passed" &&
this.proposal.status !== "Rejected" &&
this.loaded
) {
refetchNetworkOnly(this.$apollo.queries.proposal)
refetchNetworkOnly(this.$apollo.queries.parameters)
refetchNetworkOnly(this.$apollo.queries.vote)
}
}
}
}
}
}
Expand Down
46 changes: 38 additions & 8 deletions src/components/governance/PageProposals.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<template>
<TmPage data-title="Proposals" :managed="false" hide-header>
<TmPage
data-title="Proposals"
:managed="true"
hide-header
:loading="$apollo.loading && !loaded"
>
<div class="button-container">
<TmBtn
id="propose-btn"
Expand All @@ -26,10 +31,7 @@
</TmDataMsg>
</div>
</div>
<div v-else>
<TmDataLoading v-if="$apollo.loading" />
<TableProposals v-else :proposals="proposals" />
</div>
<TableProposals v-else :proposals="proposals" />
</TmPage>
</template>

Expand All @@ -39,26 +41,26 @@ import TableProposals from "governance/TableProposals"
import TmPage from "common/TmPage"
import TmBtn from "common/TmBtn"
import TmDataMsg from "common/TmDataMsg"
import TmDataLoading from "common/TmDataLoading"
import { mapGetters } from "vuex"
import { GovernanceParameters } from "src/gql"
import gql from "graphql-tag"
import refetchNetworkOnly from "scripts/refetch-network-only"
export default {
name: `page-proposals`,
components: {
ModalPropose,
TableProposals,
TmDataMsg,
TmDataLoading,
TmBtn,
TmPage
},
data: () => ({
proposals: [],
parameters: {
depositDenom: "xxx"
}
},
loaded: false
}),
computed: {
...mapGetters([`network`])
Expand Down Expand Up @@ -89,11 +91,14 @@ export default {
`
},
variables() {
/* istanbul ignore next */
return {
networkId: this.network
}
},
update(data) {
/* istanbul ignore next */
this.loaded = true
/* istanbul ignore next */
return data.proposals
}
Expand All @@ -107,10 +112,35 @@ export default {
/* istanbul ignore next */
return data.governanceParameters
}
},
$subscribe: {
blockAdded: {
variables() {
/* istanbul ignore next */
return {
networkId: this.network
}
},
query() {
/* istanbul ignore next */
return gql`
subscription($networkId: String!) {
blockAdded(networkId: $networkId) {
height
}
}
`
},
result() {
/* istanbul ignore next */
refetchNetworkOnly(this.$apollo.queries.proposals)
}
}
}
}
}
</script>

<style scoped>
.button-container {
display: flex;
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/specs/components/governance/PageProposal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe(`PageProposal`, () => {
getters,
state
}

const $apollo = {
queries: {
proposals: {
Expand Down Expand Up @@ -119,6 +120,13 @@ describe(`PageProposal`, () => {
expect(wrapper.vm.getProposalIndex(1)).toEqual(32)
})

it(`should set loaded to false on route change`, () => {
wrapper.vm.loaded = true
// Call directly watcher function
wrapper.vm.$options.watch.$route.call(wrapper.vm, { path: "xxxx" })
expect(wrapper.vm.loaded).toBe(false)
})

describe(`Proposal status`, () => {
it(`displays correctly a proposal that 'Passed'`, () => {
wrapper.vm.proposal.status = `Passed`
Expand Down
Loading

0 comments on commit c8ab4eb

Please sign in to comment.