Skip to content

Commit

Permalink
chore: update customQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszjedrasik committed Mar 17, 2021
1 parent 8f6c1b0 commit f32c3ec
Show file tree
Hide file tree
Showing 15 changed files with 361 additions and 21 deletions.
89 changes: 76 additions & 13 deletions packages/boilerplate/api-client/src/api/getCategory/index.ts
Expand Up @@ -5,70 +5,133 @@ export default async function getCategory(context, params, customQuery?: CustomQ
return Promise.resolve([
{
id: 1,
name: 'New',
slug: 'new',
childCount: 2,
children: [
{
id: 15,
name: 'Women',
slug: 'new-women',
childCount: 2,
children: [
{
id: 16,
name: 'Clothing',
slug: 'new-women-clothing',
childCount: 0,
children: []
},
{
id: 17,
name: 'Shoes',
slug: 'new-women-shoes',
childCount: 0,
children: []
}
]
},
{
id: 11,
name: 'Men',
slug: 'new-men',
childCount: 1,
children: [
{
id: 18,
name: 'Clothing',
slug: 'new-men-clothing',
childCount: 0,
children: []
},
{
id: 19,
name: 'Shoes',
slug: 'new-men-shoes',
childCount: 0,
children: []
}
]
}
]
},
{
id: 2,
name: 'Women',
slug: 'women',
items: [
childCount: 2,
children: [
{
id: 4,
name: 'Women jackets',
slug: 'women-jackets',
items: [
childCount: 2,
children: [
{
id: 9,
name: 'Winter jackets',
slug: 'winter-jackets',
items: []
childCount: 0,
children: []
},
{
id: 10,
name: 'Autumn jackets',
slug: 'autmun-jackets',
items: []
childCount: 0,
children: []
}
]
},
{
id: 5,
name: 'Skirts',
slug: 'skirts',
items: []
childCount: 0,
children: []
}
]
},
{
id: 2,
id: 3,
name: 'Men',
slug: 'men',
items: [
childCount: 1,
children: [
{
id: 6,
name: 'Men T-shirts',
slug: 'men-tshirts',
items: []
childCount: 0,
children: []
}
]
},
{
id: 3,
id: 4,
name: 'Kids',
slug: 'kids',
items: [
childCount: 1,
children: [
{
id: 7,
name: 'Toys',
slug: 'toys',
items: [
childCount: 2,
children: [
{
id: 8,
name: 'Toy Cars',
slug: 'toy-cars',
items: []
childCount: 0,
children: []
},
{
id: 8,
name: 'Dolls',
slug: 'dolls',
items: []
childCount: 0,
children: []
}
]
}
Expand Down
Binary file not shown.
Binary file not shown.
95 changes: 95 additions & 0 deletions packages/commercetools/theme/components/Header/HeaderNav.vue
@@ -0,0 +1,95 @@
<template>
<SfHeaderNavigation>
<SfHeaderNavigationItem
v-for="(category, index) in categories"
:key="index"
:label="category.name"
@mouseenter="() => handleMouseEnter(category.slug)"
@mouseleave="() => handleMouseLeave()"
@click="handleMouseLeave()"
:link="localePath(`/c/${category.slug}`)"
>
<SfMegaMenu
:is-absolute="true"
:visible="currentCatSlug === category.slug"
:title="category.name"
@close="currentCatSlug = ''"
v-if="category && category.children.length"
>
<SfMegaMenuColumn
v-for="(subCategory, subIndex) in category.children"
:key="subIndex"
:title="subCategory.name"
>
<SfLoader :loading="subCategoriesLoading">
<SfList>
<SfListItem
v-for="(subCategoryChild, childIndex) in subCategory.children"
:key="childIndex"
>
<SfMenuItem :label="subCategoryChild.name" :link="localePath(`/c/${subCategoryChild.slug}`)">
<SfLink>
{{ subCategoryChild.name }}
</SfLink>
</SfMenuItem>
</SfListItem>
</SfList>
</SfLoader>
</SfMegaMenuColumn>
<NewCatBanners v-if="currentCatSlug === 'new'" />
</SfMegaMenu>
</SfHeaderNavigationItem>
</SfHeaderNavigation>
</template>

<script>
import { SfMegaMenu, SfMenuItem, SfList, SfBanner, SfLoader } from '@storefront-ui/vue';
import { useCategory } from '@vue-storefront/commercetools';
import { onSSR } from '@vue-storefront/core';
import { ref } from '@vue/composition-api';
import debounce from 'lodash.debounce';
export default {
name: 'HeaderNav',
components: {
SfMegaMenu,
SfMenuItem,
SfList,
SfBanner,
SfLoader,
NewCatBanners: () => import('./NewCatBanners')
},
setup (_, { emit }) {
const { categories, search } = useCategory('menu-categories');
const { categories: subCategories, search: subCategoriesSearch, loading: subCategoriesLoading } = useCategory('menu-subCategories');
const currentCatSlug = ref('');
const handleMouseEnter = debounce((slug) => {
if (currentCatSlug.value) return;
currentCatSlug.value = slug;
const catChild = categories.value.find(category => category.slug === currentCatSlug.value);
emit('setOverlay', Boolean(catChild.childCount));
subCategoriesSearch({ slug });
}, 200);
const handleMouseLeave = debounce(() => {
emit('setOverlay', false);
currentCatSlug.value = '';
}, 200);
onSSR(async () => {
await search({ customQuery: { categories: 'root-categories-query' } });
});
return {
categories,
subCategories,
currentCatSlug,
subCategoriesLoading,
handleMouseEnter,
handleMouseLeave
};
}
};
</script>
8 changes: 8 additions & 0 deletions packages/commercetools/theme/middleware.config.js
Expand Up @@ -24,6 +24,14 @@ module.exports = {
},
currency: 'USD',
country: 'US'
},
customQueries: {
'root-categories-query': ({ query, variables }) => {

variables.where = 'parent is not defined';

return { query, variables };
}
}
}
}
Expand Down
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions packages/core/docs/changelog/5267.js
@@ -0,0 +1,8 @@
module.exports = {
description: 'added MegaMenu to theme',
link: 'https://github.com/vuestorefront/vue-storefront/issues/5267',
isBreaking: false,
breakingChanges: [],
author: 'Łukasz Jędrasik',
linkToGitHubAccount: 'https://github.com/lukaszjedrasik'
};
@@ -1,5 +1,6 @@
<template>
<div>
<SfOverlay :visible="!!isOverlayVisible" />
<SfHeader
data-cy="app-header"
class="sf-header--has-mobile-search"
Expand All @@ -12,8 +13,7 @@
</nuxt-link>
</template>
<template #navigation>
<SfHeaderNavigationItem class="nav-item" data-cy="app-header-url_women" label="WOMEN" :link="localePath('/c/women')"/>
<SfHeaderNavigationItem class="nav-item" data-cy="app-header-url_men" label="MEN" :link="localePath('/c/men')" />
<HeaderNav @setOverlay="isOverlayVisible = $event" />
</template>
<template #aside>
<LocaleSelector class="smartphone-only" />
Expand Down Expand Up @@ -102,6 +102,7 @@ import { onSSR } from '@vue-storefront/core';
import { useUiHelpers } from '~/composables';
import LocaleSelector from './LocaleSelector';
import SearchResults from '~/components/SearchResults';
import HeaderNav from './HeaderNav';
import { clickOutside } from '@storefront-ui/vue/src/utilities/directives/click-outside/click-outside-directive.js';
import {
mapMobileObserver,
Expand All @@ -119,7 +120,8 @@ export default {
SfBadge,
SfSearchBar,
SearchResults,
SfOverlay
SfOverlay,
HeaderNav
},
directives: { clickOutside },
setup(props, { root }) {
Expand All @@ -132,6 +134,7 @@ export default {
const term = ref(getFacetsFromURL().term);
const isSearchOpen = ref(false);
const searchBarRef = ref(null);
const isOverlayVisible = ref(false);
const cartTotalItems = computed(() => {
const count = cartGetters.getTotalItems(cart.value);
Expand Down Expand Up @@ -209,7 +212,8 @@ export default {
result,
closeOrFocusSearchBar,
searchBarRef,
isMobile
isMobile,
isOverlayVisible
};
}
};
Expand All @@ -220,6 +224,7 @@ export default {
--header-padding: var(--spacer-sm);
@include for-desktop {
--header-padding: 0;
z-index: var(--header-wrapper-z-index, 1);
}
&__logo-image {
height: 100%;
Expand Down

0 comments on commit f32c3ec

Please sign in to comment.