-
Notifications
You must be signed in to change notification settings - Fork 41
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
feat: added Pagination for Wishlist #893
base: main
Are you sure you want to change the base?
Changes from all commits
a1a076c
abe3b88
e9a612b
65bb409
0ea15fa
d7ff926
de6dede
7ecc7ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"vue-demo-store": minor | ||
--- | ||
|
||
Added a pagination for the order history page. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"vue-demo-store": minor | ||
"@shopware-pwa/composables-next": minor | ||
--- | ||
|
||
Added a pagination for the wishlist page. I made changes to the variable name to get the proper context of it. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,5 @@ | ||||||
import { ref } from "vue"; | ||||||
import type { Ref } from "vue"; | ||||||
import { ref, computed } from "vue"; | ||||||
import type { Ref, ComputedRef } from "vue"; | ||||||
import { useShopwareContext } from "#imports"; | ||||||
import type { Schemas } from "#shopware"; | ||||||
|
||||||
|
@@ -13,11 +13,22 @@ export type UseCustomerOrdersReturn = { | |||||
* | ||||||
* In order to change a page with additional parameters please use `loadOrders` method. | ||||||
*/ | ||||||
changeCurrentPage(pageNumber: number | string): Promise<void>; | ||||||
changeCurrentPage( | ||||||
pageNumber: number | string, | ||||||
limit: number | string, | ||||||
): Promise<void>; | ||||||
/** | ||||||
* Fetches the orders list and assigns the result to the `orders` property | ||||||
*/ | ||||||
loadOrders(parameters?: Schemas["Criteria"]): Promise<void>; | ||||||
/** | ||||||
* Current page number | ||||||
*/ | ||||||
getCurrentPage: ComputedRef<number>; | ||||||
/** | ||||||
* total pages count | ||||||
*/ | ||||||
getTotalPagesCount: ComputedRef<number>; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here
Suggested change
|
||||||
}; | ||||||
|
||||||
/** | ||||||
|
@@ -30,21 +41,41 @@ export function useCustomerOrders(): UseCustomerOrdersReturn { | |||||
|
||||||
const orders: Ref<Schemas["Order"][]> = ref([]); | ||||||
|
||||||
const currentPage = ref<number>(1); | ||||||
|
||||||
const totalOrderItemsCount: Ref<number> = ref(0); | ||||||
|
||||||
const limit = ref<number>(15); | ||||||
|
||||||
const loadOrders = async ( | ||||||
parameters: Schemas["Criteria"] = {}, | ||||||
): Promise<void> => { | ||||||
const fetchedOrders = await apiClient.invoke("readOrder post /order", { | ||||||
body: parameters, | ||||||
}); | ||||||
orders.value = fetchedOrders.data.orders.elements; | ||||||
totalOrderItemsCount.value = fetchedOrders.data.orders.total!; | ||||||
}; | ||||||
|
||||||
const changeCurrentPage = async (pageNumber: number | string) => | ||||||
await loadOrders({ page: +pageNumber }); | ||||||
const changeCurrentPage = async ( | ||||||
pageNumber: number | string, | ||||||
currentLimit: string | number, | ||||||
) => { | ||||||
limit.value = +currentLimit; | ||||||
await loadOrders({ page: +pageNumber, limit: +currentLimit }); | ||||||
}; | ||||||
|
||||||
const getCurrentPage = computed(() => currentPage.value); | ||||||
|
||||||
const getTotalPagesCount = computed(() => | ||||||
Math.ceil(totalOrderItemsCount.value / +limit.value), | ||||||
); | ||||||
|
||||||
return { | ||||||
orders, | ||||||
changeCurrentPage, | ||||||
loadOrders, | ||||||
getCurrentPage, | ||||||
getTotalPagesCount, | ||||||
}; | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,13 @@ import { ref, computed } from "vue"; | |
import type { Ref, ComputedRef } from "vue"; | ||
import { useShopwareContext } from "#imports"; | ||
import { ApiClientError } from "@shopware/api-client"; | ||
import type { Schemas } from "#shopware"; | ||
|
||
export type UseSyncWishlistReturn = { | ||
/** | ||
* Get products from wishlist | ||
*/ | ||
getWishlistProducts(): void; | ||
getWishlistProducts(defaultSearchCriteria?: Schemas["Criteria"]): void; | ||
/** | ||
* Merge products with wishlist already existing in API wishlist | ||
*/ | ||
|
@@ -27,10 +28,11 @@ export type UseSyncWishlistReturn = { | |
/** | ||
* Wishlist items count | ||
*/ | ||
count: ComputedRef<number>; | ||
totalWishlistItemsCount: Ref<number>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm afraid that is an unnecessary breaking change; why do you feel the need to change it? Also it should be a computed property |
||
}; | ||
|
||
const _wishlistItems: Ref<string[]> = ref([]); | ||
const totalWishlistItemsCount: Ref<number> = ref(0); | ||
|
||
/** | ||
* Composable to manage wishlist via API | ||
|
@@ -39,7 +41,6 @@ const _wishlistItems: Ref<string[]> = ref([]); | |
*/ | ||
export function useSyncWishlist(): UseSyncWishlistReturn { | ||
const { apiClient } = useShopwareContext(); | ||
|
||
async function addToWishlistSync(id: string) { | ||
await apiClient.invoke( | ||
"addProductOnWishlist post /customer/wishlist/add/{productId}", | ||
|
@@ -62,14 +63,18 @@ export function useSyncWishlist(): UseSyncWishlistReturn { | |
* Fetch wishlist items | ||
* Only for logged-in users | ||
*/ | ||
async function getWishlistProducts() { | ||
async function getWishlistProducts( | ||
defaultSearchCriteria?: Schemas["Criteria"], | ||
) { | ||
try { | ||
const response = await apiClient.invoke( | ||
"readCustomerWishlist post /customer/wishlist", | ||
{ body: defaultSearchCriteria }, | ||
); | ||
_wishlistItems.value = [ | ||
...response.data.products.elements.map((element) => element.id), | ||
]; | ||
totalWishlistItemsCount.value = response.data.products.total!; | ||
} catch (e) { | ||
if (e instanceof ApiClientError) { | ||
// If 404 ignore printing error and reset wishlist | ||
|
@@ -89,14 +94,13 @@ export function useSyncWishlist(): UseSyncWishlistReturn { | |
} | ||
|
||
const items = computed(() => _wishlistItems.value); | ||
const count = computed(() => items.value.length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Count could just show |
||
|
||
return { | ||
getWishlistProducts, | ||
addToWishlistSync, | ||
removeFromWishlistSync, | ||
mergeWishlistProducts, | ||
items, | ||
count, | ||
totalWishlistItemsCount, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,37 +1,159 @@ | ||||||
import { describe, expect, it, vi } from "vitest"; | ||||||
import { useWishlist } from "./useWishlist"; | ||||||
import { useSyncWishlist } from "./useSyncWishlist"; | ||||||
import { useUser } from "./useUser"; | ||||||
import { useSetup } from "./_test"; | ||||||
import { computed, ref } from "vue"; | ||||||
import type { Schemas } from "#shopware"; | ||||||
|
||||||
vi.mock("./useLocalWishlist.ts", () => ({ | ||||||
useLocalWishlist() { | ||||||
return { | ||||||
getWishlistProducts: () => undefined, | ||||||
items: { | ||||||
value: [ | ||||||
"local-testId-1", | ||||||
"local-testId-2", | ||||||
"local-testId-3", | ||||||
"local-testId-4", | ||||||
"local-testId-5", | ||||||
"local-testId-6", | ||||||
"local-testId-7", | ||||||
"local-testId-8", | ||||||
"local-testId-9", | ||||||
"local-testId-10", | ||||||
"local-testId-11", | ||||||
"local-testId-12", | ||||||
"local-testId-13", | ||||||
"local-testId-14", | ||||||
"local-testId-15", | ||||||
"local-testId-16", | ||||||
"local-testId-17", | ||||||
], | ||||||
}, | ||||||
clearWishlist: () => undefined, | ||||||
totalWishlistItemsCount: ref(17), | ||||||
}; | ||||||
}, | ||||||
})); | ||||||
|
||||||
vi.mock("./useSyncWishlist.ts"); | ||||||
vi.mock("./useUser"); | ||||||
|
||||||
describe("useWishlist - not logged in user", () => { | ||||||
vi.mock("./useLocalWishlist.ts", () => ({ | ||||||
useLocalWishlist() { | ||||||
return { | ||||||
getWishlistProducts: () => undefined, | ||||||
items: { value: ["local-testId333"] }, | ||||||
clearWishlist: () => undefined, | ||||||
}; | ||||||
}, | ||||||
})); | ||||||
|
||||||
it("mergeWishlistProducts", async () => { | ||||||
it("mergeWishlistProducts", () => { | ||||||
vi.mocked(useUser).mockReturnValue({ | ||||||
isLoggedIn: computed(() => false), | ||||||
isGuestSession: computed(() => true), | ||||||
// @ts-ignore | ||||||
canSyncWishlist: { value: false }, | ||||||
Comment on lines
+48
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ts-ignore is not a solution, useUser doesn't have
Suggested change
|
||||||
}); | ||||||
vi.mocked(useSyncWishlist).mockReturnValue({ | ||||||
getWishlistProducts: () => undefined, | ||||||
// @ts-ignore | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same - no ts-ignore, we need to make sure types are correct in composables |
||||||
items: { value: ["test1"] }, | ||||||
mergeWishlistProducts: () => undefined, | ||||||
removeFromWishlistSync: () => undefined, | ||||||
totalWishlistItemsCount: ref(17), | ||||||
}); | ||||||
const { vm } = useSetup(() => useWishlist()); | ||||||
|
||||||
expect(vm.items.length).toBe(1); | ||||||
expect(vm.count).toBe(1); | ||||||
await vm.clearWishlist(); | ||||||
await vm.getWishlistProducts(); | ||||||
await vm.mergeWishlistProducts(); | ||||||
vm.clearWishlist(); | ||||||
vm.getWishlistProducts(); | ||||||
vm.mergeWishlistProducts(); | ||||||
|
||||||
expect(vm.items.length).toBe(17); | ||||||
expect(vm.totalWishlistItemsCount).toBe(17); | ||||||
expect(vm.getTotalPagesCount).toBe(2); | ||||||
}); | ||||||
|
||||||
it("getWishlistProducts", async () => { | ||||||
vi.mocked(useUser).mockReturnValue({ | ||||||
isLoggedIn: computed(() => false), | ||||||
isGuestSession: computed(() => true), | ||||||
// @ts-ignore | ||||||
canSyncWishlist: { value: false }, | ||||||
}); | ||||||
vi.mocked(useSyncWishlist).mockReturnValue({ | ||||||
getWishlistProducts: () => undefined, | ||||||
// @ts-ignore | ||||||
items: { value: ["test1"] }, | ||||||
mergeWishlistProducts: () => undefined, | ||||||
removeFromWishlistSync: () => undefined, | ||||||
totalWishlistItemsCount: ref(15), | ||||||
}); | ||||||
const { vm } = useSetup(() => useWishlist()); | ||||||
|
||||||
vm.getWishlistProducts(); | ||||||
|
||||||
expect(vm.items).toEqual([ | ||||||
"local-testId-1", | ||||||
"local-testId-2", | ||||||
"local-testId-3", | ||||||
"local-testId-4", | ||||||
"local-testId-5", | ||||||
"local-testId-6", | ||||||
"local-testId-7", | ||||||
"local-testId-8", | ||||||
"local-testId-9", | ||||||
"local-testId-10", | ||||||
"local-testId-11", | ||||||
"local-testId-12", | ||||||
"local-testId-13", | ||||||
"local-testId-14", | ||||||
"local-testId-15", | ||||||
"local-testId-16", | ||||||
"local-testId-17", | ||||||
]); | ||||||
|
||||||
const query = { | ||||||
limit: 1, | ||||||
p: 2, | ||||||
} as Schemas["Criteria"]; | ||||||
vm.changeCurrentPage(2, query); | ||||||
|
||||||
expect(vm.getCurrentPage).toBe(2); | ||||||
}); | ||||||
}); | ||||||
|
||||||
describe("useWishlist - logged in user", () => { | ||||||
it("mergeWishlistProducts", async () => { | ||||||
it("mergeWishlistProducts", () => { | ||||||
// Mock the useUser composable | ||||||
vi.mocked(useUser).mockReturnValue({ | ||||||
isLoggedIn: computed(() => true), | ||||||
isGuestSession: computed(() => false), | ||||||
// @ts-ignore | ||||||
canSyncWishlist: { value: true }, | ||||||
}); | ||||||
// Mock the useSyncWishlist composable | ||||||
vi.mocked(useSyncWishlist).mockReturnValue({ | ||||||
getWishlistProducts: () => undefined, | ||||||
// @ts-ignore | ||||||
items: { value: ["local-testId333"] }, | ||||||
mergeWishlistProducts: () => undefined, | ||||||
removeFromWishlistSync: () => undefined, | ||||||
totalWishlistItemsCount: ref(15), | ||||||
}); | ||||||
const { vm } = useSetup(() => useWishlist()); | ||||||
|
||||||
vm.clearWishlist(); | ||||||
vm.getWishlistProducts(); | ||||||
vm.mergeWishlistProducts(); | ||||||
|
||||||
expect(vm.items.length).toBe(1); | ||||||
expect(vm.count).toBe(1); | ||||||
await vm.clearWishlist(); | ||||||
await vm.getWishlistProducts(); | ||||||
await vm.mergeWishlistProducts(); | ||||||
expect(vm.totalWishlistItemsCount).toBe(15); | ||||||
}); | ||||||
|
||||||
it("getWishlistProducts", () => { | ||||||
vi.mocked(useUser).mockReturnValue({ | ||||||
isLoggedIn: computed(() => true), | ||||||
isGuestSession: computed(() => false), | ||||||
// @ts-ignore | ||||||
canSyncWishlist: { value: true }, | ||||||
}); | ||||||
const { vm } = useSetup(() => useWishlist()); | ||||||
vm.getWishlistProducts(); | ||||||
|
||||||
expect(vm.items).toEqual(["local-testId333"]); | ||||||
}); | ||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's computed property, not getter function, we should rename it