Skip to content

Commit b635c0d

Browse files
Merge pull request #5850 from vuestorefront/#4599-fix/order-history-pagination
fix: Order history pagination
2 parents c1d8cca + 823a8e2 commit b635c0d

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4343
- Incorrect load of default address in checkout - @lukaszjedrasik ([#4682](https://github.com/vuestorefront/vue-storefront/issues/4682))
4444
- Error with unknown theme/index.js alias - @Fifciuu (https://github.com/vuestorefront/vue-storefront/pull/5813)
4545
- ESLint warnings caused by the double import - @lukaszjedrasik
46+
- Fix Order History Pagination - @AishwaryShrivastav / @lukaszjedrasik ([#4599](https://github.com/vuestorefront/vue-storefront/issues/4599))
47+
4648
### Changed / Improved
4749

4850
- Moved hardcoded fields from omitSelectedVariantFields.ts to config (#4679)

core/modules/order/components/UserOrdersHistory.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ export default {
1818
ordersHistory () {
1919
let items = this.getOrdersHistory
2020
if (this.lazyLoadOrdersOnScroll) {
21-
items = items.slice(0, (this.pagination.perPage + 1) * this.pagination.current)
21+
items = items.slice(0, this.pagination.perPage * this.pagination.current)
2222
}
2323
return items
2424
}
2525
},
2626
methods: {
2727
onBottomScroll () {
28-
++this.pagination.current
28+
const totalCount = this.$store.state.user.orders_history.total_count ? this.$store.state.user.orders_history.total_count : 0;
29+
const isLastPage = this.pagination.current > Math.ceil(totalCount / this.pagination.perPage);
30+
if (!isLastPage) {
31+
this.pagination.current++;
32+
this.$store.dispatch('user/appendOrdersHistory', {
33+
pageSize: this.pagination.perPage,
34+
currentPage: this.pagination.current
35+
});
36+
}
2937
}
3038
}
3139
}

core/modules/user/store/actions.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { StorageManager } from '@vue-storefront/core/lib/storage-manager'
1313
import { userHooksExecutors, userHooks } from '../hooks'
1414
import { isModuleRegistered } from '@vue-storefront/core/lib/modules'
1515
import Task from '@vue-storefront/core/lib/sync/types/Task'
16+
import uniqBy from 'lodash-es/uniqBy'
1617

1718
const actions: ActionTree<UserState, RootState> = {
1819
async startSession ({ commit, dispatch, getters }) {
@@ -269,6 +270,19 @@ const actions: ActionTree<UserState, RootState> = {
269270
return ordersHistory
270271
}
271272
},
273+
async appendOrdersHistory ({ commit, getters }, { pageSize = 20, currentPage = 1 }) {
274+
const resp = await UserService.getOrdersHistory(pageSize, currentPage)
275+
276+
if (resp.code === 200) {
277+
const oldOrders = getters.getOrdersHistory;
278+
let orders = resp.result;
279+
if (oldOrders && orders.items) orders.items = uniqBy([...oldOrders, ...orders.items], 'increment_id')
280+
281+
commit(types.USER_ORDERS_HISTORY_LOADED, orders)
282+
EventBus.$emit('user-after-loaded-orders', orders)
283+
return orders
284+
}
285+
},
272286
async refreshOrdersHistory ({ commit }, { resolvedFromCache, pageSize = 20, currentPage = 1 }) {
273287
const resp = await UserService.getOrdersHistory(pageSize, currentPage)
274288

core/modules/user/test/unit/store/actions.spec.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,73 @@ describe('User actions', () => {
456456
})
457457
});
458458

459+
describe('appendOrdersHistory action', () => {
460+
it('should append order to orders history', async () => {
461+
const responseOb = {
462+
result: data.ordersHistory,
463+
code: 200
464+
};
465+
(UserService.getOrdersHistory as jest.Mock).mockImplementation(async () => responseOb);
466+
const contextMock = {
467+
commit: jest.fn(),
468+
getters: {
469+
getOrdersHistory: responseOb.result
470+
}
471+
};
472+
const pageSize = data.pageSize;
473+
const currentPage = data.currentPage;
474+
475+
const result = await (userActions as any).appendOrdersHistory(contextMock, {
476+
pageSize,
477+
currentPage
478+
})
479+
480+
expect(contextMock.commit).toBeCalledWith(types.USER_ORDERS_HISTORY_LOADED, responseOb.result);
481+
expect(EventBus.$emit).toBeCalledWith('user-after-loaded-orders', result);
482+
expect(result).toBe(responseOb.result)
483+
})
484+
485+
it('returns orders with unique increment_id', async () => {
486+
const oldOrders = [
487+
{ name: 'a', increment_id: 0 },
488+
{ name: 'b', increment_id: 1 }
489+
]
490+
const orders = {
491+
items: [
492+
{ name: 'a', increment_id: 0 },
493+
{ name: 'c', increment_id: 2 }
494+
]
495+
}
496+
const responseOb = {
497+
result: orders,
498+
code: 200
499+
};
500+
const contextMock = {
501+
commit: jest.fn(),
502+
getters: {
503+
getOrdersHistory: oldOrders
504+
}
505+
};
506+
const pageSize = data.pageSize;
507+
const currentPage = data.currentPage;
508+
509+
(UserService.getOrdersHistory as jest.Mock).mockImplementation(async () => responseOb);
510+
const result = await (userActions as any).appendOrdersHistory(contextMock, {
511+
pageSize,
512+
currentPage
513+
})
514+
const expectedResult = {
515+
items: [
516+
{ name: 'a', increment_id: 0 },
517+
{ name: 'b', increment_id: 1 },
518+
{ name: 'c', increment_id: 2 }
519+
]
520+
}
521+
522+
expect(result).toEqual(expectedResult);
523+
})
524+
})
525+
459526
describe('refreshOrderHistory action', () => {
460527
it('should refresh orders history', async () => {
461528
const responseOb = {

0 commit comments

Comments
 (0)