@shopbite-de/storefront 2.0.3 → 2.1.1

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.
@@ -39,25 +39,43 @@ const open = defineModel<boolean>("open", { default: false });
39
39
  <div class="flex justify-center pt-2.5 md:hidden" aria-hidden="true">
40
40
  <span class="h-1 w-9 rounded-full bg-sb-line" />
41
41
  </div>
42
- <header class="flex items-start gap-3 px-5 pt-3 pb-2 md:pt-6">
42
+ <!-- A custom header (the product sheet's bon card) gets the full
43
+ width below a toolbar with the actions and the close button:
44
+ next to three 44 px buttons a long dish name broke after four
45
+ letters on a 393 px phone. -->
46
+ <header
47
+ v-if="$slots.header"
48
+ class="flex flex-col gap-2 px-5 pt-2 pb-2 md:pt-4"
49
+ >
50
+ <div class="flex items-center justify-end gap-2">
51
+ <slot name="actions" />
52
+ <DialogClose as-child>
53
+ <SbIconButton label="Schließen">
54
+ <SbIcon name="close" />
55
+ </SbIconButton>
56
+ </DialogClose>
57
+ </div>
58
+ <div class="min-w-0">
59
+ <slot name="header" />
60
+ </div>
61
+ </header>
62
+ <header v-else class="flex items-start gap-3 px-5 pt-3 pb-2 md:pt-6">
43
63
  <div class="min-w-0 flex-1">
44
- <slot name="header">
45
- <DialogTitle
46
- :class="
47
- hideTitle
48
- ? 'sr-only'
49
- : 'font-display text-2xl leading-tight text-sb-ink'
50
- "
51
- >
52
- {{ title }}
53
- </DialogTitle>
54
- <DialogDescription
55
- v-if="description"
56
- class="mt-1 text-sm text-sb-ink-muted"
57
- >
58
- {{ description }}
59
- </DialogDescription>
60
- </slot>
64
+ <DialogTitle
65
+ :class="
66
+ hideTitle
67
+ ? 'sr-only'
68
+ : 'font-display text-2xl leading-tight text-sb-ink'
69
+ "
70
+ >
71
+ {{ title }}
72
+ </DialogTitle>
73
+ <DialogDescription
74
+ v-if="description"
75
+ class="mt-1 text-sm text-sb-ink-muted"
76
+ >
77
+ {{ description }}
78
+ </DialogDescription>
61
79
  </div>
62
80
  <slot name="actions" />
63
81
  <DialogClose as-child>
@@ -1,79 +1,246 @@
1
1
  <script setup lang="ts">
2
2
  import type { Schemas } from "#shopware";
3
+ import type { LocationQueryRaw } from "vue-router";
4
+ import type { AssociationItemProduct } from "~/types/Association";
5
+ import type { WishlistEntry } from "~/composables/useWishlistEntries";
6
+ import { withQuickViewConfiguration } from "~/utils/productUrl";
3
7
 
8
+ /**
9
+ * The wishlist with configured dishes (#467). Each row names the dish with
10
+ * its configuration ("+Extra Salami, ohne Pilze"), shows today's price of
11
+ * dish and extras, adds it to the cart as the product sheet would (same
12
+ * container line item, `useAddToCart`) and opens the sheet with the saved
13
+ * configuration ("Anpassen", the quick view URL of #411 on this page).
14
+ */
4
15
  withDefaults(defineProps<{ showMenuButton?: boolean }>(), {
5
16
  showMenuButton: false,
6
17
  });
7
18
 
8
- const { getWishlistProducts, items } = useWishlist();
19
+ const route = useRoute();
9
20
  const { apiClient } = useShopwareContext();
10
21
  const { getFormattedPrice } = useCommercePrice();
22
+ const { entries, loaded, remove, clear } = useWishlistEntries();
11
23
  const {
12
- isAddingToCart,
13
- addingItemId,
14
- isLoading,
15
- clearWishlistHandler,
16
- addSingleItemToCart,
17
- addAllItemsToCart,
18
- } = useWishlistActions();
24
+ setSelectedProduct,
25
+ setSelectedExtras,
26
+ setDeselectedIngredients,
27
+ selectedQuantity,
28
+ addToCart,
29
+ } = useAddToCart();
19
30
 
