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

Rename 'sort' query parameter to 's' #40

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 11 additions & 4 deletions src/Engine/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

final class SearchRequest
{
public const QUERY_PARAMETER_SEARCH = 'q';

public const QUERY_PARAMETER_PAGE = 'p';

public const QUERY_PARAMETER_SORT = 's';

public const QUERY_PARAMETER_FILTER = 'facets';

// todo we need the hits per page here
public function __construct(
public readonly ?string $query,
Expand All @@ -20,15 +28,14 @@ public function __construct(

public static function fromRequest(Request $request): self
{
$q = $request->query->get('q');
$q = $request->query->get(self::QUERY_PARAMETER_SEARCH);
if (!is_string($q)) {
$q = null;
}

$page = max(1, (int) $request->query->get('p', 1));
$page = max(1, (int) $request->query->get(self::QUERY_PARAMETER_PAGE, 1));

// todo rename sort to s?
$sort = $request->query->get('sort');
$sort = $request->query->get(self::QUERY_PARAMETER_SORT);
if (!is_string($sort)) {
$sort = null;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Form/Builder/SearchFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Meilisearch\Search\SearchResult;
use Setono\SyliusMeilisearchPlugin\Config\Index;
use Setono\SyliusMeilisearchPlugin\Document\Metadata\MetadataFactoryInterface;
use Setono\SyliusMeilisearchPlugin\Engine\SearchRequest;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
Expand Down Expand Up @@ -118,7 +119,7 @@

$searchFormBuilder->add($facetsFormBuilder);

$searchFormBuilder->add('sort', ChoiceType::class, [
$searchFormBuilder->add(SearchRequest::QUERY_PARAMETER_SORT, ChoiceType::class, [

Check warning on line 122 in src/Form/Builder/SearchFormBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Form/Builder/SearchFormBuilder.php#L122

Added line #L122 was not covered by tests
'choices' => [
'Cheapest first' => 'price:asc',
'Biggest discount' => 'discount:desc',
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/public/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class SearchManager {
field.dispatchEvent(new CustomEvent('search:facet-changed', { bubbles: true }));
} else if(field.name === 'p') {
field.dispatchEvent(new CustomEvent('search:page-changed', { bubbles: true }));
} else if(field.name.startsWith('sort')) {
} else if(field.classList.contains('sort')) {
field.dispatchEvent(new CustomEvent('search:sort-changed', { bubbles: true }));
}
});
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.sort) }}
{{ form_widget(searchForm.s, { 'attr': { 'class': 'sort' } }) }}
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@
/**
* Sort
*/
#sort {
.sort {
padding: 0 !important;
margin: 0 !important;
text-align-last: right;
}

#sort option {
.sort option {
direction: rtl;
}
</style>
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Engine/SearchRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ 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, 'sort' => 'price:asc']);
$request = Request::create('/search', 'GET', ['q' => 'jeans', 'facets' => ['brand' => ['Celsius small', 'You are breathtaking'], 'price' => ['min' => '30', 'max' => '45']], 'p' => 2, 's' => 'price:asc']);
$searchRequest = SearchRequest::fromRequest($request);

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