Skip to content

Commit

Permalink
Merge 0fc6e24 into cdcadc4
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszjedrasik committed Mar 29, 2021
2 parents cdcadc4 + 0fc6e24 commit 320e7e0
Show file tree
Hide file tree
Showing 20 changed files with 650 additions and 32 deletions.
89 changes: 76 additions & 13 deletions packages/boilerplate/api-client/src/api/getCategory/index.ts
Original file line number Diff line number Diff line change
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.
111 changes: 111 additions & 0 deletions packages/commercetools/theme/components/Header/HeaderNav.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<template>
<SfHeaderNavigation>
<SfHeaderNavigationItem
v-for="category in categories"
:key="category.id"
:label="category.name"
@mouseenter="() => handleMouseEnter(category.slug)"
@mouseleave="() => handleMouseLeave()"
@click="handleMouseLeave()"
:link="localePath(`/c/${category.slug}`)"
>
<SfMegaMenu
is-absolute
:visible="currentCatSlug === category.slug"
:title="category.name"
@close="currentCatSlug = ''"
v-if="activeSubCategory && activeSubCategory[0] && activeSubCategory[0].children"
>
<SfMegaMenuColumn
v-for="subCategory in activeSubCategory[0].children"
:key="subCategory.id"
:title="subCategory.name"
>
<SfLoader :loading="subCategoriesLoading">
<SfList>
<SfListItem
v-for="subCategoryChild in subCategory.children"
:key="subCategoryChild.id"
>
<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 { reactive, 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 activeSubCategory = ref(null);
const fetchedSubCategories = reactive({});
const handleMouseEnter = debounce(async slug => {
currentCatSlug.value = slug;
const { childCount } = categories.value.find(category => category.slug === currentCatSlug.value);
emit('setOverlay', Boolean(childCount));
if (!fetchedSubCategories[slug] && Boolean(childCount)) {
await subCategoriesSearch({ slug });
fetchedSubCategories[slug] = subCategories.value;
}
activeSubCategory.value = fetchedSubCategories[currentCatSlug.value];
}, 200);
const handleMouseLeave = debounce(() => {
emit('setOverlay', false);
currentCatSlug.value = '';
}, 200);
onSSR(async () => {
await search({ customQuery: { categories: 'root-categories-query' } });
});
return {
categories,
fetchedSubCategories,
activeSubCategory,
subCategories,
currentCatSlug,
subCategoriesLoading,
handleMouseEnter,
handleMouseLeave
};
}
};
</script>

<style lang='scss'>
.sf-mega-menu__bar.sf-bar {
display: flex;
@include for-desktop {
display: none;
}
}
</style>
109 changes: 109 additions & 0 deletions packages/commercetools/theme/components/Header/MobileMenu.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<template>
<SfMegaMenu
visible
@close="toggleMobileMenu"
>
<SfMegaMenuColumn
v-for="category in categories"
:key="category.id"
:title="category.name"
>
<template #title="{ title, changeActive }">
<SfMenuItem
:label="title"
class="sf-mega-menu-column__header"
@click="changeActive(title); handleClickCategory(category.slug)"
/>
</template>
<SfList v-if="activeCategory && activeCategory[0] && activeCategory[0].children">
<SfListItem
v-for="subCategory in activeCategory[0].children"
:key="subCategory.id"
>
<SfMenuItem
:label="subCategory.name"
@click.native="handleClickSubCategory(subCategory.slug)"
>
<SfLink>
{{ subCategory.name }}
</SfLink>
</SfMenuItem>
</SfListItem>
</SfList>
<NewCatBanners v-if="currentCatSlug === 'new'" />
</SfMegaMenuColumn>
</SfMegaMenu>
</template>

<script>
import { SfMegaMenu, SfMenuItem, SfList, SfLoader } from '@storefront-ui/vue';
import { useCategory } from '@vue-storefront/commercetools';
import { useUiState } from '~/composables';
import { onSSR } from '@vue-storefront/core';
import { ref } from '@vue/composition-api';
export default {
name: 'MobileMenu',
components: {
SfMegaMenu,
SfMenuItem,
SfList,
SfLoader,
NewCatBanners: () => import('./NewCatBanners')
},
setup (_, { root }) {
const { categories, search } = useCategory('menu-categories');
const { categories: subCategories, search: subCategoriesSearch, loading: subCategoriesLoading } = useCategory('menu-subCategories');
const currentCatSlug = ref('');
const deepCatSlug = ref('');
const activeCategory = ref(null);
const fetchedCategories = ref({});
const { toggleMobileMenu } = useUiState();
const getSubCategories = async (slug, childCount) => {
if (!childCount) {
root.$router.push(`/c/${slug}`);
toggleMobileMenu();
}
if (!fetchedCategories.value[slug]) {
await subCategoriesSearch({ slug });
fetchedCategories.value = {
...fetchedCategories.value,
[slug]: subCategories.value
};
}
activeCategory.value = fetchedCategories.value[slug];
};
const handleClickCategory = async slug => {
currentCatSlug.value = slug;
const { childCount } = categories.value.find(category => category.slug === currentCatSlug.value);
await getSubCategories(slug, childCount);
};
const handleClickSubCategory = async slug => {
deepCatSlug.value = slug;
if (activeCategory.value && activeCategory.value[0] && activeCategory.value[0].children) {
const { childCount } = activeCategory.value[0].children.find(child => child.slug === deepCatSlug.value);
await getSubCategories(slug, childCount);
}
};
onSSR(async () => {
await search({ customQuery: { categories: 'root-categories-query' } });
});
return {
categories,
activeCategory,
currentCatSlug,
handleClickCategory,
handleClickSubCategory,
toggleMobileMenu,
subCategoriesLoading
};
}
};
</script>

0 comments on commit 320e7e0

Please sign in to comment.