@shopware/composables 0.0.0-canary-20250116155626
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/LICENSE +21 -0
- package/README.md +172 -0
- package/composables/index.ts +1 -0
- package/dist/index.d.mts +2239 -0
- package/dist/index.d.ts +2239 -0
- package/dist/index.mjs +2686 -0
- package/nuxt.config.ts +14 -0
- package/package.json +83 -0
- package/src/_test/index.ts +1 -0
- package/src/_test/useSetup.ts +43 -0
- package/src/cms/README.md +9 -0
- package/src/cms/cmsAssociations.ts +28 -0
- package/src/cms/index.ts +2 -0
- package/src/cms/useCmsElementConfig.test.ts +145 -0
- package/src/cms/useCmsElementConfig.ts +29 -0
- package/src/cms/useCmsElementImage.test.ts +179 -0
- package/src/cms/useCmsElementImage.ts +103 -0
- package/src/cms/useCmsElementProductBox.test.ts +15 -0
- package/src/cms/useCmsElementProductBox.ts +25 -0
- package/src/createShopwareContext.test.ts +23 -0
- package/src/createShopwareContext.ts +43 -0
- package/src/devtools/plugin.ts +201 -0
- package/src/helpers/ContextError.spec.ts +20 -0
- package/src/helpers/ContextError.ts +6 -0
- package/src/index.test.ts +54 -0
- package/src/index.ts +111 -0
- package/src/mocks/Cart.ts +774 -0
- package/src/mocks/CmsPage.ts +544 -0
- package/src/mocks/CmsProductBox.ts +377 -0
- package/src/mocks/Configurator.ts +123 -0
- package/src/mocks/Country.ts +184 -0
- package/src/mocks/CrossSellingResponse.ts +552 -0
- package/src/mocks/LandingPage.ts +62 -0
- package/src/mocks/Menu.ts +598 -0
- package/src/mocks/Order.ts +1151 -0
- package/src/mocks/Product.ts +473 -0
- package/src/mocks/ProductTierPrices.ts +771 -0
- package/src/mocks/Salutations.ts +51 -0
- package/src/mocks/Search.ts +518 -0
- package/src/types/cmsBlockTypes.ts +80 -0
- package/src/types/cmsElementTypes.ts +381 -0
- package/src/types/cmsSectionTypes.ts +15 -0
- package/src/types/index.ts +52 -0
- package/src/useAddToCart/useAddToCart.test.ts +39 -0
- package/src/useAddToCart/useAddToCart.ts +90 -0
- package/src/useAddress/useAddress.test.ts +134 -0
- package/src/useAddress/useAddress.ts +202 -0
- package/src/useB2bQuoteManagement/useB2bQuoteManagement.test.ts +137 -0
- package/src/useB2bQuoteManagement/useB2bQuoteManagement.ts +225 -0
- package/src/useBreadcrumbs/useBreadcrumbs.test.ts +71 -0
- package/src/useBreadcrumbs/useBreadcrumbs.ts +89 -0
- package/src/useCart/useCart.test.ts +251 -0
- package/src/useCart/useCart.ts +314 -0
- package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.test.ts +122 -0
- package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.ts +80 -0
- package/src/useCartItem/useCartItem.test.ts +168 -0
- package/src/useCartItem/useCartItem.ts +157 -0
- package/src/useCartNotification/useCartNotification.test.ts +129 -0
- package/src/useCartNotification/useCartNotification.ts +63 -0
- package/src/useCategory/useCategory.test.ts +14 -0
- package/src/useCategory/useCategory.ts +32 -0
- package/src/useCategorySearch/useCategorySearch.test.ts +98 -0
- package/src/useCategorySearch/useCategorySearch.ts +81 -0
- package/src/useCheckout/useCheckout.test.ts +104 -0
- package/src/useCheckout/useCheckout.ts +163 -0
- package/src/useCmsBlock/useCmsBlock.test.ts +25 -0
- package/src/useCmsBlock/useCmsBlock.ts +36 -0
- package/src/useCmsMeta/useCmsMeta.test.ts +17 -0
- package/src/useCmsMeta/useCmsMeta.ts +46 -0
- package/src/useCmsSection/useCmsSection.test.ts +18 -0
- package/src/useCmsSection/useCmsSection.ts +39 -0
- package/src/useCmsTranslations/useCmsTranslations.test.ts +10 -0
- package/src/useCmsTranslations/useCmsTranslations.ts +9 -0
- package/src/useContext/useContext.ts +36 -0
- package/src/useCountries/useCountries.test.ts +64 -0
- package/src/useCountries/useCountries.ts +64 -0
- package/src/useCustomerOrders/useCustomerOrders.test.ts +47 -0
- package/src/useCustomerOrders/useCustomerOrders.ts +89 -0
- package/src/useCustomerPassword/useCustomerPassword.test.ts +164 -0
- package/src/useCustomerPassword/useCustomerPassword.ts +57 -0
- package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +41 -0
- package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.ts +45 -0
- package/src/useInternationalization/useInternationalization.test.ts +155 -0
- package/src/useInternationalization/useInternationalization.ts +159 -0
- package/src/useLandingSearch/useLandingSearch.test.ts +30 -0
- package/src/useLandingSearch/useLandingSearch.ts +58 -0
- package/src/useListing/useListing.test.ts +249 -0
- package/src/useListing/useListing.ts +592 -0
- package/src/useLocalWishlist/useLocalWishlist.test.ts +54 -0
- package/src/useLocalWishlist/useLocalWishlist.ts +95 -0
- package/src/useNavigation/useNavigation.test.ts +68 -0
- package/src/useNavigation/useNavigation.ts +83 -0
- package/src/useNavigationContext/useNavigationContext.test.ts +48 -0
- package/src/useNavigationContext/useNavigationContext.ts +39 -0
- package/src/useNavigationSearch/useNavigationSearch.test.ts +110 -0
- package/src/useNavigationSearch/useNavigationSearch.ts +60 -0
- package/src/useNewsletter/useNewsletter.test.ts +55 -0
- package/src/useNewsletter/useNewsletter.ts +109 -0
- package/src/useNotifications/useNotifications.test.ts +56 -0
- package/src/useNotifications/useNotifications.ts +119 -0
- package/src/useOrderDetails/useOrderDetails.test.ts +224 -0
- package/src/useOrderDetails/useOrderDetails.ts +329 -0
- package/src/useOrderPayment/useOrderPayment.test.ts +80 -0
- package/src/useOrderPayment/useOrderPayment.ts +119 -0
- package/src/usePrice/usePrice.test.ts +97 -0
- package/src/usePrice/usePrice.ts +106 -0
- package/src/useProduct/useProduct.test.ts +36 -0
- package/src/useProduct/useProduct.ts +56 -0
- package/src/useProductAssociations/useProductAssociations.spec.ts +105 -0
- package/src/useProductAssociations/useProductAssociations.ts +107 -0
- package/src/useProductConfigurator/useProductConfigurator.spec.ts +77 -0
- package/src/useProductConfigurator/useProductConfigurator.ts +134 -0
- package/src/useProductPrice/useProductPrice.test.ts +126 -0
- package/src/useProductPrice/useProductPrice.ts +131 -0
- package/src/useProductReviews/useProductReviews.test.ts +45 -0
- package/src/useProductReviews/useProductReviews.ts +80 -0
- package/src/useProductSearch/useProductSearch.test.ts +37 -0
- package/src/useProductSearch/useProductSearch.ts +58 -0
- package/src/useProductSearchSuggest/useProductSearchSuggest.test.ts +19 -0
- package/src/useProductSearchSuggest/useProductSearchSuggest.ts +67 -0
- package/src/useProductWishlist/useProductWishlist.test.ts +63 -0
- package/src/useProductWishlist/useProductWishlist.ts +74 -0
- package/src/useSalutations/useSalutations.spec.ts +37 -0
- package/src/useSalutations/useSalutations.ts +55 -0
- package/src/useSessionContext/useSessionContext.test.ts +221 -0
- package/src/useSessionContext/useSessionContext.ts +281 -0
- package/src/useShopwareContext/useShopwareContext.test.ts +8 -0
- package/src/useShopwareContext/useShopwareContext.ts +31 -0
- package/src/useSyncWishlist/useSyncWishlist.test.ts +117 -0
- package/src/useSyncWishlist/useSyncWishlist.ts +117 -0
- package/src/useUrlResolver/useUrlResolver.test.ts +25 -0
- package/src/useUrlResolver/useUrlResolver.ts +31 -0
- package/src/useUser/useUser.test.ts +290 -0
- package/src/useUser/useUser.ts +321 -0
- package/src/useWishlist/useWishlist.test.ts +178 -0
- package/src/useWishlist/useWishlist.ts +128 -0
- package/types/api-types.ts +11 -0
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
import { getListingFilters } from "@shopware-pwa/helpers-next";
|
|
2
|
+
import { createInjectionState, createSharedComposable } from "@vueuse/core";
|
|
3
|
+
import { computed, inject, provide, ref } from "vue";
|
|
4
|
+
import type { ComputedRef, Ref } from "vue";
|
|
5
|
+
import { useCategory, useShopwareContext } from "#imports";
|
|
6
|
+
import type { Schemas, operations } from "#shopware";
|
|
7
|
+
|
|
8
|
+
function isObject<T>(item: T): boolean {
|
|
9
|
+
return item && typeof item === "object" && !Array.isArray(item);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function merge<T extends { [key in keyof T]: unknown }>(
|
|
13
|
+
target: T,
|
|
14
|
+
...sources: T[]
|
|
15
|
+
): T {
|
|
16
|
+
if (!sources.length) return target;
|
|
17
|
+
const source = sources.shift();
|
|
18
|
+
|
|
19
|
+
if (source === undefined) {
|
|
20
|
+
return target;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (isObject(target) && isObject(source)) {
|
|
24
|
+
for (const key in source) {
|
|
25
|
+
if (isObject(source[key])) {
|
|
26
|
+
if (!target[key]) Object.assign(target, { [key]: {} });
|
|
27
|
+
merge(target[key], source[key]);
|
|
28
|
+
} else {
|
|
29
|
+
Object.assign(target, { [key]: source[key] });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return merge(target, ...sources);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type ListingType = "productSearchListing" | "categoryListing";
|
|
38
|
+
|
|
39
|
+
export type ShortcutFilterParam<
|
|
40
|
+
T extends
|
|
41
|
+
keyof Schemas["ProductListingCriteria"] = keyof Schemas["ProductListingCriteria"],
|
|
42
|
+
> = {
|
|
43
|
+
code: T;
|
|
44
|
+
value: Schemas["ProductListingCriteria"][T];
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type UseListingReturn = {
|
|
48
|
+
/**
|
|
49
|
+
* Listing that is currently set
|
|
50
|
+
* {@link ListingResult} object
|
|
51
|
+
*/
|
|
52
|
+
getInitialListing: ComputedRef<Schemas["ProductListingResult"] | null>;
|
|
53
|
+
/**
|
|
54
|
+
* Sets the initial listing - available synchronously
|
|
55
|
+
* @param {@link initialListing} - initial listing to set
|
|
56
|
+
* @returns
|
|
57
|
+
*/
|
|
58
|
+
setInitialListing(
|
|
59
|
+
initialListing: Schemas["ProductListingResult"],
|
|
60
|
+
): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* @deprecated - use `search` instead
|
|
63
|
+
* Searches for the listing based on the criteria
|
|
64
|
+
* @param criteria {@link Schemas['Criteria']}
|
|
65
|
+
* @returns
|
|
66
|
+
*/
|
|
67
|
+
initSearch(
|
|
68
|
+
criteria: operations["searchPage post /search"]["body"],
|
|
69
|
+
): Promise<Schemas["ProductListingResult"]>;
|
|
70
|
+
/**
|
|
71
|
+
* Searches for the listing based on the criteria
|
|
72
|
+
* @param criteria
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
search(
|
|
76
|
+
criteria:
|
|
77
|
+
| operations["readProductListing post /product-listing/{categoryId}"]["body"]
|
|
78
|
+
| operations["searchPage post /search"]["body"],
|
|
79
|
+
): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* Loads more (next page) elements to the listing
|
|
82
|
+
*/
|
|
83
|
+
loadMore(
|
|
84
|
+
criteria?: operations["searchPage post /search"]["body"],
|
|
85
|
+
): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* Listing that is currently set
|
|
88
|
+
*/
|
|
89
|
+
getCurrentListing: ComputedRef<Schemas["ProductListingResult"] | null>;
|
|
90
|
+
/**
|
|
91
|
+
* Listing elements ({@link Product}) that are currently set
|
|
92
|
+
*/
|
|
93
|
+
getElements: ComputedRef<Schemas["ProductListingResult"]["elements"]>;
|
|
94
|
+
/**
|
|
95
|
+
* Available sorting orders
|
|
96
|
+
*/
|
|
97
|
+
getSortingOrders: ComputedRef<
|
|
98
|
+
Schemas["ProductSorting"][] | { key: string; label: string }[] | undefined
|
|
99
|
+
>;
|
|
100
|
+
/**
|
|
101
|
+
* Current sorting order
|
|
102
|
+
*/
|
|
103
|
+
getCurrentSortingOrder: ComputedRef<string | undefined>;
|
|
104
|
+
/**
|
|
105
|
+
* Changes the current sorting order
|
|
106
|
+
* @param order - i.e. "name-asc"
|
|
107
|
+
* @returns
|
|
108
|
+
*/
|
|
109
|
+
changeCurrentSortingOrder(
|
|
110
|
+
order: string,
|
|
111
|
+
query?: operations["searchPage post /search"]["body"],
|
|
112
|
+
): Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* Current page number
|
|
115
|
+
*/
|
|
116
|
+
getCurrentPage: ComputedRef<number>;
|
|
117
|
+
/**
|
|
118
|
+
* Changes the current page number
|
|
119
|
+
* @param pageNumber - page number to change to
|
|
120
|
+
* @returns
|
|
121
|
+
*/
|
|
122
|
+
changeCurrentPage(
|
|
123
|
+
page: number,
|
|
124
|
+
query?: operations["searchPage post /search"]["body"],
|
|
125
|
+
): Promise<void>;
|
|
126
|
+
/**
|
|
127
|
+
* Total number of elements found for the current search criteria
|
|
128
|
+
*/
|
|
129
|
+
getTotal: ComputedRef<number>;
|
|
130
|
+
/**
|
|
131
|
+
* Total number of pages found for the current search criteria
|
|
132
|
+
*/
|
|
133
|
+
getTotalPagesCount: ComputedRef<number>;
|
|
134
|
+
/**
|
|
135
|
+
* Number of elements per page
|
|
136
|
+
*/
|
|
137
|
+
getLimit: ComputedRef<number>;
|
|
138
|
+
/**
|
|
139
|
+
* Initial filters
|
|
140
|
+
*/
|
|
141
|
+
getInitialFilters: ComputedRef<ReturnType<typeof getListingFilters>>;
|
|
142
|
+
/**
|
|
143
|
+
* All available filters
|
|
144
|
+
*/
|
|
145
|
+
getAvailableFilters: ComputedRef<ReturnType<typeof getListingFilters>>;
|
|
146
|
+
/**
|
|
147
|
+
* Filters that are currently set
|
|
148
|
+
*/
|
|
149
|
+
getCurrentFilters: ComputedRef<
|
|
150
|
+
Schemas["ProductListingResult"]["currentFilters"]
|
|
151
|
+
>;
|
|
152
|
+
/**
|
|
153
|
+
* Sets the filters to be applied for the listing
|
|
154
|
+
* @param filters
|
|
155
|
+
* @returns
|
|
156
|
+
*/
|
|
157
|
+
setCurrentFilters(filters: ShortcutFilterParam[]): Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Indicates if the listing is being fetched
|
|
160
|
+
*/
|
|
161
|
+
loading: ComputedRef<boolean>;
|
|
162
|
+
/**
|
|
163
|
+
* Indicates if the listing is being fetched via `loadMore` method
|
|
164
|
+
*/
|
|
165
|
+
loadingMore: ComputedRef<boolean>;
|
|
166
|
+
/**
|
|
167
|
+
* Resets the filters - clears the current filters
|
|
168
|
+
*/
|
|
169
|
+
resetFilters(): Promise<void>;
|
|
170
|
+
/**
|
|
171
|
+
* Change selected filters to the query object
|
|
172
|
+
*/
|
|
173
|
+
filtersToQuery(
|
|
174
|
+
filters: Schemas["ProductListingCriteria"],
|
|
175
|
+
): Record<string, unknown>;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @public
|
|
180
|
+
* @category Product
|
|
181
|
+
*/
|
|
182
|
+
export function useListing(params?: {
|
|
183
|
+
listingType: ListingType;
|
|
184
|
+
categoryId?: string;
|
|
185
|
+
defaultSearchCriteria?: operations["searchPage post /search"]["body"];
|
|
186
|
+
}): UseListingReturn {
|
|
187
|
+
const listingType = params?.listingType || "categoryListing";
|
|
188
|
+
let categoryId = params?.categoryId || null;
|
|
189
|
+
|
|
190
|
+
// const { getDefaults } = useDefaults({ defaultsKey: contextName });
|
|
191
|
+
const { apiClient } = useShopwareContext();
|
|
192
|
+
|
|
193
|
+
let searchMethod: typeof listingType extends "productSearchListing"
|
|
194
|
+
? (
|
|
195
|
+
searchParams: operations["readProductListing post /product-listing/{categoryId}"]["body"],
|
|
196
|
+
) => Promise<Schemas["ProductListingResult"]>
|
|
197
|
+
: (
|
|
198
|
+
searchParams: operations["searchPage post /search"]["body"],
|
|
199
|
+
) => Promise<Schemas["ProductListingResult"]>;
|
|
200
|
+
|
|
201
|
+
if (listingType === "productSearchListing") {
|
|
202
|
+
searchMethod = async (
|
|
203
|
+
searchCriteria: operations["searchPage post /search"]["body"],
|
|
204
|
+
) => {
|
|
205
|
+
const { data } = await apiClient.invoke("searchPage post /search", {
|
|
206
|
+
headers: {
|
|
207
|
+
"sw-include-seo-urls": true,
|
|
208
|
+
},
|
|
209
|
+
body: searchCriteria,
|
|
210
|
+
});
|
|
211
|
+
return data;
|
|
212
|
+
};
|
|
213
|
+
} else {
|
|
214
|
+
if (!categoryId) {
|
|
215
|
+
const { category } = useCategory();
|
|
216
|
+
categoryId = category.value?.id;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
searchMethod = async (
|
|
220
|
+
searchCriteria: operations["readProductListing post /product-listing/{categoryId}"]["body"],
|
|
221
|
+
) => {
|
|
222
|
+
const { data } = await apiClient.invoke(
|
|
223
|
+
"readProductListing post /product-listing/{categoryId}",
|
|
224
|
+
{
|
|
225
|
+
headers: {
|
|
226
|
+
"sw-include-seo-urls": true,
|
|
227
|
+
},
|
|
228
|
+
pathParams: {
|
|
229
|
+
categoryId: categoryId as string, // null exception in useCategory,
|
|
230
|
+
},
|
|
231
|
+
body: searchCriteria,
|
|
232
|
+
},
|
|
233
|
+
);
|
|
234
|
+
return data;
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return createListingComposable({
|
|
239
|
+
listingKey: listingType,
|
|
240
|
+
searchMethod,
|
|
241
|
+
searchDefaults:
|
|
242
|
+
params?.defaultSearchCriteria ||
|
|
243
|
+
({} as operations["searchPage post /search"]["body"]), //getDefaults(),
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const [_createCategoryListingContext, _categoryListingContext] =
|
|
248
|
+
createInjectionState(
|
|
249
|
+
() => {
|
|
250
|
+
return useListing({ listingType: "categoryListing" });
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
injectionKey: "categoryListing",
|
|
254
|
+
},
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
export const createCategoryListingContext = _createCategoryListingContext;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Temporary workaround over `useListing` to support shared data. This composable API will change in the future.
|
|
261
|
+
*
|
|
262
|
+
* You need to call `createCategoryListingContext` before this composable.
|
|
263
|
+
*/
|
|
264
|
+
export const useCategoryListing = () => {
|
|
265
|
+
const listingContext = _categoryListingContext();
|
|
266
|
+
|
|
267
|
+
if (!listingContext) {
|
|
268
|
+
throw new Error(
|
|
269
|
+
"[useCategoryListing] Please call `createCategoryListingContext` on the appropriate parent component",
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return listingContext;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Temporary workaround over `useListing` to support shared data. This composable API will change in the future.
|
|
278
|
+
*/
|
|
279
|
+
export const useProductSearchListing = createSharedComposable(() =>
|
|
280
|
+
useListing({ listingType: "productSearchListing" }),
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Factory to create your own listing.
|
|
285
|
+
*
|
|
286
|
+
* By default you can use useListing composable, which provides you predefined listings for category(cms) listing and product search listing.
|
|
287
|
+
* Using factory you can provide our own compatible search method and use it for example for creating listing of orders in my account.
|
|
288
|
+
*
|
|
289
|
+
* @public
|
|
290
|
+
*/
|
|
291
|
+
export function createListingComposable({
|
|
292
|
+
searchMethod,
|
|
293
|
+
searchDefaults,
|
|
294
|
+
listingKey,
|
|
295
|
+
}: {
|
|
296
|
+
searchMethod(
|
|
297
|
+
searchParams:
|
|
298
|
+
| operations["readProductListing post /product-listing/{categoryId}"]["body"]
|
|
299
|
+
| operations["searchPage post /search"]["body"],
|
|
300
|
+
): Promise<Schemas["ProductListingResult"]>;
|
|
301
|
+
searchDefaults: operations["searchPage post /search"]["body"];
|
|
302
|
+
listingKey: string;
|
|
303
|
+
}): UseListingReturn {
|
|
304
|
+
// const COMPOSABLE_NAME = "createListingComposable";
|
|
305
|
+
// const contextName = COMPOSABLE_NAME;
|
|
306
|
+
|
|
307
|
+
// const router = useRouter();
|
|
308
|
+
|
|
309
|
+
// Handle CMS context to be able to show different breadcrumbs for different CMS pages.
|
|
310
|
+
// const { isVueComponent } = useVueContext();
|
|
311
|
+
// const cmsContext = isVueComponent && inject("swCmsContext", null);
|
|
312
|
+
// const cacheKey = cmsContext
|
|
313
|
+
// ? `${contextName}(cms-${cmsContext})`
|
|
314
|
+
// : contextName;
|
|
315
|
+
|
|
316
|
+
const loading = ref(false);
|
|
317
|
+
const loadingMore = ref(false);
|
|
318
|
+
|
|
319
|
+
// const { sharedRef } = useSharedState();
|
|
320
|
+
const _storeInitialListing = inject<
|
|
321
|
+
Ref<Schemas["ProductListingResult"] | null>
|
|
322
|
+
>(`useListingInitial-${listingKey}`, ref(null));
|
|
323
|
+
provide(`useListingInitial-${listingKey}`, _storeInitialListing);
|
|
324
|
+
// const _storeInitialListing = sharedRef<Schemas["ProductListingResult"]>(
|
|
325
|
+
// `${cacheKey}-initialListing-${listingKey}`
|
|
326
|
+
// );
|
|
327
|
+
// const _storeAppliedListing = sharedRef<Partial<Schemas["ProductListingResult"]>>(
|
|
328
|
+
// `${cacheKey}-appliedListing-${listingKey}`
|
|
329
|
+
// );
|
|
330
|
+
const _storeAppliedListing = inject<
|
|
331
|
+
Ref<Schemas["ProductListingResult"] | null>
|
|
332
|
+
>(`useListingApplied-${listingKey}`, ref(null));
|
|
333
|
+
provide(`useListingApplied-${listingKey}`, _storeAppliedListing);
|
|
334
|
+
|
|
335
|
+
const getInitialListing = computed(() => _storeInitialListing.value);
|
|
336
|
+
const setInitialListing = async (
|
|
337
|
+
initialListing: Schemas["ProductListingResult"],
|
|
338
|
+
) => {
|
|
339
|
+
_storeInitialListing.value = initialListing;
|
|
340
|
+
_storeAppliedListing.value = null;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const initSearch = async (
|
|
344
|
+
criteria: operations["searchPage post /search"]["body"],
|
|
345
|
+
): Promise<Schemas["ProductListingResult"]> => {
|
|
346
|
+
loading.value = true;
|
|
347
|
+
try {
|
|
348
|
+
const searchCriteria = merge(
|
|
349
|
+
{} as operations["searchPage post /search"]["body"],
|
|
350
|
+
searchDefaults,
|
|
351
|
+
criteria,
|
|
352
|
+
);
|
|
353
|
+
const result = await searchMethod(searchCriteria);
|
|
354
|
+
return result;
|
|
355
|
+
} finally {
|
|
356
|
+
loading.value = false;
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
async function search(
|
|
361
|
+
criteria: operations["searchPage post /search"]["body"],
|
|
362
|
+
) {
|
|
363
|
+
loading.value = true;
|
|
364
|
+
try {
|
|
365
|
+
const searchCriteria = merge(
|
|
366
|
+
{} as operations["searchPage post /search"]["body"],
|
|
367
|
+
searchDefaults,
|
|
368
|
+
criteria,
|
|
369
|
+
);
|
|
370
|
+
const result = await searchMethod(searchCriteria);
|
|
371
|
+
|
|
372
|
+
_storeAppliedListing.value = result;
|
|
373
|
+
} finally {
|
|
374
|
+
loading.value = false;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const loadMore = async (
|
|
379
|
+
criteria?: operations["searchPage post /search"]["body"],
|
|
380
|
+
): Promise<void> => {
|
|
381
|
+
loadingMore.value = true;
|
|
382
|
+
try {
|
|
383
|
+
const q = criteria
|
|
384
|
+
? criteria
|
|
385
|
+
: {
|
|
386
|
+
// ...router.currentRoute.query,
|
|
387
|
+
p: getCurrentPage.value + 1,
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
const searchCriteria = merge(
|
|
391
|
+
{} as operations["searchPage post /search"]["body"],
|
|
392
|
+
searchDefaults,
|
|
393
|
+
q,
|
|
394
|
+
) as operations["searchPage post /search"]["body"];
|
|
395
|
+
const result = await searchMethod(searchCriteria);
|
|
396
|
+
_storeAppliedListing.value = {
|
|
397
|
+
...(getCurrentListing.value || {}),
|
|
398
|
+
page: result.page,
|
|
399
|
+
elements: [
|
|
400
|
+
...(getCurrentListing.value?.elements || []),
|
|
401
|
+
...(result.elements ?? []),
|
|
402
|
+
],
|
|
403
|
+
} as Schemas["ProductListingResult"];
|
|
404
|
+
} finally {
|
|
405
|
+
loadingMore.value = false;
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
const getCurrentListing = computed(() => {
|
|
410
|
+
return _storeAppliedListing.value || getInitialListing.value;
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
const getElements = computed(() => {
|
|
414
|
+
return getCurrentListing.value?.elements || [];
|
|
415
|
+
});
|
|
416
|
+
const getTotal = computed(() => {
|
|
417
|
+
return getCurrentListing.value?.total || 0;
|
|
418
|
+
});
|
|
419
|
+
const getLimit = computed(() => {
|
|
420
|
+
return getCurrentListing.value?.limit || searchDefaults?.limit || 10;
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
const getTotalPagesCount = computed(() =>
|
|
424
|
+
Math.ceil(getTotal.value / getLimit.value),
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
const getSortingOrders = computed(() => {
|
|
428
|
+
return getCurrentListing.value?.availableSortings;
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
const getCurrentSortingOrder = computed(
|
|
432
|
+
() => getCurrentListing.value?.sorting,
|
|
433
|
+
);
|
|
434
|
+
async function changeCurrentSortingOrder(
|
|
435
|
+
order: string,
|
|
436
|
+
query?: operations["searchPage post /search"]["body"],
|
|
437
|
+
) {
|
|
438
|
+
await search(
|
|
439
|
+
Object.assign(
|
|
440
|
+
{
|
|
441
|
+
order,
|
|
442
|
+
},
|
|
443
|
+
query,
|
|
444
|
+
),
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const getCurrentPage = computed(() => getCurrentListing.value?.page || 1);
|
|
449
|
+
|
|
450
|
+
const changeCurrentPage = async (
|
|
451
|
+
page: number,
|
|
452
|
+
query?: operations["searchPage post /search"]["body"],
|
|
453
|
+
) => {
|
|
454
|
+
await search(
|
|
455
|
+
Object.assign(
|
|
456
|
+
{
|
|
457
|
+
page,
|
|
458
|
+
},
|
|
459
|
+
query,
|
|
460
|
+
),
|
|
461
|
+
);
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
const getInitialFilters = computed(() => {
|
|
465
|
+
return getListingFilters(getInitialListing.value?.aggregations);
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
const getAvailableFilters = computed(() => {
|
|
469
|
+
return getListingFilters(
|
|
470
|
+
_storeAppliedListing.value?.aggregations ||
|
|
471
|
+
getCurrentListing.value?.aggregations,
|
|
472
|
+
);
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
const getCurrentFilters = computed(() => {
|
|
476
|
+
// const currentFiltersResult: Schemas["ProductListingResult"]["currentFilters"] =
|
|
477
|
+
// {};
|
|
478
|
+
// const currentFilters = {
|
|
479
|
+
// ...getCurrentListing.value?.currentFilters,
|
|
480
|
+
// // ...router.currentRoute.query,
|
|
481
|
+
// };
|
|
482
|
+
// Object.keys(currentFilters).forEach(
|
|
483
|
+
// (objectKey: keyof Schemas["ProductListingResult"]["currentFilters"]) => {
|
|
484
|
+
// if (!currentFilters[objectKey]) return;
|
|
485
|
+
// if (objectKey === "navigationId") return;
|
|
486
|
+
// if (objectKey === "price") {
|
|
487
|
+
// if (currentFilters.price?.min)
|
|
488
|
+
// currentFiltersResult.price.min = currentFilters[objectKey].min;
|
|
489
|
+
// if (currentFilters[objectKey].max)
|
|
490
|
+
// currentFiltersResult["max-price"] = currentFilters[objectKey].max;
|
|
491
|
+
// return;
|
|
492
|
+
// }
|
|
493
|
+
// if (objectKey === "p") return;
|
|
494
|
+
// currentFiltersResult[objectKey] = currentFilters[objectKey];
|
|
495
|
+
// },
|
|
496
|
+
// );
|
|
497
|
+
return getCurrentListing.value
|
|
498
|
+
?.currentFilters as Schemas["ProductListingResult"]["currentFilters"];
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
// this function sets the current filters as shortcut filters @see https://shopware.stoplight.io/docs/store-api/b56ebe18277c6-searching-for-products#product-listing-criteria
|
|
502
|
+
// the downside is that this does not filter the aggregations, so the aggregations are not reduced by the filter (!)
|
|
503
|
+
const setCurrentFilters = (filters: ShortcutFilterParam[]) => {
|
|
504
|
+
const newFilters = {};
|
|
505
|
+
for (const filter of filters) {
|
|
506
|
+
Object.assign(newFilters, { [filter.code]: filter.value });
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
const appliedFilters = Object.assign(
|
|
510
|
+
{},
|
|
511
|
+
getCurrentFilters.value,
|
|
512
|
+
{
|
|
513
|
+
query: getCurrentFilters.value?.search,
|
|
514
|
+
manufacturer: getCurrentFilters.value?.manufacturer?.join("|"),
|
|
515
|
+
properties: getCurrentFilters.value?.properties?.join("|"),
|
|
516
|
+
},
|
|
517
|
+
{ ...newFilters },
|
|
518
|
+
);
|
|
519
|
+
|
|
520
|
+
if (_storeAppliedListing.value) {
|
|
521
|
+
_storeAppliedListing.value.currentFilters = {
|
|
522
|
+
...appliedFilters,
|
|
523
|
+
manufacturer: appliedFilters.manufacturer?.split("|"),
|
|
524
|
+
properties: appliedFilters.properties?.split("|"),
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
return search(appliedFilters);
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
const resetFilters = () => {
|
|
532
|
+
const defaultFilters = Object.assign(
|
|
533
|
+
{
|
|
534
|
+
manufacturer: [],
|
|
535
|
+
properties: [],
|
|
536
|
+
price: { min: 0, max: 0 },
|
|
537
|
+
search: getCurrentFilters.value?.search,
|
|
538
|
+
},
|
|
539
|
+
searchDefaults,
|
|
540
|
+
);
|
|
541
|
+
|
|
542
|
+
if (_storeAppliedListing.value) {
|
|
543
|
+
_storeAppliedListing.value.currentFilters =
|
|
544
|
+
defaultFilters as unknown as Schemas["ProductListingResult"]["currentFilters"];
|
|
545
|
+
}
|
|
546
|
+
return search({ search: getCurrentFilters.value?.search || "" });
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
const filtersToQuery = (filters: Schemas["ProductListingCriteria"]) => {
|
|
550
|
+
const queryObject: Record<string, unknown> = {};
|
|
551
|
+
|
|
552
|
+
for (const filter in filters) {
|
|
553
|
+
const currentFilter =
|
|
554
|
+
filters[filter as keyof Schemas["ProductListingCriteria"]];
|
|
555
|
+
if (currentFilter) {
|
|
556
|
+
if (Array.isArray(currentFilter) && currentFilter.length) {
|
|
557
|
+
queryObject[filter] = currentFilter.join("|");
|
|
558
|
+
} else if (!Array.isArray(currentFilter)) {
|
|
559
|
+
queryObject[filter] = currentFilter;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
return queryObject;
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
return {
|
|
568
|
+
changeCurrentPage,
|
|
569
|
+
changeCurrentSortingOrder,
|
|
570
|
+
filtersToQuery,
|
|
571
|
+
getAvailableFilters,
|
|
572
|
+
getCurrentFilters,
|
|
573
|
+
getCurrentListing,
|
|
574
|
+
getCurrentPage,
|
|
575
|
+
getCurrentSortingOrder,
|
|
576
|
+
getElements,
|
|
577
|
+
getInitialFilters,
|
|
578
|
+
getInitialListing,
|
|
579
|
+
getLimit,
|
|
580
|
+
getSortingOrders,
|
|
581
|
+
getTotal,
|
|
582
|
+
getTotalPagesCount,
|
|
583
|
+
initSearch,
|
|
584
|
+
loadMore,
|
|
585
|
+
loading: computed(() => loading.value),
|
|
586
|
+
loadingMore: computed(() => loadingMore.value),
|
|
587
|
+
resetFilters,
|
|
588
|
+
search,
|
|
589
|
+
setCurrentFilters,
|
|
590
|
+
setInitialListing,
|
|
591
|
+
};
|
|
592
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { useSetup } from "../_test";
|
|
3
|
+
import { useLocalWishlist } from "./useLocalWishlist";
|
|
4
|
+
|
|
5
|
+
describe("useLocalWishlist", () => {
|
|
6
|
+
describe("methods", () => {
|
|
7
|
+
describe("should add product to the wishlist", () => {
|
|
8
|
+
it("wishlist add product", () => {
|
|
9
|
+
const { vm } = useSetup(useLocalWishlist);
|
|
10
|
+
vm.addToWishlist("some-id");
|
|
11
|
+
expect(vm.items.length).toBe(1);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("removeFromWishlist", () => {
|
|
16
|
+
it("should remove product from the wishlist", () => {
|
|
17
|
+
const { vm } = useSetup(useLocalWishlist);
|
|
18
|
+
vm.removeFromWishlist("some-id");
|
|
19
|
+
expect(vm.items.length).toBe(0);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("clearWishlist", () => {
|
|
24
|
+
it("should clear the wishlist", () => {
|
|
25
|
+
const { vm } = useSetup(useLocalWishlist);
|
|
26
|
+
vm.addToWishlist("some-id");
|
|
27
|
+
expect(vm.count).toBe(1);
|
|
28
|
+
vm.clearWishlist();
|
|
29
|
+
expect(vm.count).toBe(0);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("getWishlistProducts", () => {
|
|
34
|
+
it("should get the wishlist products", () => {
|
|
35
|
+
const { vm } = useSetup(useLocalWishlist);
|
|
36
|
+
vm.clearWishlist();
|
|
37
|
+
vm.getWishlistProducts();
|
|
38
|
+
expect(vm.count).toBe(0);
|
|
39
|
+
});
|
|
40
|
+
it("should get the wishlist products after adding product", () => {
|
|
41
|
+
const { vm } = useSetup(useLocalWishlist);
|
|
42
|
+
vm.addToWishlist("some-id");
|
|
43
|
+
vm.getWishlistProducts();
|
|
44
|
+
expect(vm.count).toBe(1);
|
|
45
|
+
});
|
|
46
|
+
it("getWishlistProducts with items localStorage", () => {
|
|
47
|
+
const { vm } = useSetup(useLocalWishlist);
|
|
48
|
+
localStorage.removeItem("sw-wishlist-items");
|
|
49
|
+
vm.getWishlistProducts();
|
|
50
|
+
expect(vm.count).toBe(1);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|