Skip to content

Commit

Permalink
Category filter max depth & root path
Browse files Browse the repository at this point in the history
  • Loading branch information
Jade-GG committed Mar 4, 2025
1 parent 172cb24 commit 46524a3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 62 deletions.
13 changes: 9 additions & 4 deletions resources/js/components/Listing/Listing.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,20 @@ export default {
field: filter.code + (this.filterType(filter) == 'string' ? '.keyword' : ''),
type: this.filterType(filter),
})),
{ attribute: 'category_lvl0', field: 'category_lvl0.keyword', type: 'string' },
{ attribute: 'category_lvl1', field: 'category_lvl1.keyword', type: 'string' },
{ attribute: 'category_lvl2', field: 'category_lvl2.keyword', type: 'string' },
{ attribute: 'category_lvl3', field: 'category_lvl3.keyword', type: 'string' },
...this.categoryAttributes.map(attribute => ({
attribute: attribute,
field: attribute + '.keyword',
type: 'string'
})),
]
// TODO: Double check this and how it's used.
// .concat(this.additionalFilters)
},
categoryAttributes() {
return Array.from({length: config.max_category_level ?? 3}).map((_, index) => 'category_lvl' + (index + 1))
},
sortings: function () {
return Object.values(this.attributes).filter((attribute) => attribute.sorting)
},
Expand Down
1 change: 1 addition & 0 deletions resources/views/category/overview.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

@if ($category->is_anchor)
<x-rapidez::listing
:root-path="$category->parentcategories->pluck('name')->join(' > ')"
v-bind:base-filters="() => [{
query_string: {
query: 'visibility:(2 OR 4) AND category_ids:{{ $category->entity_id }}'
Expand Down
2 changes: 2 additions & 0 deletions resources/views/components/listing.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@props(['rootPath' => null])

@pushOnce('head', 'es_url-preconnect')
<link rel="preconnect" href="{{ config('rapidez.es_url') }}">

Expand Down
78 changes: 22 additions & 56 deletions resources/views/listing/partials/filter/category.blade.php
Original file line number Diff line number Diff line change
@@ -1,63 +1,29 @@
<ais-hierarchical-menu
:attributes="[
'category_lvl1',
'category_lvl2',
'category_lvl3',
{{-- TODO: This should be dynamic up to the max depth --}}
]"
{{-- root-path="Men > Tops" --}}
v-bind:attributes="listingSlotProps.categoryAttributes"
@attributes(['root-path' => $rootPath])
show-more
>
<template v-slot="{ items, refine, createURL }">
{{-- See the HierarchicalMenuList.vue --}}
<recursion :data="items" v-slot="{ data, components }">
<ul>
<li class="pl-3" v-for="(item, index) in data" :key="item.value">
<a
:href="createURL(item.value)"
:class="{ 'font-bold': item.isRefined }"
v-on:click.exact.left.prevent="refine(item.value)"
>
@{{ item.label }}
(@{{ item.count }})
</a>
<x-rapidez::filter.heading>
<x-slot:title>
@lang('Category')
</x-slot:title>
<recursion :data="items" v-slot="{ data, components }">
<ul>
<li class="pl-3" v-for="(item, index) in data" :key="item.value">
<a
:href="createURL(item.value)"
:class="{ 'font-bold': item.isRefined }"
v-on:click.exact.left.prevent="refine(item.value)"
>
@{{ item.label }}
(@{{ item.count }})
</a>

<component :is="components[index]" />
</li>
</ul>
</recursion>
<component :is="components[index]" />
</li>
</ul>
</recursion>
</x-rapidez::filter.heading>
</template>
</ais-hierarchical-menu>

{{--
<reactive-component
component-id="category"
:default-query="() => ({ aggs: {
categories: { terms: { field: 'categories.keyword' } },
category_paths: { terms: { field: 'category_paths.keyword' } }
}})"
u-r-l-params
>
<div slot-scope="{ aggregations, setQuery, value }">
<category-filter :aggregations="aggregations" :value="value" :set-query="setQuery">
<div slot-scope="{ hasResults, results }" class="pb-4">
<x-rapidez::filter.heading>
<x-slot:title>
@lang('Category')
</x-slot:title>
<input class="hidden" type="radio" name="category" value="" :checked="!value" v-on:change="setQuery({})"/>
<ul>
<category-filter-category
v-for="category in results"
v-bind:key="category.key"
:category="category"
:value="(value || config.category?.entity_id) + ''"
:set-query="setQuery"
/>
</ul>
</x-rapidez::filter.heading>
</div>
</category-filter>
</div>
</reactive-component>
--}}
2 changes: 2 additions & 0 deletions src/Http/ViewComposers/ConfigComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Str;
use Illuminate\View\View;
use Rapidez\Core\Facades\Rapidez;
use Rapidez\Core\Models\Category;

// TODO: Can we improve anything in this file?
// It doesn't feel very clean currently.
Expand Down Expand Up @@ -57,6 +58,7 @@ public function getConfig(): array
'show_tax' => (bool) Rapidez::config('tax/display/type', 1),
'grid_per_page' => Rapidez::config('catalog/frontend/grid_per_page', 12),
'grid_per_page_values' => explode(',', Rapidez::config('catalog/frontend/grid_per_page_values', '12,24,36')),
'max_category_level' => Category::withoutGlobalScopes()->selectRaw('MAX(level) as maxlevel')->first()->maxlevel,

// TODO: For the products we've the `rapidez.index` config
// set from the `src/Rapidez.php` which is accessible
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public function getParentcategoriesAttribute()
$categoryIds = explode('/', $this->path);
$categoryIds = array_slice($categoryIds, array_search(config('rapidez.root_category_id'), $categoryIds) + 1);

return ! $categoryIds ? [] : Category::whereIn($this->getTable() . '.entity_id', $categoryIds)
->orderByRaw('FIELD(' . $this->getTable() . '.entity_id,' . implode(',', $categoryIds) . ')')
return ! $categoryIds ? [] : Category::whereIn($this->getQualifiedKeyName(), $categoryIds)
->orderByRaw('FIELD(' . $this->getQualifiedKeyName() . ',' . implode(',', $categoryIds) . ')')
->get();
}

Expand Down

0 comments on commit 46524a3

Please sign in to comment.