Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

feat: add remote sorting to delegate table #1080

Merged
merged 3 commits into from Feb 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions __tests__/unit/store/modules/delegate.spec.js
Expand Up @@ -106,7 +106,7 @@ describe('delegate store module', () => {
})

axiosMock
.onGet(`http://127.0.0.1/api/delegates`, { params: { offset: 0, limit: 51 } })
.onGet(`http://127.0.0.1/api/delegates`, { params: { offset: 0, limit: 51, orderBy: 'rank:asc' } })
.reply(200, {
totalCount: 2,
delegates: v1DelegateData
Expand All @@ -118,7 +118,7 @@ describe('delegate store module', () => {
ClientService.version = 2

axiosMock
.onGet(`http://127.0.0.1/api/delegates`, { params: { page: 1, limit: 100 } })
.onGet(`http://127.0.0.1/api/delegates`, { params: { page: 1, limit: 100, orderBy: 'rank:asc' } })
.reply(200, {
data: v2DelegateData,
meta: {
Expand Down
Expand Up @@ -83,7 +83,6 @@
import { ButtonClose } from '@/components/Button'
import TransactionModal from '@/components/Transaction/TransactionModal'
import TableWrapper from '@/components/utils/TableWrapper'
import { orderBy } from 'lodash'

export default {
name: 'WalletDelegates',
Expand Down Expand Up @@ -142,9 +141,11 @@ export default {
}
]
},

isExplanationDisplayed () {
return this.$store.getters['app/showVotingExplanation']
},

votingUrl () {
return 'https://docs.ark.io/tutorials/usage-guides/how-to-vote-in-the-ark-desktop-wallet.html'
}
Expand All @@ -168,15 +169,21 @@ export default {
},

async fetchDelegates () {
if (this.isLoading) return
if (this.isLoading) {
return
}

try {
this.isLoading = true

const { limit, page, sort } = this.queryParams
const { delegates, totalCount } = await this.$client.fetchDelegates({
page: this.queryParams.page,
limit: this.queryParams.limit
page,
limit,
orderBy: `${sort.field.replace('production.', '')}:${sort.type}`
})
this.delegates = this.__sortDelegates(delegates)

this.delegates = delegates
this.totalCount = totalCount
} catch (error) {
this.$logger.error(error)
Expand Down Expand Up @@ -226,8 +233,8 @@ export default {
const sortType = sortOptions[0].type
this.__updateParams({
sort: {
type: sortType,
field: columnName
field: columnName,
type: sortType
},
page: 1
})
Expand All @@ -241,10 +248,6 @@ export default {
this.delegates = []
},

__sortDelegates (delegates = this.delegates) {
return orderBy(delegates, [this.queryParams.sort.field], [this.queryParams.sort.type])
},

__updateParams (newProps) {
this.queryParams = Object.assign({}, this.queryParams, newProps)
}
Expand Down
Expand Up @@ -270,10 +270,13 @@ export default {
},

onSortChange (sortOptions) {
const columnName = sortOptions[0].field
const sortType = sortOptions[0].type

this.__updateParams({
sort: {
type: sortOptions[0].type,
field: sortOptions[0].field
field: columnName,
type: sortType
},
page: 1
})
Expand Down
19 changes: 13 additions & 6 deletions src/renderer/services/client.js
Expand Up @@ -137,20 +137,23 @@ export default class ClientService {
* @param {Object} [query]
* @param {Number} [query.page=1]
* @param {Number} [query.limit=51]
* @param {String} [query.orderBy='rank:asc']
* @return {Object[]}
*/
async fetchDelegates ({ page, limit } = {}) {
async fetchDelegates (options = {}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@dated , I'm curious, why do you prefer using options instead of destructuring the object here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just for conformity in regards to the other methods, such as fetchWalletTransactions.

const network = store.getters['session/network']
page || (page = 1)
limit || (limit = network.constants.activeDelegates)
options.page || (options.page = 1)
options.limit || (options.limit = network.constants.activeDelegates)
options.orderBy || (options.orderBy = 'rank:asc')

let totalCount = 0
let delegates = []

if (this.__version === 1) {
const { data } = await this.client.resource('delegates').all({
offset: (page - 1) * limit,
limit
offset: (options.page - 1) * options.limit,
limit: options.limit,
orderBy: options.orderBy
})

delegates = data.delegates.map(delegate => {
Expand All @@ -170,7 +173,11 @@ export default class ClientService {

totalCount = parseInt(data.totalCount)
} else {
const { data } = await this.client.resource('delegates').all({ page, limit })
const { data } = await this.client.resource('delegates').all({
page: options.page,
limit: options.limit,
orderBy: options.orderBy
})
delegates = data.data
totalCount = data.meta.totalCount
}
Expand Down