20
31
  const products = ref<Schemas["Product"][]>([]);
32
+ const extras = ref<Schemas["Product"][]>([]);
33
+ const loadingProducts = ref(false);
21
34
 
22
- const loadProductsByItemIds = async (itemIds: string[]): Promise<void> => {
23
- isLoading.value = true;
24
-
35
+ async function loadProducts(list: WishlistEntry[]) {
36
+ const productIds = [...new Set(list.map((entry) => entry.productId))];
37
+ const extraNumbers = [...new Set(list.flatMap((entry) => entry.extras))];
38
+ if (!productIds.length) {
39
+ products.value = [];
40
+ extras.value = [];
41
+ return;
42
+ }
43
+ loadingProducts.value = true;
25
44
  try {
26
- const { data } = await apiClient.invoke("readProduct post /product", {
27
- body: { ids: itemIds || items.value },
28
- });
29
-
30
- if (data?.elements) {
31
- products.value = data.elements;
32
- }
45
+ const [dishes, extraProducts] = await Promise.all([
46
+ apiClient.invoke("readProduct post /product", {
47
+ // @ts-expect-error sw-inheritance is missing from the generated header type
48
+ headers: { "sw-inheritance": "true" },
49
+ body: { ids: productIds, limit: productIds.length },
50
+ }),
51
+ extraNumbers.length
52
+ ? apiClient.invoke("readProduct post /product", {
53
+ body: {
54
+ filter: [
55
+ {
56
+ type: "equalsAny",
57
+ field: "productNumber",
58
+ value: extraNumbers.join("|"),
59
+ },
60
+ ],
61
+ limit: extraNumbers.length,
62
+ },
63
+ })
64
+ : Promise.resolve(undefined),
65
+ ]);
66
+ products.value = dishes.data.elements ?? [];
67
+ extras.value = extraProducts?.data.elements ?? [];
33
68
  } catch (error) {
34
- console.error("[wishlist][loadProductsByItemIds]", error);
69
+ console.error("[wishlist][loadProducts]", error);
70
+ } finally {
71
+ loadingProducts.value = false;
35
72
  }
73
+ }
36
74
 
37
- isLoading.value = false;
75
+ watch(
76
+ () => entries.value.map((entry) => entry.id).join(),
77
+ () => loadProducts(entries.value),
78
+ { immediate: true },
79
+ );
80
+
81
+ function nameOf(product: Schemas["Product"] | undefined) {
82
+ return product?.translated?.name ?? product?.name ?? "";
83
+ }
84
+
85
+ type Row = {
86
+ entry: WishlistEntry;
87
+ product: Schemas["Product"];
88
+ extras: Schemas["Product"][];
89
+ configuration: string;
90
+ price: number;
91
+ // a parent with variants (saved before #467) needs a choice first
92
+ needsChoice: boolean;
38
93
  };
39
94
 
40
- watch(
41
- items,
42
- (items, oldItems) => {
43
- if (items.length !== oldItems?.length) {
44
- products.value = products.value.filter(({ id }: { id: string }) =>
45
- items.includes(id),
95
+ const rows = computed<Row[]>(() =>
96
+ entries.value.flatMap((entry) => {
97
+ const product = products.value.find(
98
+ (candidate) => candidate.id === entry.productId,
99
+ );
100
+ if (!product) return [];
101
+ const chosenExtras = entry.extras
102
+ .map((number) =>
103
+ extras.value.find((extra) => extra.productNumber === number),
104
+ )
105
+ .filter((extra): extra is Schemas["Product"] => !!extra);
106
+ const configuration = [
107
+ ...chosenExtras.map((extra) => `+${nameOf(extra)}`),
108
+ ...entry.without.map((name) => `ohne ${name}`),
109
+ ].join(", ");
110
+ const price =
111
+ (product.calculatedPrice?.unitPrice ?? 0) +
112
+ chosenExtras.reduce(
113
+ (sum, extra) => sum + (extra.calculatedPrice?.unitPrice ?? 0),
114
+ 0,
46
115
  );
47
- }
48
- if (!items.length) {
49
- return;
50
- }
51
- loadProductsByItemIds(items);
52
- },
53
- {
54
- immediate: true,
55
- },
116
+ return [
117
+ {
118
+ entry,
119
+ product,
120
+ extras: chosenExtras,
121
+ configuration,
122
+ price,
123
+ needsChoice: (product.childCount ?? 0) > 0,
124
+ },
125
+ ];
126
+ }),
56
127
  );
57
128
 
58
- onMounted(async () => {
59
- await getWishlistProducts();
60
- });
129
+ // "Anpassen" opens the product sheet on this page.
130
+ const quickView = useProductQuickView(products);
131
+
132
+ function customizeLink(row: Row) {
133
+ return {
134
+ query: withQuickViewConfiguration(route.query, {
135
+ productNumber: row.product.productNumber,
136
+ without: row.entry.without,
137
+ extras: row.entry.extras,
138
+ }) as LocationQueryRaw,
139
+ };
140
+ }
141
+
142
+ function rowLabel(row: Row) {
143
+ return row.configuration
144
+ ? `${nameOf(row.product)} (${row.configuration})`
145
+ : nameOf(row.product);
146
+ }
147
+
148
+ const addingId = ref<string | null>(null);
149
+ const addingAll = ref(false);
150
+ const failed = ref<string | null>(null);
151
+
152
+ function asExtra(extra: Schemas["Product"]): AssociationItemProduct {
153
+ return {
154
+ label: nameOf(extra),
155
+ value: extra.id,
156
+ productNumber: extra.productNumber,
157
+ price: getFormattedPrice(extra.calculatedPrice?.unitPrice),
158
+ unitPrice: extra.calculatedPrice?.unitPrice,
159
+ };
160
+ }
161
+
162
+ /** Adds one row with the same line item the product sheet creates. */
163
+ async function addRow(row: Row) {
164
+ setSelectedProduct(row.product);
165
+ setSelectedExtras(row.extras.map(asExtra));
166
+ setDeselectedIngredients(row.entry.without);
167
+ selectedQuantity.value = 1;
168
+ let added = false;
169
+ await addToCart(() => (added = true));
170
+ return added;
171
+ }
172
+
173
+ async function addOne(row: Row) {
174
+ addingId.value = row.entry.id;
175
+ failed.value = null;
176
+ try {
177
+ if (!(await addRow(row))) failed.value = rowLabel(row);
178
+ } finally {
179
+ addingId.value = null;
180
+ }
181
+ }
182
+
183
+ async function addAll() {
184
+ addingAll.value = true;
185
+ failed.value = null;
186
+ try {
187
+ for (const row of rows.value) {
188
+ if (row.needsChoice) continue;
189
+ if (!(await addRow(row))) {
190
+ failed.value = rowLabel(row);
191
+ return;
192
+ }
193
+ }
194
+ } finally {
195
+ addingAll.value = false;
196
+ }
197
+ }
198
+
199
+ const removingId = ref<string | null>(null);
200
+ async function removeRow(row: Row) {
201
+ removingId.value = row.entry.id;
202
+ try {
203
+ await remove(row.entry.id);
204
+ } catch (error) {
205
+ console.error("[wishlist][remove]", error);
206
+ } finally {
207
+ removingId.value = null;
208
+ }
209
+ }
210
+
211
+ const confirmClear = ref(false);
212
+ const clearing = ref(false);
213
+ async function clearAll() {
214
+ clearing.value = true;
215
+ try {
216
+ await clear();
217
+ confirmClear.value = false;
218
+ } catch (error) {
219
+ console.error("[wishlist][clear]", error);
220
+ } finally {
221
+ clearing.value = false;
222
+ }
223
+ }
61
224
  </script>
62
225
 
63
226
  <template>
64
227
  <div class="flex flex-col gap-6 font-body text-sb-ink">
65
228
  <p
66
- v-if="isLoading && !products.length"
229
+ v-if="!loaded || (loadingProducts && !rows.length)"
67
230
  role="status"
68
231
  class="text-sb-ink-muted"
69
232
  >
70
233
  Merkliste wird geladen …
71
234
  </p>
72
235
  <div
73
- v-else-if="!products.length"
236
+ v-else-if="!rows.length"
74
237
  class="flex flex-col items-start gap-4 rounded-sb-card border border-sb-line bg-sb-surface p-6"
75
238
  >
76
239
  <p>Ihre Merkliste ist leer.</p>
240
+ <p class="text-sm text-sb-ink-muted">
241
+ Im Gericht merken Sie es mit dem Herz, so wie Sie es zusammengestellt
242
+ haben.
243
+ </p>
77
244
  <SbButton v-if="showMenuButton" to="/speisekarte/"
78
245
  >Zur Speisekarte</SbButton
79
246
  >
@@ -83,42 +250,81 @@ onMounted(async () => {
83
250
  class="flex flex-col overflow-hidden rounded-sb-card border border-sb-line bg-sb-surface"
84
251
  >
85
252
  <li
86
- v-for="product in products"
87
- :key="product.id"
88
- class="grid grid-cols-[minmax(0,1fr)_auto] items-center gap-x-3 gap-y-2 border-b border-sb-line p-4 last:border-b-0 sm:grid-cols-[minmax(0,1fr)_auto_auto]"
253
+ v-for="row in rows"
254
+ :key="row.entry.id"
255
+ :aria-label="rowLabel(row)"
256
+ class="grid grid-cols-[minmax(0,1fr)_auto] items-start gap-x-3 gap-y-3 border-b border-sb-line p-4 last:border-b-0"
89
257
  >
90
258
  <p class="min-w-0">
91
259
  <span
92
260
  class="block font-bold [overflow-wrap:anywhere] hyphens-auto"
93
- >{{ product.translated?.name ?? product.name }}</span
261
+ >{{ nameOf(row.product) }}</span
262
+ >
263
+ <span
264
+ v-if="row.configuration"
265
+ class="block text-sm [overflow-wrap:anywhere]"
266
+ >{{ row.configuration }}</span
94
267
  >
95
268
  <span class="text-sm text-sb-ink-muted tabular-nums"
96
- >Nr. {{ product.productNumber }} ·
97
- {{ getFormattedPrice(product.calculatedPrice?.totalPrice) }}</span
269
+ >Nr. {{ row.product.productNumber }} ·
270
+ {{ getFormattedPrice(row.price) }}</span
98
271
  >
99
272
  </p>
100
- <AddToWishlist :product="product" />
101
- <SbButton
102
- variant="tint"
103
- class="col-span-full sm:col-span-1"
104
- :loading="addingItemId === product.id"
105
- @click="addSingleItemToCart(product)"
273
+ <SbIconButton
274
+ :label="`${rowLabel(row)} von der Merkliste entfernen`"
275
+ variant="ghost"
276
+ :disabled="removingId === row.entry.id"
277
+ @click="removeRow(row)"
106
278
  >
107
- In den Warenkorb
108
- <span class="sr-only"
109
- >: {{ product.translated?.name ?? product.name }}</span
279
+ <SbIcon name="close" />
280
+ </SbIconButton>
281
+ <div class="col-span-full flex flex-col gap-2 sm:flex-row">
282
+ <SbButton
283
+ v-if="!row.needsChoice"
284
+ variant="tint"
285
+ :loading="addingId === row.entry.id"
286
+ :disabled="addingAll"
287
+ @click="addOne(row)"
110
288
  >
111
- </SbButton>
289
+ In den Warenkorb
290
+ <span class="sr-only">: {{ rowLabel(row) }}</span>
291
+ </SbButton>
292
+ <SbButton variant="secondary" :to="customizeLink(row)">
293
+ {{ row.needsChoice ? "Auswählen" : "Anpassen" }}
294
+ <span class="sr-only">: {{ rowLabel(row) }}</span>
295
+ </SbButton>
296
+ </div>
112
297
  </li>
113
298
  </ul>
299
+ <p
300
+ v-if="failed"
301
+ role="alert"
302
+ class="rounded-sb-control border-[1.5px] border-sb-danger p-4 text-sm"
303
+ >
304
+ {{ failed }} konnte nicht in den Warenkorb gelegt werden. Bitte
305
+ versuchen Sie es erneut.
306
+ </p>
114
307
  <div class="flex flex-col gap-3 sm:flex-row">
115
- <SbButton :loading="isAddingToCart" @click="addAllItemsToCart(products)"
308
+ <SbButton :loading="addingAll" @click="addAll"
116
309
  >Alle in den Warenkorb</SbButton
117
310
  >
118
- <SbButton variant="ghost" @click="clearWishlistHandler"
311
+ <SbButton variant="ghost" @click="confirmClear = true"
119
312
  >Merkliste leeren</SbButton
120
313
  >
121
314
  </div>
315
+ <SbConfirmDialog
316
+ v-model:open="confirmClear"
317
+ title="Merkliste leeren?"
318
+ description="Alle gemerkten Gerichte werden entfernt."
319
+ confirm-label="Merkliste leeren"
320
+ :loading="clearing"
321
+ @confirm="clearAll"
322
+ />
122
323
  </template>
324
+ <LazyProductQuickView
325
+ v-if="quickView.mounted.value"
326
+ v-model:open="quickView.open.value"
327
+ :product="quickView.product.value"
328
+ />
123
329
  </div>
124
330
  </template>
@@ -0,0 +1,57 @@
1
+ <script setup lang="ts">
2
+ import type { WishlistConfiguration } from "~/composables/useWishlistEntries";
3
+
4
+ /**
5
+ * Saves the dish as configured right now (#467): variant, deselected
6
+ * ingredients and extras. A toggle with `aria-pressed` and a stable name;
7
+ * the pressed state follows the configuration, so changing an extra after
8
+ * saving shows the new combination as not saved yet.
9
+ */
10
+ const props = defineProps<{
11
+ selection: WishlistConfiguration | undefined;
12
+ name: string;
13
+ }>();
14
+
15
+ const { find, add, remove } = useWishlistEntries();
16
+ const { trackAddToWishlist } = useTrackEvent();
17
+
18
+ const saved = computed(() =>
19
+ props.selection ? find(props.selection) : undefined,
20
+ );
21
+ const busy = ref(false);
22
+ const failed = ref(false);
23
+
24
+ async function toggle() {
25
+ if (!props.selection || busy.value) return;
26
+ busy.value = true;
27
+ failed.value = false;
28
+ try {
29
+ if (saved.value) {
30
+ await remove(saved.value.id);
31
+ } else {
32
+ await add(props.selection);
33
+ trackAddToWishlist({ productNumber: props.selection.productNumber });
34
+ }
35
+ } catch (error) {
36
+ console.error("[wishlist][toggle]", error);
37
+ failed.value = true;
38
+ } finally {
39
+ busy.value = false;
40
+ }
41
+ }
42
+ </script>
43
+
44
+ <template>
45
+ <SbIconButton
46
+ :label="`${name} merken`"
47
+ :pressed="!!saved"
48
+ :disabled="!selection"
49
+ :aria-busy="busy || undefined"
50
+ @click="toggle"
51
+ >
52
+ <SbIcon name="heart" :filled="!!saved" />
53
+ </SbIconButton>
54
+ <span class="sr-only" role="status">{{
55
+ failed ? "Merken hat nicht geklappt. Bitte versuchen Sie es erneut." : ""
56
+ }}</span>
57
+ </template>
@@ -50,7 +50,9 @@ export function useTrackEvent() {
50
50
  push(["trackEcommerceOrder", order.orderNumber, order.price.totalPrice]);
51
51
  }
52
52
 
53
- function trackAddToWishlist(product: Schemas["Product"]) {
53
+ function trackAddToWishlist(
54
+ product: Pick<Schemas["Product"], "productNumber">,
55
+ ) {
54
56
  push(["trackEvent", "Product", "AddToWishlist", product.productNumber]);
55
57
  }
56
58