Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate FiltersInjection mixin #1467

Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/_vue3-migration-test/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
TestScroll,
TestSortDropdown,
TestSortList,
TestSortPickerList
TestSortPickerList,
TestFiltersInjection
} from './';

const routes = [
Expand Down Expand Up @@ -92,6 +93,11 @@ const routes = [
path: '/elements-list',
name: 'ElementsList',
component: TestElementsList
},
{
path: '/filters-injection',
name: 'FiltersInjection',
component: TestFiltersInjection
}
];

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as TestFiltersInjection } from './test-filters-injection.vue';
export { default as TestFacets } from './test-facets.vue';
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script setup lang="ts">
import FiltersSearch from '../../../../../x-components/src/x-modules/facets/components/lists/filters-search.vue';
import SlicedFilters from '../../../../../x-components/src/x-modules/facets/components/lists/sliced-filters.vue';
import SortedFilters from '../../../../../x-components/src/x-modules/facets/components/lists/sorted-filters.vue';
import ExcludeFiltersWithNoResults from '../../../../../x-components/src/x-modules/facets/components/lists/exclude-filters-with-no-results.vue';
import FiltersList from '../../../../../x-components/src/x-modules/facets/components/lists/filters-list.vue';
import { useState } from '../../../../../x-components/src/composables/use-state';
import { useRegisterXModule } from '../../../../../x-components/src/composables/use-register-x-module';
import { facetsXModule } from '../../../../../x-components/src/x-modules/facets/x-module';

useRegisterXModule(facetsXModule);
joseacabaneros marked this conversation as resolved.
Show resolved Hide resolved

const facet = useState('facets', ['facets']).facets.value['category'];
</script>

<template>
<div>
<ExcludeFiltersWithNoResults :filters="facet.filters">
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
<SortedFilters>
<FiltersSearch :data-test="`filters-search-${facet.id}`">
<SlicedFilters :max="4" :data-test="`${facet.label}-sliced-filters`">
<FiltersList
v-slot="{
// eslint-disable-next-line vue/no-unused-vars
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
filter
}"
>
{{ filter.label }}
{{ filter.totalResults }}
</FiltersList>
</SlicedFilters>
</FiltersSearch>
</SortedFilters>
</ExcludeFiltersWithNoResults>
</div>
</template>

<style scoped lang="scss">
.list {
overflow: auto;
height: 100px;
}
</style>
10 changes: 10 additions & 0 deletions packages/_vue3-migration-test/src/x-modules/facets/x-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ const facets: Record<Facet['id'], Facet> = {
color: createSimpleFacetStub('color', createFilter => [
createFilter('red', true),
createFilter('blue', false)
]),
category: createSimpleFacetStub('category', createFilter => [
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
createFilter('dress', false),
createFilter('floral', false, 5),
createFilter('skirt', false, 9),
createFilter('print', false, 9),
createFilter('midi', false, 3),
createFilter('fitted', false, 0),
createFilter('short', false),
createFilter('long', false, 15)
])
};
const filters = arrayToObject(
Expand Down
joseacabaneros marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,38 +1,51 @@
import Vue, { ComponentOptions, defineComponent, PropType, provide, ref } from 'vue';
import { Filter } from '@empathyco/x-types';
import { mount, Wrapper } from '@vue/test-utils';
import Vue, { ComponentOptions } from 'vue';
import { Component, Prop } from 'vue-property-decorator';
import {
createHierarchicalFilter,
createSimpleFilter
} from '../../../../../__stubs__/filters-stubs.factory';
import { XProvide } from '../../../../../components/decorators/injection.decorators';
import FiltersInjectionMixin from '../filters-injection.mixin';

@Component({
} from '../../__stubs__/filters-stubs.factory';
import { useFiltersInjection } from '../use-filters-injection';

const Provider = defineComponent({
name: 'Provider',
props: {
injectedData: {
type: Array as PropType<Filter[]>
}
},
setup(props) {
const filters = ref(props.injectedData);
provide('filters', filters);
},
template: `
<div>
<slot />
</div>
`
})
class Provider extends Vue {
@Prop()
@XProvide('filters')
public injectedData!: any;
}
});

