basklee-nuxt 0.9.9 → 0.9.10
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 +8 -4
- package/package.json +1 -1
- package/src/project-readme.mjs +8 -4
- package/templates/app/components/basklee/account/AccountSection.README.md +1 -1
- package/templates/app/components/basklee/account/AccountSection.vue +3 -3
- package/templates/app/components/basklee/products/StorefrontProduct.README.md +2 -0
- package/templates/app/components/basklee/products/StorefrontProduct.vue +53 -0
- package/templates/app/components/basklee/storefront/StorefrontAccount.README.md +2 -1
- package/templates/app/composables/useBaskleeApi.ts +32 -4
- package/templates/app/composables/useBaskleeRelatedProducts.ts +125 -0
- package/templates/app/composables/useBaskleeSignIn.ts +2 -2
- package/templates/app/utils/basklee/api.ts +19 -13
- package/templates/app/utils/basklee/relatedProducts.ts +106 -0
- package/templates/basklee/COMPONENTS.md +3 -1
package/README.md
CHANGED
|
@@ -725,9 +725,11 @@ HTTP `401` means the request has no signed-in session. When it arrives while
|
|
|
725
725
|
`store.isAuthenticated` is `true`, the client calls the store's `endSession()`
|
|
726
726
|
once: the profile, wallet, authenticated discovery, cart/wishlist counters and
|
|
727
727
|
account values of known products are removed at once, then Application Load
|
|
728
|
-
reloads for the guest session.
|
|
729
|
-
|
|
730
|
-
stays
|
|
728
|
+
reloads for the guest session. Every `401` (also a guest's) then opens
|
|
729
|
+
`/profile?locale=<locale>&redirect=<current page>`, which returns to the page
|
|
730
|
+
after sign-in; on `/profile` and `/logout` the page stays. The failed request
|
|
731
|
+
is not repeated; a `403`, CSRF rejection or network failure stays an ordinary
|
|
732
|
+
request error.
|
|
731
733
|
|
|
732
734
|
See the [cookie lifecycle and browser example](https://docs.basklee.com/en/sandbox/appload/nuxt#session-cookies)
|
|
733
735
|
for exact source references and a copyable component.
|
|
@@ -1100,7 +1102,9 @@ heart, an availability notification, Checkout in the cart, a plan's Subscribe
|
|
|
1100
1102
|
or Try, an event) opens `/profile?locale=<locale>&redirect=<current page>` at
|
|
1101
1103
|
once through `useBaskleeSignIn().requireSignIn()` in
|
|
1102
1104
|
`app/composables/useBaskleeSignIn.ts`, with no separate sign-in notice.
|
|
1103
|
-
|
|
1105
|
+
A request that answers HTTP 401 opens the same address: the shared API client
|
|
1106
|
+
ends a signed-in session first (the guest storefront shows at once), and the
|
|
1107
|
+
sign-in page returns to the page afterwards; `/profile` and `/logout` stay put.
|
|
1104
1108
|
Account pages (`/orders`, `/wallet`, `/addresses`, `/subscriptions`, `/tickets`,
|
|
1105
1109
|
`/activity`, `/wishlist`) declare the `basklee-account` route middleware in
|
|
1106
1110
|
`app/middleware/basklee-account.ts`: after Application Load confirms a guest
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "basklee-nuxt",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.10",
|
|
4
4
|
"description": "Nuxt 4 storefront for your Basklee marketplace: setup CLI, server API gateway, typed store and ready-made catalog, cart, checkout and account components in English, Russian and Spanish, plus an Android app build. Powered by Basklee: https://basklee.com",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/src/project-readme.mjs
CHANGED
|
@@ -93,7 +93,9 @@ heart, an availability notification, Checkout in the cart, a plan's Subscribe
|
|
|
93
93
|
or Try, an event) opens \`/profile?locale=<locale>&redirect=<current page>\` at
|
|
94
94
|
once through \`useBaskleeSignIn().requireSignIn()\` in
|
|
95
95
|
\`app/composables/useBaskleeSignIn.ts\`, with no separate sign-in notice.
|
|
96
|
-
|
|
96
|
+
A request that answers HTTP 401 opens the same address: the shared API client
|
|
97
|
+
ends a signed-in session first (the guest storefront shows at once), and the
|
|
98
|
+
sign-in page returns to the page afterwards; \`/profile\` and \`/logout\` stay put.
|
|
97
99
|
Account pages (\`/orders\`, \`/wallet\`, \`/addresses\`, \`/subscriptions\`, \`/tickets\`,
|
|
98
100
|
\`/activity\`, \`/wishlist\`) declare the \`basklee-account\` route middleware in
|
|
99
101
|
\`app/middleware/basklee-account.ts\`: after Application Load confirms a guest
|
|
@@ -409,9 +411,11 @@ HTTP \`401\` means the request has no signed-in session. When it arrives while
|
|
|
409
411
|
\`store.isAuthenticated\` is \`true\`, the client calls the store's \`endSession()\`
|
|
410
412
|
once: the profile, wallet, authenticated discovery, cart/wishlist counters and
|
|
411
413
|
account values of known products are removed at once, then Application Load
|
|
412
|
-
reloads for the guest session.
|
|
413
|
-
|
|
414
|
-
stays
|
|
414
|
+
reloads for the guest session. Every \`401\` (also a guest's) then opens
|
|
415
|
+
\`/profile?locale=<locale>&redirect=<current page>\`, which returns to the page
|
|
416
|
+
after sign-in; on \`/profile\` and \`/logout\` the page stays. The failed request
|
|
417
|
+
is not repeated; a \`403\`, CSRF rejection or network failure stays an ordinary
|
|
418
|
+
request error.
|
|
415
419
|
|
|
416
420
|
[Cookie lifecycle and browser request example](https://docs.basklee.com/en/sandbox/appload/nuxt#session-cookies)
|
|
417
421
|
|
|
@@ -14,7 +14,7 @@ This is the only navigation on an account page: the components rendered in the s
|
|
|
14
14
|
|
|
15
15
|
The component never renders a sign-in form. Account pages declare the `basklee-account` route middleware (`app/middleware/basklee-account.ts`); after Application Load confirms a guest session, it replaces the page with `/profile?locale=<locale>&redirect=<current page>`. The sign-in form on `/profile` returns to that page after the refreshed Application Load confirms the session. A pending or failed Application Load does not redirect.
|
|
16
16
|
|
|
17
|
-
If the session ends while an account page is open, for example when a request answers HTTP 401, the component shows a short message that the section needs sign-in instead of the section content, and the
|
|
17
|
+
If the session ends while an account page is open, for example when a request answers HTTP 401, the component shows a short message that the section needs sign-in instead of the section content, and the header shows Login. The shared API client removes the signed-in data from the store at once, reloads Application Load for the guest session and opens `/profile?locale=<locale>&redirect=<current page>`, which returns to the page after sign-in. After a failed Application Load it shows an error with Retry, which repeats Application Load.
|
|
18
18
|
|
|
19
19
|
The guide describes its inputs and events. Source is maintained in
|
|
20
20
|
`app/components/basklee/account/AccountSection.vue`; it has no separate interface file.
|
|
@@ -20,9 +20,9 @@
|
|
|
20
20
|
const sections = computed(() => Object.entries(labels.value.navigation));
|
|
21
21
|
|
|
22
22
|
// The basklee-account middleware opens /profile before a guest reaches
|
|
23
|
-
// this page. When the session ends while the page is open, the
|
|
24
|
-
//
|
|
25
|
-
//
|
|
23
|
+
// this page. When the session ends while the page is open, the section
|
|
24
|
+
// explains that the account content needs sign-in while the shared API
|
|
25
|
+
// client opens the sign-in page, which returns here afterwards. A failed Application Load is
|
|
26
26
|
// not a guest session: it offers Retry instead.
|
|
27
27
|
</script>
|
|
28
28
|
|
|
@@ -32,6 +32,8 @@ The manufacturer card shows the logo from `manufacture.small_image`, or `manufac
|
|
|
32
32
|
|
|
33
33
|
The seller card shows `seller.avatar`, `seller.name` and a positive `seller.raiting`. Its link points to `/sellers?seller=` with `seller.id`; selecting it emits `seller`, and the supplied page opens that address. The card is hidden when `init.visual_settings.single_seller_mode` is on.
|
|
34
34
|
|
|
35
|
+
Under the reviews, a row of other products, “You may also like”: the products attached to this product on the panel's Relevant Products tab (`related.data` of the returned product, without the product itself, up to eight), in their order. Only a product without attached products shows up to eight more products of its `main_category` (with its public subcategories), asked from `products_search`, and a link to that category's page. Without attached products and without other products of the category the row is not shown; a failed category request leaves it off the page. The row's cards use the same purchase, wishlist, availability and preorder actions as the product; selecting a card opens that product's page. The logic is in `app/composables/useBaskleeRelatedProducts.ts` and `app/utils/basklee/relatedProducts.ts`.
|
|
36
|
+
|
|
35
37
|
After client-side navigation, until the product arrives, the component shows a placeholder with the page layout: gallery frame, thumbnails, name, price, description, characteristics, variants and button. The page keeps its height, the section has `aria-busy` and a hidden status announces loading. A failed request replaces the placeholder with the error notice and Retry.
|
|
36
38
|
|
|
37
39
|
When the selected product endpoint succeeds but returns no product with this ID, the server render of the supplied page answers HTTP 404; after client-side navigation the component emits `missing` and shows the unavailable state. `products_search` returns an empty list both for an unknown ID and for a product that is not published. A failed request, including an invalid preview key, is not a missing product: the error notice keeps Retry. The supplied `app/pages/product/[id].vue` opens the storefront 404 page on `missing` through `useBaskleeMissingPage()`.
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
watch,
|
|
9
9
|
} from 'vue';
|
|
10
10
|
import { NuxtLink } from '#components';
|
|
11
|
+
import { navigateTo } from '#imports';
|
|
11
12
|
import { useBaskleeStore } from '@/stores/basklee';
|
|
12
13
|
import { useBaskleeProductsStore } from '@/stores/baskleeProducts';
|
|
13
14
|
import { useBaskleeCatalogStore } from '@/stores/baskleeCatalog';
|
|
@@ -18,6 +19,8 @@
|
|
|
18
19
|
} from '@/composables/useBaskleeFeature';
|
|
19
20
|
import { useBaskleeProductActions } from '@/composables/useBaskleeProductActions';
|
|
20
21
|
import { useBaskleeProductVariants } from '@/composables/useBaskleeProductVariants';
|
|
22
|
+
import { useBaskleeRelatedProducts } from '@/composables/useBaskleeRelatedProducts';
|
|
23
|
+
import { relatedProductsLabels } from '@/utils/basklee/relatedProducts';
|
|
21
24
|
import { reviewLabels } from '@/utils/basklee/reviewLabels';
|
|
22
25
|
import { featureLabels } from '@/utils/basklee/featureLabels';
|
|
23
26
|
import { productLabels } from '@/utils/basklee/productLabels';
|
|
@@ -54,6 +57,7 @@
|
|
|
54
57
|
IProductSpecificationRow,
|
|
55
58
|
} from '@/interfaces/basklee/products/IProductSpecification';
|
|
56
59
|
import ProductCard from '@/components/basklee/products/ProductCard.vue';
|
|
60
|
+
import ProductGroup from '@/components/basklee/products/ProductGroup.vue';
|
|
57
61
|
import ProductReviews from '@/components/basklee/products/ProductReviews.vue';
|
|
58
62
|
import ProductPreorderDialog from '@/components/basklee/products/ProductPreorderDialog.vue';
|
|
59
63
|
import ProductSpecificationsDialog from '@/components/basklee/products/ProductSpecificationsDialog.vue';
|
|
@@ -697,6 +701,18 @@
|
|
|
697
701
|
currentId.value = selection.variant.product.id;
|
|
698
702
|
image.value = 0;
|
|
699
703
|
}
|
|
704
|
+
// The row under the product: the products attached on the panel's
|
|
705
|
+
// «Relevant Products» tab first, else more of the product's category.
|
|
706
|
+
const related = useBaskleeRelatedProducts({ api, product });
|
|
707
|
+
const relatedLabels = computed(() =>
|
|
708
|
+
relatedProductsLabels(selectionStore.localeCode),
|
|
709
|
+
);
|
|
710
|
+
function openRelated(item: IProduct) {
|
|
711
|
+
void navigateTo({
|
|
712
|
+
path: `/product/${item.id}`,
|
|
713
|
+
query: { locale: selectionStore.localeCode },
|
|
714
|
+
});
|
|
715
|
+
}
|
|
700
716
|
onMounted(() => {
|
|
701
717
|
mounted.value = true;
|
|
702
718
|
if (!confirmed.value) {
|
|
@@ -1334,6 +1350,43 @@
|
|
|
1334
1350
|
:product-id="currentId"
|
|
1335
1351
|
/>
|
|
1336
1352
|
</div>
|
|
1353
|
+
<section
|
|
1354
|
+
v-if="related.productIds.value.length"
|
|
1355
|
+
class="mt-14 pt-10 border-0 border-t border-solid border-basklee-border @min-[592px]/product:mt-20"
|
|
1356
|
+
:aria-label="relatedLabels.title"
|
|
1357
|
+
data-product-related
|
|
1358
|
+
>
|
|
1359
|
+
<header class="mb-6 flex flex-wrap items-baseline justify-between gap-x-6 gap-y-2">
|
|
1360
|
+
<h2 class="m-0 text-xl font-semibold text-basklee-text">
|
|
1361
|
+
{{ relatedLabels.title }}
|
|
1362
|
+
</h2>
|
|
1363
|
+
<NuxtLink
|
|
1364
|
+
v-if="related.categoryId.value"
|
|
1365
|
+
:to="{
|
|
1366
|
+
path: `/category/${related.categoryId.value}`,
|
|
1367
|
+
query: { locale: selectionStore.localeCode },
|
|
1368
|
+
}"
|
|
1369
|
+
class="text-sm text-basklee-text underline underline-offset-4"
|
|
1370
|
+
data-product-related-category
|
|
1371
|
+
>
|
|
1372
|
+
{{ relatedLabels.category }}
|
|
1373
|
+
</NuxtLink>
|
|
1374
|
+
</header>
|
|
1375
|
+
<ProductGroup
|
|
1376
|
+
:product-ids="related.productIds.value"
|
|
1377
|
+
:disabled="actions.pending.value || actions.needsRefresh.value"
|
|
1378
|
+
@select="openRelated"
|
|
1379
|
+
@select-seller="emit('seller', $event)"
|
|
1380
|
+
@select-variant="variants.select"
|
|
1381
|
+
@request-secondary-variants="variants.requestSecondary"
|
|
1382
|
+
@toggle-favorite="actions.run('toggleFavorite', $event)"
|
|
1383
|
+
@add="actions.run('add', $event)"
|
|
1384
|
+
@remove="actions.run('remove', $event)"
|
|
1385
|
+
@subscribe="actions.run('subscribe', $event)"
|
|
1386
|
+
@unsubscribe="actions.run('unsubscribe', $event)"
|
|
1387
|
+
@preorder="actions.openPreorder"
|
|
1388
|
+
/>
|
|
1389
|
+
</section>
|
|
1337
1390
|
<ProductPreorderDialog
|
|
1338
1391
|
:product-id="actions.preorder.value"
|
|
1339
1392
|
:pending="actions.pending.value"
|
|
@@ -25,7 +25,8 @@ After confirmed authentication changes, refresh Application Load to replace the
|
|
|
25
25
|
session, profile, wallet and live endpoint discovery. When a request answers
|
|
26
26
|
HTTP 401 during a signed-in session, the shared API client removes the profile
|
|
27
27
|
and wallet from the store at once and reloads Application Load for the guest
|
|
28
|
-
session; the button shows Login again and
|
|
28
|
+
session; the button shows Login again, and the sign-in page opens with the
|
|
29
|
+
current page as its return address.
|
|
29
30
|
|
|
30
31
|
The authenticated name comes from `store.profile`; missing names use Profile.
|
|
31
32
|
The wallet reads only the selected currency's backend-converted amount. Zero
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
navigateTo,
|
|
2
3
|
useNuxtApp,
|
|
3
4
|
usePinia,
|
|
4
5
|
useRequestFetch,
|
|
@@ -10,6 +11,7 @@ import { useBaskleeStore } from '@/stores/basklee';
|
|
|
10
11
|
import { useBaskleeSelectionsStore } from '@/stores/baskleeSelections';
|
|
11
12
|
import { useBaskleeAppLoad } from '@/composables/useBaskleeAppLoad';
|
|
12
13
|
import { BaskleeApi } from '@/utils/basklee/api';
|
|
14
|
+
import { signInPageLocation } from '@/utils/basklee/signInReturn';
|
|
13
15
|
import type {
|
|
14
16
|
BaskleeApiRequest,
|
|
15
17
|
BaskleeApiResponse,
|
|
@@ -149,10 +151,18 @@ function createClients(): BaskleeClients {
|
|
|
149
151
|
browser: new BaskleeApi({
|
|
150
152
|
...shared,
|
|
151
153
|
canRequest: () => import.meta.client,
|
|
152
|
-
// HTTP 401
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
154
|
+
// HTTP 401: the request needs a signed-in customer. An ended session
|
|
155
|
+
// shows the guest storefront at once; then the sign-in page opens and
|
|
156
|
+
// returns to this page afterwards. The failed request is not repeated.
|
|
157
|
+
onUnauthorized: (signedIn) =>
|
|
158
|
+
nuxt.runWithContext(() => {
|
|
159
|
+
const ending = signedIn
|
|
160
|
+
? useBaskleeAppLoad().endSession()
|
|
161
|
+
: undefined;
|
|
162
|
+
openSignInAfterUnauthorized(nuxt, store.localeCode);
|
|
163
|
+
|
|
164
|
+
return ending;
|
|
165
|
+
}),
|
|
156
166
|
}),
|
|
157
167
|
// Page data that the server render awaits; see useBaskleeServerData.
|
|
158
168
|
// A failed server request leaves the page to the browser, which loads
|
|
@@ -161,6 +171,24 @@ function createClients(): BaskleeClients {
|
|
|
161
171
|
};
|
|
162
172
|
}
|
|
163
173
|
|
|
174
|
+
/*
|
|
175
|
+
* After HTTP 401 the visitor goes to the /profile sign-in page with this page
|
|
176
|
+
* in `redirect`, for example `/profile?locale=en&redirect=/orders?locale=en`.
|
|
177
|
+
* The sign-in page itself and /logout stay where they are: /profile already
|
|
178
|
+
* shows the form, and logging out should not ask for sign-in.
|
|
179
|
+
*/
|
|
180
|
+
function openSignInAfterUnauthorized(
|
|
181
|
+
nuxt: ReturnType<typeof useNuxtApp>,
|
|
182
|
+
locale: string,
|
|
183
|
+
) {
|
|
184
|
+
const route = nuxt.$router.currentRoute.value;
|
|
185
|
+
if (route.path === '/profile' || route.path === '/logout') {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
void navigateTo(signInPageLocation(route.fullPath, locale));
|
|
190
|
+
}
|
|
191
|
+
|
|
164
192
|
function useBaskleeClients() {
|
|
165
193
|
const nuxt = useNuxtApp();
|
|
166
194
|
const existing = clients.get(nuxt);
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { computed, onScopeDispose, ref, toValue, watch } from 'vue';
|
|
2
|
+
import type { MaybeRefOrGetter } from 'vue';
|
|
3
|
+
import { useBaskleeProductsStore } from '@/stores/baskleeProducts';
|
|
4
|
+
import { useBaskleeCatalogStore } from '@/stores/baskleeCatalog';
|
|
5
|
+
import type { BaskleeApi } from '@/utils/basklee/api';
|
|
6
|
+
import {
|
|
7
|
+
attachedRelatedProducts,
|
|
8
|
+
relatedCategoryId,
|
|
9
|
+
relatedCategoryProducts,
|
|
10
|
+
relatedCategoryRequest,
|
|
11
|
+
} from '@/utils/basklee/relatedProducts';
|
|
12
|
+
import type {
|
|
13
|
+
IProduct,
|
|
14
|
+
IProductSearchSeller,
|
|
15
|
+
} from '@/interfaces/basklee/products/IProduct';
|
|
16
|
+
|
|
17
|
+
/*
|
|
18
|
+
* The products of the row under a product page (utils/basklee/relatedProducts.ts):
|
|
19
|
+
* the products attached on the panel's «Relevant Products» tab first, else
|
|
20
|
+
* more of the product's main category. The products go into the product
|
|
21
|
+
* store, so the row's cards read them like every other card. A failed
|
|
22
|
+
* category request leaves the row off the page.
|
|
23
|
+
*/
|
|
24
|
+
export function useBaskleeRelatedProducts(options: {
|
|
25
|
+
api: Pick<BaskleeApi, 'request'>;
|
|
26
|
+
product: MaybeRefOrGetter<IProduct | undefined>;
|
|
27
|
+
}) {
|
|
28
|
+
const productStore = useBaskleeProductsStore();
|
|
29
|
+
const catalogStore = useBaskleeCatalogStore();
|
|
30
|
+
const categoryIds = ref<number[]>([]);
|
|
31
|
+
const pending = ref(false);
|
|
32
|
+
let request: AbortController | undefined;
|
|
33
|
+
|
|
34
|
+
const attached = computed(() =>
|
|
35
|
+
attachedRelatedProducts(toValue(options.product)),
|
|
36
|
+
);
|
|
37
|
+
const categoryId = computed(() =>
|
|
38
|
+
relatedCategoryId(toValue(options.product)),
|
|
39
|
+
);
|
|
40
|
+
const productIds = computed(() =>
|
|
41
|
+
attached.value.length
|
|
42
|
+
? attached.value.map((item) => item.id)
|
|
43
|
+
: categoryIds.value,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
function cancel() {
|
|
47
|
+
request?.abort();
|
|
48
|
+
request = undefined;
|
|
49
|
+
pending.value = false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function loadCategory(category: number, productId: number) {
|
|
53
|
+
cancel();
|
|
54
|
+
categoryIds.value = [];
|
|
55
|
+
if (!category) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const current = new AbortController();
|
|
59
|
+
request = current;
|
|
60
|
+
pending.value = true;
|
|
61
|
+
try {
|
|
62
|
+
const data = await options.api.request<{
|
|
63
|
+
products?: { data?: IProduct[] };
|
|
64
|
+
selects?: { sellers?: IProductSearchSeller[] };
|
|
65
|
+
}>(
|
|
66
|
+
'products_search',
|
|
67
|
+
relatedCategoryRequest(category, catalogStore.catalog),
|
|
68
|
+
current.signal,
|
|
69
|
+
);
|
|
70
|
+
if (current.signal.aborted) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const items = relatedCategoryProducts(data.products?.data, productId);
|
|
74
|
+
for (const item of items) {
|
|
75
|
+
productStore.setProduct(item, 'products', data.selects?.sellers ?? []);
|
|
76
|
+
}
|
|
77
|
+
categoryIds.value = items.map((item) => item.id);
|
|
78
|
+
} catch {
|
|
79
|
+
// The row stays off the page; the next visit of the page asks again.
|
|
80
|
+
} finally {
|
|
81
|
+
if (request === current) {
|
|
82
|
+
request = undefined;
|
|
83
|
+
pending.value = false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
watch(
|
|
89
|
+
attached,
|
|
90
|
+
(items) => {
|
|
91
|
+
for (const item of items) {
|
|
92
|
+
if (!productStore.getProduct(item.id)) {
|
|
93
|
+
productStore.setProduct(item, 'products');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
{ immediate: true },
|
|
98
|
+
);
|
|
99
|
+
watch(
|
|
100
|
+
() => [categoryId.value, toValue(options.product)?.id ?? 0] as const,
|
|
101
|
+
([category, productId], previous) => {
|
|
102
|
+
if (
|
|
103
|
+
previous &&
|
|
104
|
+
previous[0] === category &&
|
|
105
|
+
previous[1] === productId
|
|
106
|
+
) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (import.meta.client) {
|
|
110
|
+
void loadCategory(category, productId);
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
{ immediate: true },
|
|
114
|
+
);
|
|
115
|
+
onScopeDispose(cancel);
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
productIds,
|
|
119
|
+
/** The category the row comes from, or 0 for attached products. */
|
|
120
|
+
categoryId: computed(() =>
|
|
121
|
+
attached.value.length ? 0 : categoryId.value,
|
|
122
|
+
),
|
|
123
|
+
pending,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -25,8 +25,8 @@ export function provideBaskleeSignIn(handler: BaskleeSignInHandler) {
|
|
|
25
25
|
* A guest who clicks an action that needs an account (a review, a reaction,
|
|
26
26
|
* a favorite, a plan subscription) goes straight to the /profile sign-in page,
|
|
27
27
|
* which returns to `redirect` afterwards. Call requireSignIn() only from the
|
|
28
|
-
* visitor's own click
|
|
29
|
-
*
|
|
28
|
+
* visitor's own click. A request that answers HTTP 401 opens the same page
|
|
29
|
+
* through the shared API client (see useBaskleeApi).
|
|
30
30
|
*/
|
|
31
31
|
export function useBaskleeSignIn() {
|
|
32
32
|
const store = useBaskleeStore();
|
|
@@ -80,7 +80,7 @@ export class BaskleeApi {
|
|
|
80
80
|
private canRequest: () => boolean;
|
|
81
81
|
private isSignedIn: () => boolean;
|
|
82
82
|
private currency: () => string | number | undefined;
|
|
83
|
-
private
|
|
83
|
+
private onUnauthorized: ((signedIn: boolean) => unknown) | undefined;
|
|
84
84
|
|
|
85
85
|
constructor(options: {
|
|
86
86
|
transport: BaskleeTransport;
|
|
@@ -90,7 +90,7 @@ export class BaskleeApi {
|
|
|
90
90
|
canRequest?: () => boolean;
|
|
91
91
|
isSignedIn?: () => boolean;
|
|
92
92
|
currency?: () => string | number | undefined;
|
|
93
|
-
|
|
93
|
+
onUnauthorized?: (signedIn: boolean) => unknown;
|
|
94
94
|
}) {
|
|
95
95
|
this.transport = options.transport;
|
|
96
96
|
this.endpoints = options.endpoints;
|
|
@@ -99,10 +99,10 @@ export class BaskleeApi {
|
|
|
99
99
|
this.canRequest = options.canRequest ?? (() => true);
|
|
100
100
|
this.isSignedIn = options.isSignedIn ?? (() => false);
|
|
101
101
|
this.currency = options.currency ?? (() => undefined);
|
|
102
|
-
this.
|
|
102
|
+
this.onUnauthorized = options.onUnauthorized;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
/** The
|
|
105
|
+
/** The handling of the latest HTTP 401 (guest refresh, sign-in), while it runs. */
|
|
106
106
|
get sessionEnd() {
|
|
107
107
|
return this.sessionEnding;
|
|
108
108
|
}
|
|
@@ -201,7 +201,7 @@ export class BaskleeApi {
|
|
|
201
201
|
signal?.throwIfAborted();
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
-
this.
|
|
204
|
+
this.detectUnauthorized(response, name);
|
|
205
205
|
signal?.throwIfAborted();
|
|
206
206
|
return response;
|
|
207
207
|
}
|
|
@@ -210,23 +210,28 @@ export class BaskleeApi {
|
|
|
210
210
|
* The backend answers HTTP 401 when the request has no signed-in session:
|
|
211
211
|
* an authentication-required endpoint without a customer ({ auth: false }),
|
|
212
212
|
* or a session the backend rejected and whose cookies it deleted.
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
213
|
+
* Every such answer goes to `onUnauthorized`, which opens sign-in. With
|
|
214
|
+
* `signedIn` true the application still shows a customer, so the session
|
|
215
|
+
* has ended and the handler first removes the account data. One handler
|
|
216
|
+
* call serves concurrent failures. The 401 of `profile_logout` only says
|
|
217
|
+
* that the session had already ended, so it is not passed on; 403 and CSRF
|
|
218
|
+
* rejection are ordinary request errors.
|
|
216
219
|
*/
|
|
217
|
-
private
|
|
220
|
+
private detectUnauthorized(response: BaskleeApiResponse, name = '') {
|
|
218
221
|
if (
|
|
219
222
|
response.status !== 401 ||
|
|
223
|
+
name === 'profile_logout' ||
|
|
220
224
|
this.sessionEnding ||
|
|
221
|
-
!this.
|
|
222
|
-
!this.isSignedIn()
|
|
225
|
+
!this.onUnauthorized
|
|
223
226
|
) {
|
|
224
227
|
return;
|
|
225
228
|
}
|
|
226
229
|
|
|
227
230
|
let ending: Promise<void>;
|
|
228
231
|
try {
|
|
229
|
-
ending = Promise.resolve(this.
|
|
232
|
+
ending = Promise.resolve(this.onUnauthorized(this.isSignedIn())).then(
|
|
233
|
+
() => undefined,
|
|
234
|
+
);
|
|
230
235
|
} catch (error) {
|
|
231
236
|
ending = Promise.reject(error);
|
|
232
237
|
}
|
|
@@ -239,6 +244,7 @@ export class BaskleeApi {
|
|
|
239
244
|
});
|
|
240
245
|
}
|
|
241
246
|
|
|
247
|
+
|
|
242
248
|
private csrfToken() {
|
|
243
249
|
return this.cookies().match(/(?:^|;\s*)csrftoken=([^;]*)/)?.[1] ?? '';
|
|
244
250
|
}
|
|
@@ -257,7 +263,7 @@ export class BaskleeApi {
|
|
|
257
263
|
if (!this.renewal) {
|
|
258
264
|
this.renewal = this.send('/api/csrf_renew', 'POST', {})
|
|
259
265
|
.then((response) => {
|
|
260
|
-
this.
|
|
266
|
+
this.detectUnauthorized(response);
|
|
261
267
|
this.successData(response);
|
|
262
268
|
if (!this.csrfToken()) {
|
|
263
269
|
throw new BaskleeApiError('failed');
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { categoryBranchIds, categoryTrail } from '@/utils/basklee/category';
|
|
2
|
+
import type { ICategory } from '@/interfaces/basklee/category/ICategory';
|
|
3
|
+
import type { IProduct } from '@/interfaces/basklee/products/IProduct';
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* The row of other products under a product page.
|
|
7
|
+
*
|
|
8
|
+
* The products the seller attached to this product on the panel's «Relevant
|
|
9
|
+
* Products» tab come first: they arrive with the product (`related.data`),
|
|
10
|
+
* so they need no request. Only a product without them shows more products
|
|
11
|
+
* of its own main category, asked from `products_search` the way the
|
|
12
|
+
* category page asks (the category with its subcategories), one more than
|
|
13
|
+
* shown because the product itself may be among them.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** How many products the row shows. */
|
|
17
|
+
export const RELATED_PRODUCTS_LIMIT = 8;
|
|
18
|
+
|
|
19
|
+
/** The attached products of `product`, without the product itself. */
|
|
20
|
+
export function attachedRelatedProducts(
|
|
21
|
+
product: IProduct | undefined,
|
|
22
|
+
limit = RELATED_PRODUCTS_LIMIT,
|
|
23
|
+
): IProduct[] {
|
|
24
|
+
const items = product?.related?.data;
|
|
25
|
+
|
|
26
|
+
if (!product || !Array.isArray(items)) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return items
|
|
31
|
+
.filter(
|
|
32
|
+
(item) =>
|
|
33
|
+
Number.isInteger(item?.id) && item.id > 0 && item.id !== product.id,
|
|
34
|
+
)
|
|
35
|
+
.slice(0, limit);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The category whose products fill the row when nothing is attached: the
|
|
40
|
+
* product's main category, or 0 when the row needs none.
|
|
41
|
+
*/
|
|
42
|
+
export function relatedCategoryId(product: IProduct | undefined): number {
|
|
43
|
+
const category = product?.main_category;
|
|
44
|
+
|
|
45
|
+
if (
|
|
46
|
+
!product ||
|
|
47
|
+
attachedRelatedProducts(product).length ||
|
|
48
|
+
typeof category !== 'number' ||
|
|
49
|
+
!Number.isInteger(category) ||
|
|
50
|
+
category <= 0
|
|
51
|
+
) {
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return category;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The `products_search` body for that category: the category with its public
|
|
60
|
+
* subcategories when the catalog tree knows it, otherwise the category alone.
|
|
61
|
+
*/
|
|
62
|
+
export function relatedCategoryRequest(
|
|
63
|
+
categoryId: number,
|
|
64
|
+
catalog: ICategory[],
|
|
65
|
+
limit = RELATED_PRODUCTS_LIMIT,
|
|
66
|
+
) {
|
|
67
|
+
const category = categoryTrail(catalog, categoryId).at(-1);
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
filters: {
|
|
71
|
+
categories: category ? categoryBranchIds(category) : [categoryId],
|
|
72
|
+
},
|
|
73
|
+
paginate: { page: 1, pp_items: limit + 1 },
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The products of the category answer that the row shows. */
|
|
78
|
+
export function relatedCategoryProducts(
|
|
79
|
+
items: IProduct[] | undefined,
|
|
80
|
+
productId: number,
|
|
81
|
+
limit = RELATED_PRODUCTS_LIMIT,
|
|
82
|
+
): IProduct[] {
|
|
83
|
+
return (Array.isArray(items) ? items : [])
|
|
84
|
+
.filter((item) => Number.isInteger(item?.id) && item.id !== productId)
|
|
85
|
+
.slice(0, limit);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const RELATED_EN = {
|
|
89
|
+
title: 'You may also like',
|
|
90
|
+
category: 'All products of the category',
|
|
91
|
+
};
|
|
92
|
+
const RELATED_TITLES: Record<string, { title: string; category: string }> = {
|
|
93
|
+
en: RELATED_EN,
|
|
94
|
+
ru: { title: 'Вам может понравиться', category: 'Все товары категории' },
|
|
95
|
+
es: {
|
|
96
|
+
title: 'También te puede gustar',
|
|
97
|
+
category: 'Todos los productos de la categoría',
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/** The row's heading and the link to its category, in the given language. */
|
|
102
|
+
export function relatedProductsLabels(locale: string) {
|
|
103
|
+
return (
|
|
104
|
+
RELATED_TITLES[String(locale || '').toLowerCase().slice(0, 2)] ?? RELATED_EN
|
|
105
|
+
);
|
|
106
|
+
}
|
|
@@ -1480,7 +1480,9 @@ heart, an availability notification, Checkout in the cart, a plan's Subscribe
|
|
|
1480
1480
|
or Try, an event) opens `/profile?locale=<locale>&redirect=<current page>` at
|
|
1481
1481
|
once through `useBaskleeSignIn().requireSignIn()` in
|
|
1482
1482
|
`app/composables/useBaskleeSignIn.ts`, with no separate sign-in notice.
|
|
1483
|
-
|
|
1483
|
+
A request that answers HTTP 401 opens the same address: the shared API client
|
|
1484
|
+
ends a signed-in session first (the guest storefront shows at once), and the
|
|
1485
|
+
sign-in page returns to the page afterwards; `/profile` and `/logout` stay put.
|
|
1484
1486
|
Account pages (`/orders`, `/wallet`, `/addresses`, `/subscriptions`, `/tickets`,
|
|
1485
1487
|
`/activity`, `/wishlist`) declare the `basklee-account` route middleware in
|
|
1486
1488
|
`app/middleware/basklee-account.ts`: after Application Load confirms a guest
|