@shopbite-de/storefront 1.21.2 → 1.22.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/app/components/Category/Listing.vue +1 -0
- package/app/components/Category/SearchInput.vue +33 -0
- package/app/composables/useListingSearch.ts +66 -0
- package/app/composables/useTrackEvent.ts +11 -0
- package/app/pages/suche.vue +177 -0
- package/nuxt.config.ts +1 -0
- package/package.json +1 -1
- package/server/api/search.get.ts +99 -0
|
@@ -125,6 +125,7 @@ const moreThanOneFilterAndOption = computed<boolean>(
|
|
|
125
125
|
<div>
|
|
126
126
|
<Breadcrumb :category-id="category?.id" />
|
|
127
127
|
<CategoryHeader v-if="category" :category="category" />
|
|
128
|
+
<CategorySearchInput class="mb-4 grow flex" />
|
|
128
129
|
<div class="flex flex-row justify-between gap-4 mb-4">
|
|
129
130
|
<UBadge variant="subtle" :label="`${elements.length} Produkte`" />
|
|
130
131
|
<USelect
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
const router = useRouter();
|
|
3
|
+
|
|
4
|
+
const query = ref("");
|
|
5
|
+
|
|
6
|
+
function submit() {
|
|
7
|
+
const term = query.value.trim();
|
|
8
|
+
if (!term) return;
|
|
9
|
+
router.push({ path: "/suche", query: { q: term } });
|
|
10
|
+
}
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<template>
|
|
14
|
+
<UInput
|
|
15
|
+
v-model="query"
|
|
16
|
+
name="search"
|
|
17
|
+
size="md"
|
|
18
|
+
icon="i-lucide-search"
|
|
19
|
+
placeholder="Produkte suchen..."
|
|
20
|
+
@keyup.enter="submit"
|
|
21
|
+
>
|
|
22
|
+
<template v-if="query" #trailing>
|
|
23
|
+
<UButton
|
|
24
|
+
color="neutral"
|
|
25
|
+
variant="link"
|
|
26
|
+
size="sm"
|
|
27
|
+
icon="i-lucide-circle-x"
|
|
28
|
+
aria-label="Suche leeren"
|
|
29
|
+
@click="query = ''"
|
|
30
|
+
/>
|
|
31
|
+
</template>
|
|
32
|
+
</UInput>
|
|
33
|
+
</template>
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Schemas } from "#shopware";
|
|
2
|
+
import type { NitroFetchRequest } from "nitropack";
|
|
3
|
+
|
|
4
|
+
export function useListingSearch(initialQuery: string) {
|
|
5
|
+
const nuxtApp = useNuxtApp();
|
|
6
|
+
const loading = ref(false);
|
|
7
|
+
let currentController: AbortController | null = null;
|
|
8
|
+
|
|
9
|
+
async function fetchSearch(
|
|
10
|
+
query: string,
|
|
11
|
+
order?: string,
|
|
12
|
+
signal?: AbortSignal,
|
|
13
|
+
): Promise<Schemas["ProductListingResult"]> {
|
|
14
|
+
return await $fetch("/api/search" as NitroFetchRequest, {
|
|
15
|
+
query: {
|
|
16
|
+
query,
|
|
17
|
+
...(order && { order }),
|
|
18
|
+
},
|
|
19
|
+
signal,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const hasInitialQuery = initialQuery.trim().length > 0;
|
|
24
|
+
|
|
25
|
+
const { data: listing, pending } = useLazyAsyncData(
|
|
26
|
+
`search-${initialQuery}`,
|
|
27
|
+
() => fetchSearch(initialQuery),
|
|
28
|
+
{
|
|
29
|
+
immediate: hasInitialQuery,
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const showSkeleton = computed(() => pending.value && !nuxtApp.isHydrating);
|
|
34
|
+
const elements = computed(() => listing.value?.elements ?? []);
|
|
35
|
+
const sortingOrders = computed(() => listing.value?.availableSortings);
|
|
36
|
+
const currentSortingOrder = computed(() => listing.value?.sorting);
|
|
37
|
+
|
|
38
|
+
async function applySearch(query: string, order?: string) {
|
|
39
|
+
currentController?.abort();
|
|
40
|
+
currentController = new AbortController();
|
|
41
|
+
const { signal } = currentController;
|
|
42
|
+
|
|
43
|
+
loading.value = true;
|
|
44
|
+
try {
|
|
45
|
+
listing.value = await fetchSearch(query, order, signal);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
if ((e as { name?: string }).name !== "AbortError") throw e;
|
|
48
|
+
} finally {
|
|
49
|
+
loading.value = false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function changeSorting(order: string, query: string) {
|
|
54
|
+
await applySearch(query, order);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
showSkeleton,
|
|
59
|
+
loading,
|
|
60
|
+
elements,
|
|
61
|
+
sortingOrders,
|
|
62
|
+
currentSortingOrder,
|
|
63
|
+
applySearch,
|
|
64
|
+
changeSorting,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -53,10 +53,21 @@ export function useTrackEvent() {
|
|
|
53
53
|
]);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
function trackSearch(term: string, productNumbers: string[]) {
|
|
57
|
+
proxy._paq.push([
|
|
58
|
+
"trackEvent",
|
|
59
|
+
"Search",
|
|
60
|
+
term,
|
|
61
|
+
productNumbers.join(","),
|
|
62
|
+
productNumbers.length,
|
|
63
|
+
]);
|
|
64
|
+
}
|
|
65
|
+
|
|
56
66
|
return {
|
|
57
67
|
trackProductView,
|
|
58
68
|
trackOrder,
|
|
59
69
|
trackAddToWishlist,
|
|
60
70
|
trackAddToCart,
|
|
71
|
+
trackSearch,
|
|
61
72
|
};
|
|
62
73
|
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import type { Schemas } from "#shopware";
|
|
3
|
+
import type { NitroFetchRequest } from "nitropack";
|
|
4
|
+
|
|
5
|
+
definePageMeta({
|
|
6
|
+
layout: "listing",
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
const route = useRoute();
|
|
10
|
+
const router = useRouter();
|
|
11
|
+
const { searchFallbackCategoryId } = useRuntimeConfig().public.shopBite;
|
|
12
|
+
|
|
13
|
+
const searchQuery = computed(() => String(route.query.q ?? "").trim());
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
showSkeleton,
|
|
17
|
+
loading,
|
|
18
|
+
elements,
|
|
19
|
+
sortingOrders,
|
|
20
|
+
currentSortingOrder,
|
|
21
|
+
applySearch,
|
|
22
|
+
changeSorting,
|
|
23
|
+
} = useListingSearch(searchQuery.value);
|
|
24
|
+
|
|
25
|
+
const { trackSearch } = useTrackEvent();
|
|
26
|
+
|
|
27
|
+
// Re-fetch when the URL query changes (e.g. user searches again from this page)
|
|
28
|
+
watch(searchQuery, (q) => applySearch(q));
|
|
29
|
+
|
|
30
|
+
// Fire a Matomo event whenever results settle for a given search term
|
|
31
|
+
watch(elements, (products) => {
|
|
32
|
+
if (!searchQuery.value || showSkeleton.value) return;
|
|
33
|
+
trackSearch(
|
|
34
|
+
searchQuery.value,
|
|
35
|
+
products.map((p) => p.productNumber),
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Fallback: fetch a curated category listing when search returns no results.
|
|
40
|
+
// The category should have a dynamic product group assigned in Shopware.
|
|
41
|
+
const showFallback = computed(
|
|
42
|
+
() =>
|
|
43
|
+
!showSkeleton.value &&
|
|
44
|
+
!loading.value &&
|
|
45
|
+
elements.value.length === 0 &&
|
|
46
|
+
!!searchFallbackCategoryId,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const { data: fallbackProducts, refresh: refreshFallback } = useLazyAsyncData<
|
|
50
|
+
Schemas["Product"][]
|
|
51
|
+
>(
|
|
52
|
+
"search-fallback-listing",
|
|
53
|
+
async () => {
|
|
54
|
+
const result = await $fetch(
|
|
55
|
+
`/api/listing/${searchFallbackCategoryId}` as NitroFetchRequest,
|
|
56
|
+
);
|
|
57
|
+
return (result as { elements?: Schemas["Product"][] }).elements ?? [];
|
|
58
|
+
},
|
|
59
|
+
{ immediate: false },
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
watch(showFallback, (visible) => {
|
|
63
|
+
if (visible && !fallbackProducts.value) {
|
|
64
|
+
refreshFallback();
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const currentSorting = ref(currentSortingOrder.value ?? "Sortieren");
|
|
69
|
+
|
|
70
|
+
watch(currentSortingOrder, (val) => {
|
|
71
|
+
if (val) currentSorting.value = val;
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
watch(currentSorting, async (val) => {
|
|
75
|
+
if (val === currentSortingOrder.value) return;
|
|
76
|
+
await changeSorting(val, searchQuery.value);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const searchInput = ref(searchQuery.value);
|
|
80
|
+
|
|
81
|
+
function submitSearch() {
|
|
82
|
+
const term = searchInput.value.trim();
|
|
83
|
+
if (!term) return;
|
|
84
|
+
router.push({ path: "/suche", query: { q: term } });
|
|
85
|
+
}
|
|
86
|
+
</script>
|
|
87
|
+
|
|
88
|
+
<template>
|
|
89
|
+
<UContainer>
|
|
90
|
+
<UPage>
|
|
91
|
+
<UPageBody>
|
|
92
|
+
<div class="flex flex-col gap-4">
|
|
93
|
+
<div class="flex flex-col sm:flex-row gap-4">
|
|
94
|
+
<UInput
|
|
95
|
+
v-model="searchInput"
|
|
96
|
+
name="search"
|
|
97
|
+
class="flex-1"
|
|
98
|
+
size="md"
|
|
99
|
+
icon="i-lucide-search"
|
|
100
|
+
placeholder="Produkte suchen..."
|
|
101
|
+
@keyup.enter="submitSearch"
|
|
102
|
+
>
|
|
103
|
+
<template v-if="searchInput" #trailing>
|
|
104
|
+
<UButton
|
|
105
|
+
color="neutral"
|
|
106
|
+
variant="link"
|
|
107
|
+
size="sm"
|
|
108
|
+
icon="i-lucide-circle-x"
|
|
109
|
+
aria-label="Suche leeren"
|
|
110
|
+
@click="searchInput = ''"
|
|
111
|
+
/>
|
|
112
|
+
</template>
|
|
113
|
+
</UInput>
|
|
114
|
+
<USelect
|
|
115
|
+
v-model="currentSorting"
|
|
116
|
+
icon="i-lucide-arrow-down-wide-narrow"
|
|
117
|
+
value-key="key"
|
|
118
|
+
:items="sortingOrders"
|
|
119
|
+
placeholder="Sortierung"
|
|
120
|
+
/>
|
|
121
|
+
</div>
|
|
122
|
+
|
|
123
|
+
<div
|
|
124
|
+
v-if="searchQuery && !showSkeleton"
|
|
125
|
+
class="flex items-center gap-2"
|
|
126
|
+
>
|
|
127
|
+
<p class="text-sm text-muted">
|
|
128
|
+
Suchergebnisse für „{{ searchQuery }}"
|
|
129
|
+
</p>
|
|
130
|
+
<UBadge variant="subtle" :label="`${elements.length} Produkte`" />
|
|
131
|
+
</div>
|
|
132
|
+
|
|
133
|
+
<div
|
|
134
|
+
v-if="showSkeleton"
|
|
135
|
+
class="flex flex-col gap-4"
|
|
136
|
+
aria-busy="true"
|
|
137
|
+
aria-label="Produkte werden geladen"
|
|
138
|
+
>
|
|
139
|
+
<LazyProductCardSkeleton v-for="i in 6" :key="i" />
|
|
140
|
+
</div>
|
|
141
|
+
|
|
142
|
+
<div
|
|
143
|
+
v-else-if="elements.length > 0"
|
|
144
|
+
class="flex flex-col gap-4 transition-opacity duration-200"
|
|
145
|
+
:class="{ 'opacity-40 pointer-events-none': loading }"
|
|
146
|
+
>
|
|
147
|
+
<ProductCard
|
|
148
|
+
v-for="product in elements"
|
|
149
|
+
:key="product.id"
|
|
150
|
+
:product="product"
|
|
151
|
+
:with-favorite-button="true"
|
|
152
|
+
:with-add-to-cart-button="true"
|
|
153
|
+
/>
|
|
154
|
+
</div>
|
|
155
|
+
|
|
156
|
+
<template v-else-if="searchQuery && !showSkeleton && !loading">
|
|
157
|
+
<p class="text-muted">
|
|
158
|
+
Keine Produkte für „{{ searchQuery }}" gefunden.
|
|
159
|
+
</p>
|
|
160
|
+
<template v-if="showFallback && fallbackProducts?.length">
|
|
161
|
+
<p class="text-sm font-medium mt-2">Das könnte dir gefallen:</p>
|
|
162
|
+
<div class="flex flex-col gap-4">
|
|
163
|
+
<ProductCard
|
|
164
|
+
v-for="product in fallbackProducts"
|
|
165
|
+
:key="product.id"
|
|
166
|
+
:product="product"
|
|
167
|
+
:with-favorite-button="true"
|
|
168
|
+
:with-add-to-cart-button="true"
|
|
169
|
+
/>
|
|
170
|
+
</div>
|
|
171
|
+
</template>
|
|
172
|
+
</template>
|
|
173
|
+
</div>
|
|
174
|
+
</UPageBody>
|
|
175
|
+
</UPage>
|
|
176
|
+
</UContainer>
|
|
177
|
+
</template>
|
package/nuxt.config.ts
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { components } from "~~/api-types/storeApiTypes";
|
|
2
|
+
|
|
3
|
+
type Schemas = components["schemas"];
|
|
4
|
+
|
|
5
|
+
const BASE_CRITERIA = {
|
|
6
|
+
includes: {
|
|
7
|
+
product: [
|
|
8
|
+
"id",
|
|
9
|
+
"productNumber",
|
|
10
|
+
"name",
|
|
11
|
+
"description",
|
|
12
|
+
"calculatedPrice",
|
|
13
|
+
"translated",
|
|
14
|
+
"properties",
|
|
15
|
+
"propertyIds",
|
|
16
|
+
"sortedProperties",
|
|
17
|
+
"cover",
|
|
18
|
+
],
|
|
19
|
+
property: ["id", "name", "translated", "displayType", "options"],
|
|
20
|
+
property_group_option: [
|
|
21
|
+
"id",
|
|
22
|
+
"name",
|
|
23
|
+
"translated",
|
|
24
|
+
"group",
|
|
25
|
+
"media",
|
|
26
|
+
"mediaId",
|
|
27
|
+
],
|
|
28
|
+
product_option: ["id", "groupId", "name", "translated", "group"],
|
|
29
|
+
},
|
|
30
|
+
associations: {
|
|
31
|
+
cover: {
|
|
32
|
+
associations: {
|
|
33
|
+
media: {},
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
properties: {
|
|
37
|
+
associations: {
|
|
38
|
+
group: {},
|
|
39
|
+
media: {},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
limit: 100,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function sanitizeString(value: unknown, maxLength: number): string | undefined {
|
|
47
|
+
if (typeof value !== "string" || value === "") return undefined;
|
|
48
|
+
const trimmed = value.trim();
|
|
49
|
+
return trimmed.length <= maxLength ? trimmed : undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function sanitizePage(value: unknown): number | undefined {
|
|
53
|
+
const n = Number(value);
|
|
54
|
+
return Number.isInteger(n) && n >= 1 && n <= 1000 ? n : undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export default defineCachedEventHandler(
|
|
58
|
+
async (event) => {
|
|
59
|
+
const { endpoint, accessToken } = useRuntimeConfig().public.shopware;
|
|
60
|
+
const q = getQuery(event) as Record<string, unknown>;
|
|
61
|
+
|
|
62
|
+
const search = sanitizeString(q.query, 256);
|
|
63
|
+
const order = sanitizeString(q.order, 64);
|
|
64
|
+
const p = sanitizePage(q.p);
|
|
65
|
+
|
|
66
|
+
if (!search) {
|
|
67
|
+
return {
|
|
68
|
+
elements: [],
|
|
69
|
+
aggregations: {},
|
|
70
|
+
availableSortings: [],
|
|
71
|
+
} as unknown as Schemas["ProductListingResult"];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return await $fetch(`${endpoint}/search`, {
|
|
75
|
+
method: "POST",
|
|
76
|
+
headers: {
|
|
77
|
+
"sw-access-key": accessToken,
|
|
78
|
+
"sw-include-seo-urls": "true",
|
|
79
|
+
},
|
|
80
|
+
body: {
|
|
81
|
+
search,
|
|
82
|
+
...BASE_CRITERIA,
|
|
83
|
+
...(order && { order }),
|
|
84
|
+
...(p && { p }),
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
maxAge: useRuntimeConfig().public.shopBite.cacheTtl.listing,
|
|
90
|
+
name: "search",
|
|
91
|
+
getKey: (event) => {
|
|
92
|
+
const q = getQuery(event) as Record<string, unknown>;
|
|
93
|
+
const search = sanitizeString(q.query, 256) ?? "";
|
|
94
|
+
const order = sanitizeString(q.order, 64) ?? "";
|
|
95
|
+
const p = sanitizePage(q.p) ?? 1;
|
|
96
|
+
return [search, order, p].join("|");
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
);
|