Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a1a076c
addedPaginationForWishlistChangesFewVariableNames
Apr 21, 2024
abe3b88
addedPaginationForOrderHistoryPage
Apr 21, 2024
e9a612b
Merge branch 'main' into paginationForWishlistOrderHistoryPage
mkucmus Apr 26, 2024
65bb409
feat: fix unit tests for wishlist, increase coverage
BrocksiNet May 16, 2024
0ea15fa
Merge branch 'main' into paginationForWishlistOrderHistoryPage
BrocksiNet May 16, 2024
d7ff926
feat: fix display bug
BrocksiNet May 16, 2024
de6dede
Merge branch 'main' into pr/khanSoliheen/893
patzick Jun 5, 2024
7ecc7ce
chore: fix types after merge
patzick Jun 5, 2024
248b8ae
Merge branch 'main' into paginationForWishlistOrderHistoryPage
BrocksiNet Jul 5, 2024
25dbc2c
feat: fix ts errors
BrocksiNet Jul 5, 2024
d7045a4
feat: improve wishlist tests, fix order pagination
BrocksiNet Jul 5, 2024
222258d
feat: fix useCustomerOrders test
BrocksiNet Jul 5, 2024
d500445
feat: improve useCustomerOrders coverage
BrocksiNet Jul 5, 2024
19ed7f9
fix(demo): wishlist count, tests, and message
BrocksiNet Jul 8, 2024
27e73b6
fix(demo): update changeset
BrocksiNet Jul 8, 2024
fa9eabc
fix(composable): remove useless call, fix tests, pagination works
BrocksiNet Jul 8, 2024
02cebf1
fix(composable): update changeset, update useWishlist
BrocksiNet Jul 8, 2024
c4b2a10
fix(composable): remove log
BrocksiNet Jul 8, 2024
8834df5
fix(composable): count guests, initial wishlist
BrocksiNet Jul 9, 2024
4de8143
fix(composable): wishlist on category pages
BrocksiNet Jul 9, 2024
6df4991
fix(composable): remove not needed parameter
BrocksiNet Jul 9, 2024
7230b91
fix(demo): limit parameter
BrocksiNet Jul 9, 2024
245e869
Merge branch 'main' into pr/khanSoliheen/893
patzick Jul 9, 2024
80dc0c6
feat(composables): review wishlist
BrocksiNet Jul 10, 2024
ae92a8b
fix(docs): format
BrocksiNet Jul 10, 2024
29edfad
fix(composables): type definition
BrocksiNet Jul 10, 2024
442adc8
Merge branch 'main' into paginationForWishlistOrderHistoryPage
patzick Jul 11, 2024
0c683e7
chore: cr suggestions
patzick Jul 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/light-cows-protect.md
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.
6 changes: 6 additions & 0 deletions .changeset/tough-queens-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"vue-demo-store": minor
"@shopware-pwa/composables-next": minor
BrocksiNet marked this conversation as resolved.
Show resolved Hide resolved
---

Added a pagination for the wishlist page. I made changes to the variable name to get the proper context of it.
41 changes: 36 additions & 5 deletions packages/composables/src/useCustomerOrders.ts
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";

Expand All @@ -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,
patzick marked this conversation as resolved.
Show resolved Hide resolved
): 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>;
BrocksiNet marked this conversation as resolved.
Show resolved Hide resolved
/**
* total pages count
*/
getTotalPagesCount: ComputedRef<number>;
BrocksiNet marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand All @@ -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,
};
}
4 changes: 2 additions & 2 deletions packages/composables/src/useSyncWishlist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("useSyncWishlist", () => {
vm.getWishlistProducts();
vm.removeFromWishlistSync("some-id");
// Mocked value
expect(vm.count).toBe(vm.count);
expect(vm.totalWishlistItemsCount).toBe(vm.totalWishlistItemsCount);
});
});

Expand All @@ -38,7 +38,7 @@ describe("useSyncWishlist", () => {
const { vm } = useSetup(() => useSyncWishlist());

vm.getWishlistProducts();
expect(vm.count).toBe(0);
expect(vm.totalWishlistItemsCount).toBe(0);
expect(vm.items.length).toBe(0);
});
});
Expand Down
18 changes: 12 additions & 6 deletions packages/composables/src/useSyncWishlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { ref, computed } from "vue";
import type { Ref, ComputedRef } from "vue";
import { useShopwareContext } from "#imports";
import { ApiClientError } from "@shopware/api-client";
import type { operations } from "#shopware";

export type UseSyncWishlistReturn = {
/**
* Get products from wishlist
*/
getWishlistProducts(): void;
getWishlistProducts(
defaultSearchCriteria?: operations["searchPage post /search"]["body"],
): void;
/**
* Merge products with wishlist already existing in API wishlist
*/
Expand All @@ -27,10 +30,11 @@ export type UseSyncWishlistReturn = {
/**
* Wishlist items count
*/
count: ComputedRef<number>;
totalWishlistItemsCount: Ref<number>;
BrocksiNet marked this conversation as resolved.
Show resolved Hide resolved
};

const _wishlistItems: Ref<string[]> = ref([]);
const totalWishlistItemsCount: Ref<number> = ref(0);

/**
* Composable to manage wishlist via API
Expand All @@ -39,7 +43,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}",
Expand All @@ -62,14 +65,18 @@ export function useSyncWishlist(): UseSyncWishlistReturn {
* Fetch wishlist items
* Only for logged-in users
*/
async function getWishlistProducts() {
async function getWishlistProducts(
defaultSearchCriteria?: operations["searchPage post /search"]["body"],
) {
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
Expand All @@ -89,14 +96,13 @@ export function useSyncWishlist(): UseSyncWishlistReturn {
}

const items = computed(() => _wishlistItems.value);
const count = computed(() => items.value.length);
BrocksiNet marked this conversation as resolved.
Show resolved Hide resolved

return {
getWishlistProducts,
addToWishlistSync,
removeFromWishlistSync,
mergeWishlistProducts,
items,
count,
totalWishlistItemsCount,
};
}
164 changes: 143 additions & 21 deletions packages/composables/src/useWishlist.test.ts
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 { operations } 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 },
BrocksiNet marked this conversation as resolved.
Show resolved Hide resolved
});
vi.mocked(useSyncWishlist).mockReturnValue({
getWishlistProducts: () => undefined,
// @ts-ignore
BrocksiNet marked this conversation as resolved.
Show resolved Hide resolved
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 operations["searchPage post /search"]["body"];
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"]);
});
});
Loading