Skip to content

Commit

Permalink
Merge pull request #41 from Setono/rename-facets-to-f
Browse files Browse the repository at this point in the history
Rename 'facets' query parameter to 'f'. Also making a namespace on the classes used in the html elements
  • Loading branch information
loevgaard authored Dec 5, 2024
2 parents 1b48c88 + d27d1a4 commit b71f342
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 28 deletions.
5 changes: 2 additions & 3 deletions src/Engine/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class SearchRequest

public const QUERY_PARAMETER_SORT = 's';

public const QUERY_PARAMETER_FILTER = 'facets';
public const QUERY_PARAMETER_FILTER = 'f';

// todo we need the hits per page here
public function __construct(
Expand All @@ -40,9 +40,8 @@ public static function fromRequest(Request $request): self
$sort = null;
}

// todo rename facets to f or filters?
/** @var array<string, mixed> $filters */
$filters = $request->query->all('facets');
$filters = $request->query->all(self::QUERY_PARAMETER_FILTER);

return new self($q, $filters, $page, $sort);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Builder/SearchFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function build(SearchResult $searchResult): FormInterface

// todo this nesting makes the URLs uglier. Can we do something else?
// todo could this be fixed by adding the facet forms to some kind of collection?
$facetsFormBuilder = $this->formFactory->createNamedBuilder('facets');
$facetsFormBuilder = $this->formFactory->createNamedBuilder(SearchRequest::QUERY_PARAMETER_FILTER);

