Skip to content

Commit e1e54ed

Browse files
committed
refactor: remove category getters call from app header
M2-415
1 parent 6b38fe0 commit e1e54ed

File tree

9 files changed

+31
-41
lines changed

9 files changed

+31
-41
lines changed

packages/theme/components/AppHeader.vue

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
</div>
109109
</template>
110110

111-
<script>
111+
<script lang="ts">
112112
import {
113113
SfOverlay, SfHeader, SfButton, SfBadge,
114114
} from '@storefront-ui/vue';
@@ -122,7 +122,6 @@ import {
122122
onMounted,
123123
useFetch,
124124
} from '@nuxtjs/composition-api';
125-
import { categoryGetters } from '~/getters';
126125
import HeaderNavigationItem from '~/components/Header/Navigation/HeaderNavigationItem.vue';
127126
import {
128127
useCart,
@@ -183,16 +182,25 @@ export default defineComponent({
183182
184183
useFetch(async () => {
185184
await categoriesListLoad({ pageSize: 20 });
186-
categoryTree.value = categoryGetters
187-
.getCategoryTree(categoryList.value?.[0])
188-
?.items.filter((c) => c.count > 0);
185+
186+
function prepareMenuData() {
187+
return categoryList.value?.[0]?.children
188+
.map((category) => ({
189+
includeInMenu: category.include_in_menu,
190+
label: category.name,
191+
slug: `/${category.url_path}${category.url_suffix}`,
192+
}))
193+
.filter((category) => category.includeInMenu) ?? [];
194+
}
195+
196+
categoryTree.value = prepareMenuData();
189197
});
190198
191199
onMounted(() => {
192200
if (app.$device.isDesktop) {
193201
loadCartTotalQty();
194202
// eslint-disable-next-line promise/catch-or-return
195-
loadWishlistItemsCount()
203+
loadWishlistItemsCount({})
196204
.then((response) => {
197205
wishlistItemsQty.value = response;
198206

packages/theme/composables/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export declare type GetProductSearchParams = {
8787
export declare type AvailableStores = AvailableStoresQuery['availableStores'];
8888
export declare type CartItem = CartItemInterface;
8989
export declare type CustomQuery = Record<string, string>;
90-
export declare type Category = CategoryTree | CategorySearchQuery['categoryList'][0];
90+
export declare type Category = CategoryTree;
9191
export interface Product extends ProductInterface, ConfigurableProduct, Omit<BundleProduct, 'items'>, Omit<GroupedProduct, 'items'>, Omit<DownloadableProduct, 'items'>, Omit<VirtualProduct, 'items'> { __typename: string }
9292
export declare type Filter = Record<string, any>;
9393
export declare type Countries = CountriesListQuery['countries'][0];

packages/theme/composables/useCart/useCart.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { Context } from '@nuxt/types';
21
import { ComputedRef, Ref } from '@nuxtjs/composition-api';
32
import { ComposableFunctionArgs } from '~/composables/types';
43
import { CartItemInput } from '~/modules/GraphQL/types';
54

65
export interface UseCartInterface<CART, CART_ITEM, PRODUCT> {
76
load: (params: ComposableFunctionArgs<{ realCart?: boolean; }>) => Promise<void>;
8-
loadTotalQty: (context: Context['app']) => Promise<void>;
7+
loadTotalQty: () => Promise<void>;
98
addItem: (
109
params: ComposableFunctionArgs<{
1110
product: PRODUCT;

packages/theme/getters/productGetters.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ import {
77
Product,
88
} from '~/composables/types';
99
import { ProductGetters as ProductGettersBase } from '~/getters/types';
10-
import { BundleProduct, GroupedProduct } from '~/modules/GraphQL/types';
10+
import {
11+
BundleProduct, CategoryInterface, GroupedProduct, ProductInterface,
12+
} from '~/modules/GraphQL/types';
1113
import { htmlDecode } from '~/helpers/htmlDecoder';
1214
import categoryGetters from './categoryGetters';
1315
import reviewGetters from './reviewGetters';
1416

15-
type ProductVariantFilters = any;
16-
17-
export const getName = (product: Product): string => {
17+
export const getName = (product: ProductInterface): string => {
1818
if (!product) {
1919
return '';
2020
}
2121

2222
return htmlDecode(product.name);
2323
};
2424

25-
export const getSlug = (product: Product, category?: Category): string => {
25+
export const getSlug = (product: ProductInterface, category?: Category): string => {
2626
const rewrites = product?.url_rewrites;
2727
let url = product?.sku ? `/p/${product.sku}` : '';
2828
if (!rewrites || rewrites.length === 0) {
@@ -99,14 +99,6 @@ export const getProductThumbnailImage = (product: Product): string => {
9999
return product.thumbnail.url;
100100
};
101101

102-
export const getFiltered = (products: Product[], filters: ProductVariantFilters | any = {}): Product[] => {
103-
if (!products) {
104-
return [];
105-
}
106-
107-
return products;
108-
};
109-
110102
export const getAttributes = (
111103
products: Product,
112104
_filterByAttributeName?: string[],
@@ -207,7 +199,7 @@ export const getFormattedPrice = (price: number) => {
207199
}).format(price);
208200
};
209201

210-
export const getBreadcrumbs = (product: any, category?: Category): AgnosticBreadcrumb[] => {
202+
export const getBreadcrumbs = (product: ProductInterface, category?: Category): AgnosticBreadcrumb[] => {
211203
let breadcrumbs = [];
212204

213205
if (!product) {
@@ -244,14 +236,14 @@ export const getGroupedProducts = (product: GroupedProduct & { __typename: strin
244236
// eslint-disable-next-line no-underscore-dangle
245237
export const getBundleProducts = (product: BundleProduct & { __typename: string }): BundleProduct['items'] | undefined => product.__typename === 'BundleProduct' && product?.items?.sort(sortProduct);
246238

247-
export interface ProductGetters extends ProductGettersBase<Product, ProductVariantFilters> {
239+
export interface ProductGetters extends ProductGettersBase<Product> {
248240
getCategory(product: Product, currentUrlPath: string): Category | null;
249241
getProductRelatedProduct(product: Product): Product[];
250242
getProductSku(product: Product): string;
251243
getProductThumbnailImage(product: Product): string;
252244
getProductUpsellProduct(product: Product): Product[];
253245
getShortDescription(product: Product): string;
254-
getSlug(product: Product, category?: Category): string;
246+
getSlug(product: Product, category?: CategoryInterface): string;
255247
getTypeId(product: Product): string;
256248
getSwatchData(swatchData: Product['configurable_options'][0]['values'][0]['swatch_data']): string | undefined;
257249
getGroupedProducts(product: GroupedProduct): GroupedProduct['items'] | undefined;
@@ -266,7 +258,6 @@ const productGetters: ProductGetters = {
266258
getCategoryIds,
267259
getCoverImage,
268260
getDescription,
269-
getFiltered,
270261
getFormattedPrice,
271262
getGallery,
272263
getId,

packages/theme/getters/types.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ export interface AddressGetter {
1919
}[];
2020
}
2121

22-
export interface ProductGetters<PRODUCT, PRODUCT_FILTER> {
22+
export interface ProductGetters<PRODUCT> {
2323
getName: (product: PRODUCT) => string;
2424
getSlug: (product: PRODUCT) => string;
2525
getPrice: (product: PRODUCT) => AgnosticPrice;
2626
getGallery: (product: PRODUCT) => AgnosticMediaGalleryItem[];
2727
getCoverImage: (product: PRODUCT) => string;
28-
getFiltered: (products: PRODUCT[], filters?: PRODUCT_FILTER) => PRODUCT[];
2928
getAttributes: (products: PRODUCT[] | PRODUCT, filters?: Array<string>) => Record<string, AgnosticAttribute | string>;
3029
getDescription: (product: PRODUCT) => string;
3130
getCategoryIds: (product: PRODUCT) => string[];
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import { computed, useRoute } from '@nuxtjs/composition-api';
2-
import { productGetters } from '~/getters';
32

43
export const productData = (products) => {
54
const route = useRoute();
6-
const { params: { id }, query } = route.value;
5+
const { params: { id } } = route.value;
76

87
return {
98
id,
109
product: computed(() => {
1110
if (!products) return {};
12-
const baseProduct = Array.isArray(products.value?.items) && products.value?.items[0] ? products.value?.items[0] : [];
13-
14-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
15-
return productGetters.getFiltered(baseProduct, {
16-
master: true,
17-
attributes: query,
18-
});
11+
return Array.isArray(products.value?.items) && products.value?.items[0] ? products.value?.items[0] : [];
1912
}),
2013
};
2114
};

packages/theme/modules/catalog/pages/category.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
class="breadcrumbs"
1010
/>
1111
<CategoryNavbar
12+
v-if="isShowProducts"
1213
:sort-by="sortBy"
1314
:pagination="pagination"
1415
@reloadProducts="fetch"
1516
/>
1617
<div class="main section">
1718
<CategorySidebar
19+
v-if="isShowProducts"
1820
class="sidebar desktop-only"
1921
/>
2022
<div
@@ -320,7 +322,6 @@ export default defineComponent({
320322
const { fetch } = useFetch(async () => {
321323
routeData.value = await resolveUrl();
322324
const content = await getContentData(routeData.value?.id);
323-
324325
cmsContent.value = content?.cmsBlock?.content ?? '';
325326
isShowCms.value = content.isShowCms;
326327
isShowProducts.value = content.isShowProducts;

packages/theme/pages/Home.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export default defineComponent({
251251
252252
addTags([{ prefix: CacheTagPrefix.View, value: 'home' }]);
253253
254-
newProducts.value = productGetters.getFiltered(productsData?.items, { master: true });
254+
newProducts.value = productsData?.items;
255255
});
256256
257257
// @ts-ignore

packages/theme/types/core.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,13 +655,12 @@ export interface AgnosticStore {
655655
[x: string]: unknown;
656656
}
657657

658-
export interface ProductGetters<PRODUCT, PRODUCT_FILTER> {
658+
export interface ProductGetters<PRODUCT> {
659659
getName: (product: PRODUCT) => string;
660660
getSlug: (product: PRODUCT) => string;
661661
getPrice: (product: PRODUCT) => AgnosticPrice;
662662
getGallery: (product: PRODUCT) => AgnosticMediaGalleryItem[];
663663
getCoverImage: (product: PRODUCT) => string;
664-
getFiltered: (products: PRODUCT[], filters?: PRODUCT_FILTER) => PRODUCT[];
665664
getAttributes: (products: PRODUCT[] | PRODUCT, filters?: Array<string>) => Record<string, AgnosticAttribute | string>;
666665
getDescription: (product: PRODUCT) => string;
667666
getCategoryIds: (product: PRODUCT) => string[];

0 commit comments

Comments
 (0)