@shopware/composables 1.9.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -6
- package/dist/index.d.mts +99 -29
- package/dist/index.d.ts +99 -29
- package/dist/index.mjs +104 -59
- package/package.json +10 -10
- package/src/cms/cmsAssociations.ts +5 -1
- package/src/cms/useCmsElementConfig.ts +1 -1
- package/src/types/cmsBlockTypes.ts +2 -0
- package/src/types/cmsElementTypes.ts +25 -19
- package/src/useCart/useCart.test.ts +12 -0
- package/src/useCart/useCart.ts +17 -0
- package/src/useCountries/useCountries.test.ts +41 -0
- package/src/useCountries/useCountries.ts +16 -0
- package/src/useCustomerOrders/useCustomerOrders.ts +1 -1
- package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +19 -21
- package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.ts +23 -30
- package/src/useLandingSearch/useLandingSearch.test.ts +1 -1
- package/src/useLandingSearch/useLandingSearch.ts +1 -1
- package/src/useListing/useListing.test.ts +73 -0
- package/src/useListing/useListing.ts +10 -3
- package/src/useNavigationSearch/useNavigationSearch.test.ts +21 -0
- package/src/useNavigationSearch/useNavigationSearch.ts +6 -3
- package/src/useNewsletter/useNewsletter.ts +13 -0
- package/src/useOrderDetails/useOrderDetails.ts +5 -11
- package/src/useOrderPayment/useOrderPayment.ts +1 -0
- package/src/usePrice/usePrice.test.ts +3 -3
- package/src/useProductWishlist/useProductWishlist.ts +2 -2
- package/src/useSessionContext/useSessionContext.ts +1 -1
- package/src/useSyncWishlist/useSyncWishlist.test.ts +34 -17
- package/src/useSyncWishlist/useSyncWishlist.ts +27 -8
- package/src/useUser/useUser.test.ts +126 -3
- package/src/useUser/useUser.ts +6 -1
- package/src/useWishlist/useWishlist.test.ts +9 -0
- package/src/useWishlist/useWishlist.ts +22 -6
|
@@ -10,6 +10,12 @@ export type UseCountriesReturn = {
|
|
|
10
10
|
operations["readCountry post /country"]["response"]
|
|
11
11
|
>;
|
|
12
12
|
getStatesForCountry(countryId: string): Schemas["CountryState"][] | null;
|
|
13
|
+
getCountriesOptions: ComputedRef<
|
|
14
|
+
{
|
|
15
|
+
label: string;
|
|
16
|
+
value: string;
|
|
17
|
+
}[]
|
|
18
|
+
>;
|
|
13
19
|
};
|
|
14
20
|
|
|
15
21
|
/**
|
|
@@ -39,6 +45,15 @@ export function useCountries(): UseCountriesReturn {
|
|
|
39
45
|
return _sharedCountries.value ?? [];
|
|
40
46
|
});
|
|
41
47
|
|
|
48
|
+
const getCountriesOptions = computed(() => {
|
|
49
|
+
return (
|
|
50
|
+
_sharedCountries.value?.map((element: Schemas["Country"]) => ({
|
|
51
|
+
label: element.translated.name,
|
|
52
|
+
value: element.id,
|
|
53
|
+
})) ?? []
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
42
57
|
const mountedCallback = async () => {
|
|
43
58
|
if (!_sharedCountries.value) {
|
|
44
59
|
await fetchCountries();
|
|
@@ -60,5 +75,6 @@ export function useCountries(): UseCountriesReturn {
|
|
|
60
75
|
fetchCountries,
|
|
61
76
|
getStatesForCountry,
|
|
62
77
|
getCountries,
|
|
78
|
+
getCountriesOptions,
|
|
63
79
|
};
|
|
64
80
|
}
|
|
@@ -49,7 +49,7 @@ export function useCustomerOrders(): UseCustomerOrdersReturn {
|
|
|
49
49
|
|
|
50
50
|
const totalOrderItemsCount: Ref<number> = ref(0);
|
|
51
51
|
|
|
52
|
-
const currentParams = ref<
|
|
52
|
+
const currentParams = ref<operations["readOrder post /order"]["body"]>({});
|
|
53
53
|
|
|
54
54
|
const loadOrders = async (
|
|
55
55
|
parameters: operations["readOrder post /order"]["body"] = {},
|
|
@@ -8,31 +8,29 @@ describe("useDefaultOrderAssociations", () => {
|
|
|
8
8
|
|
|
9
9
|
expect(vm).toMatchInlineSnapshot(`
|
|
10
10
|
{
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"stateMachineState": {},
|
|
18
|
-
},
|
|
11
|
+
"addresses": {},
|
|
12
|
+
"deliveries": {
|
|
13
|
+
"associations": {
|
|
14
|
+
"shippingMethod": {},
|
|
15
|
+
"shippingOrderAddress": {},
|
|
16
|
+
"stateMachineState": {},
|
|
19
17
|
},
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
},
|
|
18
|
+
},
|
|
19
|
+
"lineItems": {
|
|
20
|
+
"associations": {
|
|
21
|
+
"cover": {},
|
|
22
|
+
"downloads": {
|
|
23
|
+
"associations": {
|
|
24
|
+
"media": {},
|
|
27
25
|
},
|
|
28
26
|
},
|
|
29
27
|
},
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
},
|
|
28
|
+
},
|
|
29
|
+
"stateMachineState": {},
|
|
30
|
+
"transactions": {
|
|
31
|
+
"associations": {
|
|
32
|
+
"paymentMethod": {},
|
|
33
|
+
"stateMachineState": {},
|
|
36
34
|
},
|
|
37
35
|
},
|
|
38
36
|
}
|
|
@@ -1,45 +1,38 @@
|
|
|
1
1
|
import type { Schemas } from "#shopware";
|
|
2
2
|
|
|
3
|
-
export type UseDefaultOrderAssociationsReturn = Schemas["Criteria"] & {
|
|
4
|
-
checkPromotion?: boolean;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
3
|
/**
|
|
8
4
|
* Returns default order associations. You can override this composable in your project.
|
|
9
5
|
* @public
|
|
10
6
|
* @category Order
|
|
11
7
|
*/
|
|
12
|
-
export function useDefaultOrderAssociations():
|
|
13
|
-
const orderAssociations
|
|
14
|
-
{
|
|
8
|
+
export function useDefaultOrderAssociations(): Schemas["Criteria"]["associations"] {
|
|
9
|
+
const orderAssociations = {
|
|
10
|
+
stateMachineState: {},
|
|
11
|
+
lineItems: {
|
|
15
12
|
associations: {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
associations: {
|
|
19
|
-
cover: {},
|
|
20
|
-
downloads: {
|
|
21
|
-
associations: {
|
|
22
|
-
media: {},
|
|
23
|
-
},
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
addresses: {},
|
|
28
|
-
deliveries: {
|
|
29
|
-
associations: {
|
|
30
|
-
shippingMethod: {},
|
|
31
|
-
shippingOrderAddress: {},
|
|
32
|
-
stateMachineState: {},
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
transactions: {
|
|
13
|
+
cover: {},
|
|
14
|
+
downloads: {
|
|
36
15
|
associations: {
|
|
37
|
-
|
|
38
|
-
stateMachineState: {},
|
|
16
|
+
media: {},
|
|
39
17
|
},
|
|
40
18
|
},
|
|
41
19
|
},
|
|
42
|
-
}
|
|
20
|
+
},
|
|
21
|
+
addresses: {},
|
|
22
|
+
deliveries: {
|
|
23
|
+
associations: {
|
|
24
|
+
shippingMethod: {},
|
|
25
|
+
shippingOrderAddress: {},
|
|
26
|
+
stateMachineState: {},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
transactions: {
|
|
30
|
+
associations: {
|
|
31
|
+
paymentMethod: {},
|
|
32
|
+
stateMachineState: {},
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
};
|
|
43
36
|
|
|
44
37
|
return orderAssociations;
|
|
45
38
|
}
|
|
@@ -23,7 +23,7 @@ describe("useLandingSearch", () => {
|
|
|
23
23
|
expect.stringContaining("readLandingPage"),
|
|
24
24
|
expect.objectContaining({
|
|
25
25
|
pathParams: { landingPageId: "test" },
|
|
26
|
-
body: { associations: cmsAssociations },
|
|
26
|
+
body: { associations: cmsAssociations.associations },
|
|
27
27
|
}),
|
|
28
28
|
);
|
|
29
29
|
});
|
|
@@ -37,7 +37,7 @@ export function useLandingSearch(): {
|
|
|
37
37
|
},
|
|
38
38
|
) => {
|
|
39
39
|
const associations: operations["readLandingPage post /landing-page/{landingPageId}"]["body"]["associations"] =
|
|
40
|
-
options?.withCmsAssociations ? cmsAssociations : {};
|
|
40
|
+
options?.withCmsAssociations ? cmsAssociations.associations : {};
|
|
41
41
|
const result = await apiClient.invoke(
|
|
42
42
|
"readLandingPage post /landing-page/{landingPageId}",
|
|
43
43
|
{
|
|
@@ -246,4 +246,77 @@ describe("useListing", () => {
|
|
|
246
246
|
);
|
|
247
247
|
expect(vm.getAvailableFilters).toEqual([]);
|
|
248
248
|
});
|
|
249
|
+
|
|
250
|
+
it("should accept initialListing parameter", async () => {
|
|
251
|
+
const mockListing = {
|
|
252
|
+
apiAlias: "product_listing",
|
|
253
|
+
elements: [
|
|
254
|
+
{
|
|
255
|
+
id: "product-1",
|
|
256
|
+
name: "Test Product",
|
|
257
|
+
} as Schemas["Product"],
|
|
258
|
+
],
|
|
259
|
+
page: 1,
|
|
260
|
+
limit: 10,
|
|
261
|
+
total: 25,
|
|
262
|
+
} as Schemas["ProductListingResult"];
|
|
263
|
+
|
|
264
|
+
const { vm } = await useSetup(() =>
|
|
265
|
+
useListing({
|
|
266
|
+
listingType: "productSearchListing",
|
|
267
|
+
initialListing: mockListing,
|
|
268
|
+
}),
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
expect(vm.getInitialListing).toEqual(mockListing);
|
|
272
|
+
expect(vm.getCurrentListing).toEqual(mockListing);
|
|
273
|
+
expect(vm.getElements).toEqual(mockListing.elements);
|
|
274
|
+
expect(vm.getTotal).toBe(25);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("should handle null initialListing parameter", async () => {
|
|
278
|
+
const { vm } = await useSetup(() =>
|
|
279
|
+
useListing({
|
|
280
|
+
listingType: "productSearchListing",
|
|
281
|
+
initialListing: null,
|
|
282
|
+
}),
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
expect(vm.getInitialListing).toBeNull();
|
|
286
|
+
expect(vm.getCurrentListing).toBeNull();
|
|
287
|
+
expect(vm.getElements).toEqual([]);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("should work without initialListing parameter (backward compatibility)", async () => {
|
|
291
|
+
const { vm } = await useSetup(() =>
|
|
292
|
+
useListing({
|
|
293
|
+
listingType: "productSearchListing",
|
|
294
|
+
}),
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
expect(vm.getInitialListing).toBeNull();
|
|
298
|
+
expect(vm.getCurrentListing).toBeNull();
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it("should initialize category listing with initialListing", async () => {
|
|
302
|
+
const mockListing = {
|
|
303
|
+
apiAlias: "product_listing",
|
|
304
|
+
elements: [{ id: "product-1" } as Schemas["Product"]],
|
|
305
|
+
page: 1,
|
|
306
|
+
limit: 10,
|
|
307
|
+
total: 1,
|
|
308
|
+
} as Schemas["ProductListingResult"];
|
|
309
|
+
|
|
310
|
+
const { vm } = await useSetup(() =>
|
|
311
|
+
useListing({
|
|
312
|
+
listingType: "categoryListing",
|
|
313
|
+
categoryId: "test-category",
|
|
314
|
+
initialListing: mockListing,
|
|
315
|
+
}),
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
expect(vm.getInitialListing).toEqual(mockListing);
|
|
319
|
+
expect(vm.getElements).toHaveLength(1);
|
|
320
|
+
expect(vm.getTotal).toBe(1);
|
|
321
|
+
});
|
|
249
322
|
});
|
|
@@ -183,6 +183,7 @@ export function useListing(params?: {
|
|
|
183
183
|
listingType: ListingType;
|
|
184
184
|
categoryId?: string;
|
|
185
185
|
defaultSearchCriteria?: operations["searchPage post /search"]["body"];
|
|
186
|
+
initialListing?: Schemas["ProductListingResult"] | null;
|
|
186
187
|
}): UseListingReturn {
|
|
187
188
|
const listingType = params?.listingType || "categoryListing";
|
|
188
189
|
let categoryId = params?.categoryId || null;
|
|
@@ -241,13 +242,17 @@ export function useListing(params?: {
|
|
|
241
242
|
searchDefaults:
|
|
242
243
|
params?.defaultSearchCriteria ||
|
|
243
244
|
({} as operations["searchPage post /search"]["body"]), //getDefaults(),
|
|
245
|
+
initialListing: params?.initialListing,
|
|
244
246
|
});
|
|
245
247
|
}
|
|
246
248
|
|
|
247
249
|
const [_createCategoryListingContext, _categoryListingContext] =
|
|
248
250
|
createInjectionState(
|
|
249
|
-
() => {
|
|
250
|
-
return useListing({
|
|
251
|
+
(initialListing?: Schemas["ProductListingResult"] | null) => {
|
|
252
|
+
return useListing({
|
|
253
|
+
listingType: "categoryListing",
|
|
254
|
+
initialListing,
|
|
255
|
+
});
|
|
251
256
|
},
|
|
252
257
|
{
|
|
253
258
|
injectionKey: "categoryListing",
|
|
@@ -292,6 +297,7 @@ export function createListingComposable({
|
|
|
292
297
|
searchMethod,
|
|
293
298
|
searchDefaults,
|
|
294
299
|
listingKey,
|
|
300
|
+
initialListing,
|
|
295
301
|
}: {
|
|
296
302
|
searchMethod(
|
|
297
303
|
searchParams:
|
|
@@ -300,6 +306,7 @@ export function createListingComposable({
|
|
|
300
306
|
): Promise<Schemas["ProductListingResult"]>;
|
|
301
307
|
searchDefaults: operations["searchPage post /search"]["body"];
|
|
302
308
|
listingKey: string;
|
|
309
|
+
initialListing?: Schemas["ProductListingResult"] | null;
|
|
303
310
|
}): UseListingReturn {
|
|
304
311
|
// const COMPOSABLE_NAME = "createListingComposable";
|
|
305
312
|
// const contextName = COMPOSABLE_NAME;
|
|
@@ -319,7 +326,7 @@ export function createListingComposable({
|
|
|
319
326
|
// const { sharedRef } = useSharedState();
|
|
320
327
|
const _storeInitialListing = inject<
|
|
321
328
|
Ref<Schemas["ProductListingResult"] | null>
|
|
322
|
-
>(`useListingInitial-${listingKey}`, ref(null));
|
|
329
|
+
>(`useListingInitial-${listingKey}`, ref(initialListing ?? null));
|
|
323
330
|
provide(`useListingInitial-${listingKey}`, _storeInitialListing);
|
|
324
331
|
// const _storeInitialListing = sharedRef<Schemas["ProductListingResult"]>(
|
|
325
332
|
// `${cacheKey}-initialListing-${listingKey}`
|
|
@@ -76,6 +76,27 @@ describe("useNavigationSearch", () => {
|
|
|
76
76
|
}),
|
|
77
77
|
);
|
|
78
78
|
});
|
|
79
|
+
it("should resolve technical url with trailing slash", async () => {
|
|
80
|
+
const { vm, injections } = useSetup(useNavigationSearch);
|
|
81
|
+
injections.apiClient.invoke.mockResolvedValue({ data: { elements: [] } });
|
|
82
|
+
|
|
83
|
+
await vm.resolvePath("/detail/test/");
|
|
84
|
+
|
|
85
|
+
expect(injections.apiClient.invoke).toHaveBeenCalledWith(
|
|
86
|
+
expect.stringContaining("readSeoUrl"),
|
|
87
|
+
expect.objectContaining({
|
|
88
|
+
body: {
|
|
89
|
+
filter: [
|
|
90
|
+
{
|
|
91
|
+
type: "equals",
|
|
92
|
+
field: "pathInfo",
|
|
93
|
+
value: "/detail/test",
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
}),
|
|
98
|
+
);
|
|
99
|
+
});
|
|
79
100
|
it("should not invoke api call for / search ", async () => {
|
|
80
101
|
const { vm, injections } = useSetup(useNavigationSearch);
|
|
81
102
|
injections.apiClient.invoke.mockResolvedValue({
|
|
@@ -35,9 +35,12 @@ export function useNavigationSearch(): UseNavigationSearchReturn {
|
|
|
35
35
|
path.startsWith("/detail/") ||
|
|
36
36
|
path.startsWith("/landingPage/");
|
|
37
37
|
|
|
38
|
-
// remove leading slash in case of seo url
|
|
39
|
-
const normalizedPath = isTechnicalUrl
|
|
40
|
-
|
|
38
|
+
// remove leading slash in case of seo url or remove trailing slash in case of technical url
|
|
39
|
+
const normalizedPath = isTechnicalUrl
|
|
40
|
+
? path.endsWith("/")
|
|
41
|
+
? path.slice(0, -1)
|
|
42
|
+
: path
|
|
43
|
+
: path.substring(1);
|
|
41
44
|
|
|
42
45
|
const seoResult = await apiClient.invoke("readSeoUrl post /seo-url", {
|
|
43
46
|
body: {
|
|
@@ -37,6 +37,14 @@ export type UseNewsletterReturn = {
|
|
|
37
37
|
* Inform about newsletter confirmation
|
|
38
38
|
*/
|
|
39
39
|
confirmationNeeded: ComputedRef<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* Subscribe key
|
|
42
|
+
*/
|
|
43
|
+
SUBSRIBE_KEY: string;
|
|
44
|
+
/**
|
|
45
|
+
* Unsubscribe key
|
|
46
|
+
*/
|
|
47
|
+
UNSUBSCRIBE_KEY: string;
|
|
40
48
|
};
|
|
41
49
|
|
|
42
50
|
/**
|
|
@@ -45,6 +53,9 @@ export type UseNewsletterReturn = {
|
|
|
45
53
|
* @category Customer & Account
|
|
46
54
|
*/
|
|
47
55
|
export function useNewsletter(): UseNewsletterReturn {
|
|
56
|
+
const SUBSRIBE_KEY = "subscribe";
|
|
57
|
+
const UNSUBSCRIBE_KEY = "unsubscribe";
|
|
58
|
+
|
|
48
59
|
const { apiClient } = useShopwareContext();
|
|
49
60
|
const { getStorefrontUrl } = useInternationalization();
|
|
50
61
|
const newsletterStatus =
|
|
@@ -105,5 +116,7 @@ export function useNewsletter(): UseNewsletterReturn {
|
|
|
105
116
|
getNewsletterStatus,
|
|
106
117
|
newsletterStatus,
|
|
107
118
|
confirmationNeeded,
|
|
119
|
+
SUBSRIBE_KEY,
|
|
120
|
+
UNSUBSCRIBE_KEY,
|
|
108
121
|
};
|
|
109
122
|
}
|
|
@@ -2,7 +2,7 @@ import { defu } from "defu";
|
|
|
2
2
|
import { computed, inject, provide, ref } from "vue";
|
|
3
3
|
import type { ComputedRef, Ref } from "vue";
|
|
4
4
|
import { useDefaultOrderAssociations, useShopwareContext } from "#imports";
|
|
5
|
-
import type { Schemas } from "#shopware";
|
|
5
|
+
import type { Schemas, operations } from "#shopware";
|
|
6
6
|
|
|
7
7
|
export type UseOrderDetailsReturn = {
|
|
8
8
|
/**
|
|
@@ -182,17 +182,11 @@ export function useOrderDetails(
|
|
|
182
182
|
orderAssociations,
|
|
183
183
|
associations ? associations : {},
|
|
184
184
|
);
|
|
185
|
-
const params = {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
type: "equals",
|
|
189
|
-
field: "id",
|
|
190
|
-
value: orderId,
|
|
191
|
-
},
|
|
192
|
-
],
|
|
193
|
-
associations: mergedAssociations.associations,
|
|
185
|
+
const params: operations["readOrder post /order"]["body"] = {
|
|
186
|
+
ids: [orderId],
|
|
187
|
+
associations: mergedAssociations,
|
|
194
188
|
checkPromotion: true,
|
|
195
|
-
}
|
|
189
|
+
};
|
|
196
190
|
|
|
197
191
|
const orderDetailsResponse = await apiClient.invoke(
|
|
198
192
|
"readOrder post /order",
|
|
@@ -64,6 +64,7 @@ export function useOrderPayment(
|
|
|
64
64
|
const state = computed(() => activeTransaction.value?.stateMachineState);
|
|
65
65
|
const isAsynchronous = computed(
|
|
66
66
|
() =>
|
|
67
|
+
// @ts-expect-error TODO: [MAJOR] Removed since 6.7
|
|
67
68
|
activeTransaction.value?.paymentMethod?.asynchronous &&
|
|
68
69
|
activeTransaction.value?.paymentMethod?.afterOrderEnabled,
|
|
69
70
|
);
|
|
@@ -42,7 +42,7 @@ describe("usePrice", () => {
|
|
|
42
42
|
it("should use navigator language if locale is not provided", () => {
|
|
43
43
|
const { vm } = useSetup(usePrice, {
|
|
44
44
|
shopware: {
|
|
45
|
-
browserLocale: "
|
|
45
|
+
browserLocale: "de-DE",
|
|
46
46
|
},
|
|
47
47
|
});
|
|
48
48
|
vm.update({
|
|
@@ -50,8 +50,8 @@ describe("usePrice", () => {
|
|
|
50
50
|
localeCode: undefined,
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
-
expect(vm.currencyLocale).toBe("
|
|
54
|
-
expect(vm.getFormattedPrice(2.55)).toMatchInlineSnapshot(
|
|
53
|
+
expect(vm.currencyLocale).toBe("de-DE");
|
|
54
|
+
expect(vm.getFormattedPrice(2.55)).toMatchInlineSnapshot(`"2,55 $"`);
|
|
55
55
|
});
|
|
56
56
|
|
|
57
57
|
it("should init price object", () => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { computed } from "vue";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ComputedRef } from "vue";
|
|
3
3
|
import { useLocalWishlist, useSyncWishlist, useUser } from "#imports";
|
|
4
4
|
|
|
5
5
|
export type UseProductWishlistReturn = {
|
|
@@ -14,7 +14,7 @@ export type UseProductWishlistReturn = {
|
|
|
14
14
|
/**
|
|
15
15
|
* Indicates whether a product is in wishlist
|
|
16
16
|
*/
|
|
17
|
-
isInWishlist:
|
|
17
|
+
isInWishlist: ComputedRef<boolean>;
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -89,27 +89,44 @@ describe("useSyncWishlist", () => {
|
|
|
89
89
|
|
|
90
90
|
describe("getWishlistProducts - error", () => {
|
|
91
91
|
it("should handle error after fetching product wishlist", async () => {
|
|
92
|
-
const { vm } = useSetup(() => useSyncWishlist()
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
],
|
|
92
|
+
const { vm, injections } = useSetup(() => useSyncWishlist());
|
|
93
|
+
// First, set up a successful call to populate state
|
|
94
|
+
injections.apiClient.invoke.mockResolvedValueOnce({
|
|
95
|
+
data: {
|
|
96
|
+
products: {
|
|
97
|
+
total: 9,
|
|
98
|
+
page: 1,
|
|
99
|
+
elements: [
|
|
100
|
+
{
|
|
101
|
+
id: "test-id",
|
|
103
102
|
},
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}>);
|
|
107
|
-
}),
|
|
103
|
+
],
|
|
104
|
+
},
|
|
108
105
|
},
|
|
109
106
|
});
|
|
110
|
-
vm.getWishlistProducts();
|
|
111
|
-
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
|
|
107
|
+
await vm.getWishlistProducts();
|
|
112
108
|
expect(vm.count).toBe(9);
|
|
109
|
+
expect(vm.items.length).toBe(1);
|
|
110
|
+
|
|
111
|
+
// Now test error handling - state should be cleared on error
|
|
112
|
+
injections.apiClient.invoke.mockImplementation(() => {
|
|
113
|
+
throw new ApiClientError({
|
|
114
|
+
status: 500,
|
|
115
|
+
_data: {
|
|
116
|
+
errors: [
|
|
117
|
+
{
|
|
118
|
+
title: "Test error",
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
},
|
|
122
|
+
} as unknown as FetchResponse<{
|
|
123
|
+
errors: Array<{ title: string }>;
|
|
124
|
+
}>);
|
|
125
|
+
});
|
|
126
|
+
await vm.getWishlistProducts();
|
|
127
|
+
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
|
|
128
|
+
// State should be cleared when error occurs
|
|
129
|
+
expect(vm.count).toBe(0);
|
|
113
130
|
expect(vm.items.length).toBe(0);
|
|
114
131
|
});
|
|
115
132
|
});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ApiClientError } from "@shopware/api-client";
|
|
2
1
|
import { computed, ref } from "vue";
|
|
3
2
|
import type { ComputedRef, Ref } from "vue";
|
|
4
3
|
import { useShopwareContext } from "#imports";
|
|
@@ -35,12 +34,26 @@ export type UseSyncWishlistReturn = {
|
|
|
35
34
|
* Current page number
|
|
36
35
|
*/
|
|
37
36
|
currentPage: ComputedRef<number>;
|
|
37
|
+
/**
|
|
38
|
+
* Limit number of products per page
|
|
39
|
+
*/
|
|
40
|
+
limit: ComputedRef<number>;
|
|
41
|
+
/**
|
|
42
|
+
* Wishlist products
|
|
43
|
+
*/
|
|
44
|
+
products: ComputedRef<Schemas["Product"][]>;
|
|
45
|
+
/**
|
|
46
|
+
* Indicates if the wishlist is loading
|
|
47
|
+
*/
|
|
48
|
+
isLoading: Ref<boolean>;
|
|
38
49
|
};
|
|
39
50
|
|
|
40
51
|
const _wishlistItems: Ref<string[]> = ref([]);
|
|
52
|
+
const _wishlistProducts: Ref<Schemas["Product"][]> = ref([]);
|
|
41
53
|
const _currentPage: Ref<number> = ref(1);
|
|
54
|
+
const _limit: Ref<number> = ref(15);
|
|
42
55
|
const totalWishlistItemsCount: Ref<number> = ref(0);
|
|
43
|
-
|
|
56
|
+
const isLoading: Ref<boolean> = ref(false);
|
|
44
57
|
/**
|
|
45
58
|
* Composable to manage wishlist via API
|
|
46
59
|
* @public
|
|
@@ -81,14 +94,15 @@ export function useSyncWishlist(): UseSyncWishlistReturn {
|
|
|
81
94
|
_wishlistItems.value = [
|
|
82
95
|
...response.data.products.elements.map((element) => element.id),
|
|
83
96
|
];
|
|
97
|
+
_wishlistProducts.value = response.data.products.elements;
|
|
84
98
|
totalWishlistItemsCount.value = response.data.products.total ?? 0;
|
|
85
99
|
_currentPage.value = response.data.products.page ?? 1;
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
100
|
+
_limit.value = response.data.products.limit ?? 15;
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.error("[useSyncWishlist][getWishlistProducts][error]:", error);
|
|
103
|
+
_wishlistItems.value = [];
|
|
104
|
+
_wishlistProducts.value = [];
|
|
105
|
+
totalWishlistItemsCount.value = 0;
|
|
92
106
|
}
|
|
93
107
|
}
|
|
94
108
|
|
|
@@ -104,14 +118,19 @@ export function useSyncWishlist(): UseSyncWishlistReturn {
|
|
|
104
118
|
const items = computed(() => _wishlistItems.value);
|
|
105
119
|
const count = computed(() => totalWishlistItemsCount.value);
|
|
106
120
|
const currentPage = computed(() => _currentPage.value);
|
|
121
|
+
const products = computed(() => _wishlistProducts.value);
|
|
122
|
+
const limit = computed(() => _limit.value);
|
|
107
123
|
|
|
108
124
|
return {
|
|
109
125
|
getWishlistProducts,
|
|
110
126
|
addToWishlistSync,
|
|
111
127
|
removeFromWishlistSync,
|
|
112
128
|
mergeWishlistProducts,
|
|
129
|
+
products,
|
|
113
130
|
items,
|
|
114
131
|
count,
|
|
115
132
|
currentPage,
|
|
133
|
+
isLoading,
|
|
134
|
+
limit,
|
|
116
135
|
};
|
|
117
136
|
}
|