/**
* Here is an example of the facet stats array
Expand Down
7 changes: 4 additions & 3 deletions src/Resources/public/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,17 @@ class SearchManager {

field.dispatchEvent(new CustomEvent('search:form-changed', { bubbles: true }));

if(field.name.startsWith('facets')) {
field.dispatchEvent(new CustomEvent('search:facet-changed', { bubbles: true }));
// todo would be better to have a way to check the type of the field directly on the field, e.g. with a data attribute
if(field.closest('.ssm-filter') !== null) {
field.dispatchEvent(new CustomEvent('search:filter-changed', { bubbles: true }));
} else if(field.name === 'p') {
field.dispatchEvent(new CustomEvent('search:page-changed', { bubbles: true }));
} else if(field.classList.contains('sort')) {
field.dispatchEvent(new CustomEvent('search:sort-changed', { bubbles: true }));
}
});

this.#form.addEventListener('search:facet-changed', (event) => this.#options.onFacetChange.bind(this, this.#form, event.target)());
this.#form.addEventListener('search:filter-changed', (event) => this.#options.onFacetChange.bind(this, this.#form, event.target)());
this.#form.addEventListener('search:page-changed', (event) => this.#options.onSortChange.bind(this, this.#form, event.target)());
this.#form.addEventListener('search:sort-changed', (event) => this.#options.onSortChange.bind(this, this.#form, event.target)());
this.#form.addEventListener('submit', () => this.#options.onSubmit.bind(this, this.#form)());
Expand Down
10 changes: 5 additions & 5 deletions src/Resources/views/form/search_theme.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% block setono_sylius_meilisearch_facet_checkbox_row %}
<div class="facet facet-checkbox">
<div class="ssm-filter ssm-filter-checkbox">
{{ form_label(form) }}
<ul>
<li>{{- form_widget(form) -}}{{ form_label(form) }}</li>
Expand All @@ -8,10 +8,10 @@
{% endblock %}

{% block setono_sylius_meilisearch_facet_range_row %}
<div class="facet facet-range">
<div class="ssm-filter ssm-filter-range">
{{ form_label(form) }}
{# todo these input fields should be prefilled with min and max values, but should not be submitted if those values are equal to the default value #}
<div class="facet-range-widget">
<div class="ssm-filter-range-widget">
<div>
{{ form_label(form.min) }}
{{ form_widget(form.min) }}
Expand All @@ -25,7 +25,7 @@
{% endblock %}

{% block setono_sylius_meilisearch_facet_choice_row %}
<div class="facet facet-choice">
<div class="ssm-filter ssm-filter-choice">
{{ form_label(form) }}
<ul>
{%- for child in form %}
Expand All @@ -39,7 +39,7 @@
{% endblock %}

{% block setono_sylius_meilisearch_page_choice_widget %}
<nav class="pagination">
<nav class="ssm-pagination">
<ol>
{%- for child in form %}
<li{{ child.vars.label == '__current' ? ' style="display:none"' : '' }}>
Expand Down
1 change: 0 additions & 1 deletion src/Resources/views/search/_facets.html.twig

This file was deleted.

1 change: 1 addition & 0 deletions src/Resources/views/search/_filters.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ form_widget(searchForm.f, { 'attr': { 'class': 'ssm-filters' } }) }}
2 changes: 1 addition & 1 deletion src/Resources/views/search/_results.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{ form_start(searchForm, { 'attr': { 'id': 'search-form' } }) }}
<div class="ui stackable grid">
<div class="four wide column">
{{ include('@SetonoSyliusMeilisearchPlugin/search/_facets.html.twig') }}
{{ include('@SetonoSyliusMeilisearchPlugin/search/_filters.html.twig') }}
</div>
<div class="twelve wide column">
<div class="ui grid">
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/search/_sorting.html.twig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{ form_widget(searchForm.s, { 'attr': { 'class': 'sort' } }) }}
{{ form_widget(searchForm.s, { 'attr': { 'class': 'ssm-sort' } }) }}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
}
/**
* Facets
* Filters
*/
#facets {
.ssm-filters {
display: flex;
flex-direction: column;
gap: 30px;
}
.facet > label {
.ssm-filter > label {
font-size: 14px !important;
text-transform: uppercase;
font-weight: bold;
Expand All @@ -34,7 +34,7 @@
margin: 0 0 7px 0 !important;
}
.facet ul {
.ssm-filter ul {
list-style-type: none;
padding: 0;
margin: 0;
Expand All @@ -43,26 +43,26 @@
gap: 7px;
}
.facet li {
.ssm-filter li {
display: flex;
gap: 10px;
align-items: center;
}
.facet li label {
.ssm-filter li label {
margin: 0 !important;
padding: 0 !important;
font-size: 14px !important;
}
.facet-range .facet-range-widget {
.ssm-filter-range .ssm-filter-range-widget {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
gap: 10px;
}
.facet-range input {
.ssm-filter-range input {
width: 100%;
padding: 5px 10px !important;
border: 1px solid #cacaca;
Expand All @@ -72,7 +72,7 @@
/**
* Pagination
*/
nav.pagination ol {
.ssm-pagination ol {
list-style: none;
margin: 0;
padding: 0;
Expand All @@ -84,13 +84,13 @@
/**
* Sort
*/
.sort {
.ssm-sort {
padding: 0 !important;
margin: 0 !important;
text-align-last: right;
}
.sort option {
.ssm-sort option {
direction: rtl;
}
</style>
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/SearchFormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testItCreatesFormForSearchResultsWithProperlySortedFacetValues()

$form = $searchFormBuilder->build($result);
/** @var ChoiceListInterface $choiceList */
$choiceList = $form->get('facets')->get('size')->getConfig()->getAttributes()['choice_list'];
$choiceList = $form->get(SearchRequest::QUERY_PARAMETER_FILTER)->get('size')->getConfig()->getAttributes()['choice_list'];

self::assertSame(
['S' => 'S', 'M' => 'M', 'L' => 'L', 'XL' => 'XL', 'XXL' => 'XXL'],
Expand Down
8 changes: 7 additions & 1 deletion tests/Unit/Engine/SearchRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public function it_creates_search_request(): void
*/
public function it_creates_from_request(): void
{
$request = Request::create('/search', 'GET', ['q' => 'jeans', 'facets' => ['brand' => ['Celsius small', 'You are breathtaking'], 'price' => ['min' => '30', 'max' => '45']], 'p' => 2, 's' => 'price:asc']);
$request = Request::create('/search', 'GET', [
SearchRequest::QUERY_PARAMETER_SEARCH => 'jeans',
SearchRequest::QUERY_PARAMETER_FILTER => ['brand' => ['Celsius small', 'You are breathtaking'], 'price' => ['min' => '30', 'max' => '45']],
SearchRequest::QUERY_PARAMETER_PAGE => 2,
SearchRequest::QUERY_PARAMETER_SORT => 'price:asc',
]);

$searchRequest = SearchRequest::fromRequest($request);

self::assertSame('jeans', $searchRequest->query);
Expand Down

0 comments on commit b71f342

Please sign in to comment.