Skip to content

Commit 85f06a3

Browse files
Marcin Kwiatkowskibartoszherba
andauthored
refactor: refactored usedRelatedProducts composable (#732)
* refactor(relatedproducts): refactored usedRelatedProducts composable * Update packages/composables/src/factories/useRelatedProductsFactory.ts Co-authored-by: Bartosz Herba <[email protected]> * Update packages/composables/src/composables/useRelatedProducts/index.ts Co-authored-by: Bartosz Herba <[email protected]> Co-authored-by: Bartosz Herba <[email protected]>
1 parent dd38e2c commit 85f06a3

File tree

6 files changed

+86
-7
lines changed

6 files changed

+86
-7
lines changed

packages/composables/src/composables/useRelatedProducts/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import {
1414
UseRelatedProductsFactoryParams,
1515
} from '../../factories/useRelatedProductsFactory';
1616

17+
/**
18+
* @deprecated since version 1.0.0
19+
*/
1720
const factoryParams: UseRelatedProductsFactoryParams<RelatedProductQuery['products']['items'][0]['related_products'], ProductsSearchParams> = {
1821
productsSearch: async (
1922
context: Context,

packages/composables/src/factories/useRelatedProductsFactory.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
} from '@vue-storefront/core';
1212
import { UseRelatedProducts, UseRelatedProductsErrors } from '../types/composables';
1313

14+
/**
15+
* @deprecated since version 1.0.0
16+
*/
1417
export interface UseRelatedProductsFactoryParams<
1518
PRODUCTS,
1619
RELATED_PRODUCTS_SEARCH_PARAMS extends ProductsSearchParams,

packages/theme/components/RelatedProducts.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
/>
77
</template>
88
<script>
9-
import { defineComponent } from '@nuxtjs/composition-api';
10-
import { useRelatedProducts } from '@vue-storefront/magento';
11-
import { onSSR } from '@vue-storefront/core';
9+
import { defineComponent, useFetch, ref } from '@nuxtjs/composition-api';
10+
import { useRelatedProducts } from '~/composables';
1211
import ProductsCarousel from '~/components/ProductsCarousel.vue';
1312
import { productData } from '~/helpers/product/productData';
1413
@@ -21,19 +20,20 @@ export default defineComponent({
2120
const { id } = productData();
2221
const {
2322
search,
24-
products,
2523
loading,
26-
} = useRelatedProducts(id);
24+
} = useRelatedProducts();
25+
const products = ref([]);
2726
28-
onSSR(async () => {
27+
useFetch(async () => {
2928
const baseSearchQuery = {
3029
filter: {
3130
sku: {
3231
eq: id,
3332
},
3433
},
3534
};
36-
await search(baseSearchQuery);
35+
36+
products.value = await search(baseSearchQuery);
3737
});
3838
3939
return {

packages/theme/composables/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export { default as useCategorySearch } from './useCategorySearch';
1818
export { default as useProduct } from './useProduct';
1919
export { default as useShipping } from './useShipping';
2020
export { default as useShippingProvider } from './useShippingProvider';
21+
export { default as useRelatedProducts } from './useRelatedProducts';
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { ref, useContext, Ref } from '@nuxtjs/composition-api';
2+
import { ComposableFunctionArgs, ProductsSearchParams, Logger } from '@vue-storefront/core';
3+
// eslint-disable-next-line import/no-extraneous-dependencies
4+
import { GetProductSearchParams } from '@vue-storefront/magento-api';
5+
import { UseRelatedProductsError, RelatedProducts, UseRelatedProductsInterface } from '~/composables/useRelatedProducts/useRelatedProducts';
6+
7+
export const useRelatedProducts = (): UseRelatedProductsInterface => {
8+
const { app } = useContext();
9+
const loading: Ref<boolean> = ref(false);
10+
const error: Ref<UseRelatedProductsError> = ref({
11+
search: null,
12+
});
13+
14+
const search = async (params: ComposableFunctionArgs<ProductsSearchParams>): Promise<Array<RelatedProducts>> => {
15+
const {
16+
customQuery,
17+
...searchParams
18+
} = params;
19+
20+
let results = null;
21+
22+
try {
23+
loading.value = true;
24+
25+
Logger.debug('[Magento] Load related products based on ', { searchParams });
26+
27+
const { data } = await app
28+
.$vsf
29+
.$magento
30+
.api
31+
.relatedProduct(searchParams as GetProductSearchParams);
32+
33+
Logger.debug('[Result] Related products:', { data });
34+
35+
results = data.products?.items[0]?.related_products;
36+
error.value.search = null;
37+
} catch (err) {
38+
error.value.search = err;
39+
Logger.error('useRelatedProducts/search', err);
40+
} finally {
41+
loading.value = false;
42+
}
43+
44+
return results;
45+
};
46+
47+
return {
48+
search,
49+
loading,
50+
error,
51+
};
52+
};
53+
54+
export default useRelatedProducts;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ComposableFunctionArgs, ProductsSearchParams } from '@vue-storefront/core';
2+
import { Ref } from '@nuxtjs/composition-api';
3+
// eslint-disable-next-line import/no-extraneous-dependencies
4+
import {
5+
RelatedProductQuery,
6+
} from '@vue-storefront/magento-api';
7+
8+
export interface UseRelatedProductsError {
9+
search: Error | null;
10+
}
11+
12+
export type RelatedProducts = RelatedProductQuery['products']['items'][0]['related_products'];
13+
14+
export interface UseRelatedProductsInterface {
15+
search: (params?: ComposableFunctionArgs<ProductsSearchParams>) => Promise<Array<RelatedProducts>>;
16+
loading: Ref<boolean>;
17+
error: Ref<UseRelatedProductsError>;
18+
}

0 commit comments

Comments
 (0)