const Filters: ComponentOptions<Vue> = {
mixins: [FiltersInjectionMixin],
props: {
filters: Array as PropType<Filter[]>,
parentId: {
type: String as PropType<Filter['id']>,
required: false
}
},
setup(props) {
const renderedFilters = useFiltersInjection(props);
return { renderedFilters };
},
template: `
<div>
<li v-for="filter of renderedFilters" :key="filter.id">{{ filter.id }}</li>
</div>
`
};

describe('test filters injection mixin', () => {
function renderFiltersMixin({
describe('test filters injection composable', () => {
function renderFiltersComposable({
propFilters,
injectedFilters,
parentId
Expand Down Expand Up @@ -76,7 +89,7 @@ describe('test filters injection mixin', () => {
const whiteFilter = createSimpleFilter('color', 'white');
const blackFilter = createSimpleFilter('color', 'black');
const blueFilter = createSimpleFilter('color', 'blue');
const { setPropFilters, getRenderedFilterIds } = renderFiltersMixin({
const { setPropFilters, getRenderedFilterIds } = renderFiltersComposable({
propFilters: [whiteFilter, blackFilter]
});
let renderedIds = getRenderedFilterIds();
Expand All @@ -96,7 +109,7 @@ describe('test filters injection mixin', () => {
const whiteFilter = createSimpleFilter('color', 'white');
const blackFilter = createSimpleFilter('color', 'black');
const blueFilter = createSimpleFilter('color', 'blue');
const { getRenderedFilterIds } = renderFiltersMixin({
const { getRenderedFilterIds } = renderFiltersComposable({
injectedFilters: [whiteFilter, blackFilter, blueFilter]
});
const renderedIds = getRenderedFilterIds();
Expand All @@ -110,7 +123,7 @@ describe('test filters injection mixin', () => {
const whiteFilter = createSimpleFilter('color', 'white');
const blackFilter = createSimpleFilter('color', 'black');
const blueFilter = createSimpleFilter('color', 'blue');
const { getRenderedFilterIds } = renderFiltersMixin({
const { getRenderedFilterIds } = renderFiltersComposable({
propFilters: [whiteFilter, blackFilter],
injectedFilters: [blueFilter]
});
Expand All @@ -137,7 +150,7 @@ describe('test filters injection mixin', () => {
categoryShirts.parentId = categoryJeans.parentId = categoryMen.id;
const categoryWomen = createHierarchicalFilter('category', 'women', false);

const { getRenderedFilterIds } = renderFiltersMixin({
const { getRenderedFilterIds } = renderFiltersComposable({
propFilters: [categoryShirts, categoryJeans, categoryMen, categoryWomen]
});
const renderedIds = getRenderedFilterIds();
Expand All @@ -164,7 +177,7 @@ describe('test filters injection mixin', () => {
categoryShirts.parentId = categoryJeans.parentId = categoryMen.id;
const categoryWomen = createHierarchicalFilter('category', 'women', false);

const { getRenderedFilterIds } = renderFiltersMixin({
const { getRenderedFilterIds } = renderFiltersComposable({
propFilters: [categoryShirts, categoryJeans, categoryMen, categoryWomen],
parentId: categoryMen.id
});
Expand Down
1 change: 1 addition & 0 deletions packages/x-components/src/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './use-store';
export * from './use-state';
export * from './use-getter';
export * from './use-alias-api';
export * from './use-filters-injection';
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,52 +1,30 @@
import { Filter, HierarchicalFilter, isHierarchicalFilter } from '@empathyco/x-types';
import Vue from 'vue';
import Component from 'vue-class-component';
import { Prop } from 'vue-property-decorator';
import { XInject } from '../../../../components/decorators/injection.decorators';
import { isArrayEmpty } from '../../../../utils/array';
import { computed, inject, Ref } from 'vue';
import { isArrayEmpty } from '../utils/index';
joseacabaneros marked this conversation as resolved.
Show resolved Hide resolved

/**
* Mixin to share filters injection logic.
* Composable to share filters injection logic.
*
* @param props - Composable props.
* @returns An array of filters.
*
* @public
*/
@Component
export default class FiltersInjectionMixin extends Vue {
export function useFiltersInjection(props: {
/** The list of filters to be rendered as slots. */
filters?: Filter[] | undefined;
joseacabaneros marked this conversation as resolved.
Show resolved Hide resolved
/**
* The list of filters to be rendered as slots.
*
* @public
This prop is used in the `HierarchicalFilter` component and only in that case. It is necessary
to make the `renderedFilters` to return only the filters of each level of the hierarchy.
*/
@Prop()
protected filters!: Filter[];

parentId?: Filter['id'] | undefined;
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
}) {
/**
* This prop is used in the `HierarchicalFilter` component and only in that case. It is necessary
* to make the `renderedFilters` to return only the filters of each level of the hierarchy.
* The injected filters array.
*
* @public
*/
@Prop({ required: false })
protected parentId?: Filter['id'];

/** The injected filters array.
*
* @public
*/
@XInject('filters')
public injectedFilters!: Filter[];

/**
* The prop or injected filters array, filtered by parentId if they are
* {@link @empathyco/x-types#HierarchicalFilter}.
*
* @returns An array of filters.
*
* @internal
*/
protected get renderedFilters(): Filter[] {
return this.filterByParentId(this.propOrInjectedFilters);
}
const injectedFilters = inject<Ref<Filter[]> | undefined>('filters', undefined);

/**
* An array of filters formed by those that are passed through prop or injected.
Expand All @@ -55,15 +33,15 @@ export default class FiltersInjectionMixin extends Vue {
*
* @internal
*/
protected get propOrInjectedFilters(): Filter[] {
const propOrInjectedFilters = computed((): void | Filter[] => {
return (
this.filters ??
this.injectedFilters ??
props.filters ??
injectedFilters?.value ??
//TODO: add here logger
//eslint-disable-next-line no-console
console.warn('It is necessary to pass a prop or inject the list of filters')
);
}
});

/**
* In the case that the filters are {@link @empathyco/x-types#HierarchicalFilter}, this method
Expand All @@ -76,13 +54,23 @@ export default class FiltersInjectionMixin extends Vue {
* @returns The list of the filters filtered by parentId.
* @internal
*/
protected filterByParentId(filters: Filter[]): Filter[] {
const filterByParentId = (filters: Filter[]): Filter[] => {
if (!isArrayEmpty(filters) && isHierarchicalFilter(filters[0])) {
return (filters as HierarchicalFilter[]).filter(
filter => filter.parentId === (this.parentId ?? null)
filter => filter.parentId === (props.parentId ?? null)
);
} else {
return filters;
}
}
};

/**
* The prop or injected filters array, filtered by parentId if they are
* {@link @empathyco/x-types#HierarchicalFilter}.
*
* @returns An array of filters.
*
* @internal
*/
return computed<Filter[]>(() => filterByParentId(propOrInjectedFilters.value as Filter[]));
}
2 changes: 1 addition & 1 deletion packages/x-components/src/views/home/aside.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<ChevronDown />
</template>
<!-- Filters -->
<SlicedFilters max="4" :filters="facet.filters">
<SlicedFilters :max="4" :filters="facet.filters">
joseacabaneros marked this conversation as resolved.
Show resolved Hide resolved
<FiltersList v-slot="{ filter }">
<HierarchicalFilter
:filter="filter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export { default as PreselectedFilters } from './preselected-filters.vue';
// Lists
// eslint-disable-next-line max-len
export { default as ExcludeFiltersWithNoResults } from './lists/exclude-filters-with-no-results.vue';
export { default as FiltersInjectionMixin } from './lists/filters-injection.mixin';
export { default as FiltersList } from './lists/filters-list.vue';
export { default as FiltersSearch } from './lists/filters-search.vue';
export { default as SelectedFilters } from './lists/selected-filters.vue';
Expand Down
Loading
Loading