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

fix: handle aggregation funct on FE #883

Merged
merged 4 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 1 addition & 9 deletions app/Http/Controllers/NewCatalogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ class NewCatalogController extends Controller
*/
public function index()
{
$yearLimits = Cache::remember(
'items.year-limits',
now()->addHours(1),
fn() => Item::getYearLimits()
);

return view('frontend.catalog-new.index-new', [
'yearLimits' => $yearLimits,
]);
return view('frontend.catalog-new.index-new');
}
}
25 changes: 0 additions & 25 deletions app/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,31 +144,6 @@ public function getCitation()
return $this->getTitleWithAuthors() . ', ' . $this->getDatingFormated() . ', ' . $this->gallery . ', ' . $this->getUrl();
}

// Get limits from elastic, because date_earliest and date_latest
// are not indexed in DB
public static function getYearLimits()
{
$searchResult = self::searchQuery(Query::matchAll())
->size(false)
->aggregate('min', [
'min' => [
'field' => 'date_earliest',
],
])
->aggregate('max', [
'max' => [
'field' => 'date_latest',
],
])
->execute()
->raw();

return [
'min' => $searchResult['aggregations']['min']['value'],
'max' => $searchResult['aggregations']['max']['value'],
];
}

public static function loadValidatorMetadata(ClassMetadata $metadata) {
$metadata->addGetterConstraint('images', new Valid());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getParsedFilterFromUrl() {
}

function stringifyUrl({ url, params }) {
const { filter, size, terms, page } = params
const { filter, size, terms, page, min, max } = params
const {
yearRange,
author,
Expand Down Expand Up @@ -63,6 +63,8 @@ function stringifyUrl({ url, params }) {
sort: {
[sort]: SORT_DIRECTIONS[sort],
},
min,
max,
page,
terms,
size,
Expand Down Expand Up @@ -254,6 +256,8 @@ export default {
filter: this.query,
terms: AGGREGATIONS_TERMS,
size: AGGREGATIONS_SIZE,
min: { date_earliest: 'date_earliest' },
max: { date_latest: 'date_latest' },
},
}),
{ headers: this.apiHeaders }
Expand Down
17 changes: 13 additions & 4 deletions resources/js/components/catalog-new/SearchOptionsController.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ export default {
filteredOptions() {
// TODO better matching algorithm?
const query = this.search.toLowerCase()
return this.options
.filter((option) => option.value.toLowerCase().includes(query))
.map((option) => ({
const optionsWithSelected = [
...this.options.map((option) => ({
...option,
checked: this.selected.includes(option.value),
}))
})),
...this.selected
.filter((queryItem) =>
this.options.every((option) => option.value !== queryItem)
)
.map((selected) => ({ value: selected, count: 0, checked: true })),
]

return optionsWithSelected.filter((option) =>
option.value.toLowerCase().includes(query)
)
},
},
render() {
Expand Down
18 changes: 11 additions & 7 deletions resources/views/frontend/catalog-new/index-new.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,12 @@ class="tw-mb-3">
{{ trans('item.filter.clear') }}
</x-filter.reset_button>
</div>
<filter-new-year-slider :default-from="Number(query.yearRange?.from)"
:default-to="Number(query.yearRange?.to)" :min="{{ $yearLimits['min'] ?? 0 }}"
:max="{{ $yearLimits['max'] ?? now()->year }}"
@change="handleYearRangeChange">
<filter-new-year-slider
v-bind:default-from="Number(query.yearRange?.from)"
v-bind:default-to="Number(query.yearRange?.to)"
v-bind:min="Number(aggregations.date_earliest) - 5"
v-bind:max="Math.min(Number(aggregations.date_latest) + 5, new Date().getFullYear())"
v-on:change="handleYearRangeChange">
</filter-new-year-slider>
</div>
</div>
Expand Down Expand Up @@ -389,9 +391,11 @@ class="tw-ml-2"
@endslot
@slot('body')
<filter-new-year-slider class="tw-px-4"
:default-from="Number(query.yearRange?.from)" :default-to="Number(query.yearRange?.to)"
:min="{{ $yearLimits['min'] ?? 0 }}" :max="{{ $yearLimits['max'] ?? now()->year }}"
@change="handleYearRangeChange">
v-bind:default-from="Number(query.yearRange?.from)"
v-bind:default-to="Number(query.yearRange?.to)"
v-bind:min="Number(aggregations.date_earliest) - 5"
v-bind:max="Math.min(Number(aggregations.date_latest) + 5, new Date().getFullYear())"
v-on:change="handleYearRangeChange">
</filter-new-year-slider>
<div v-if="query.yearRange"
class="tw-flex tw-justify-center">
Expand Down
Loading