search with parameters
This commit is contained in:
@@ -1,13 +1,46 @@
|
||||
{# Search / listing results, swapped in by htmx on each query and rendered
|
||||
server-side on first load. Mirrors the empty-state handling of index.html. #}
|
||||
{% if products | length > 0 %}
|
||||
{% include "shop/_product_grid.html" %}
|
||||
{% elif query and query != "" %}
|
||||
<div class="rounded-radius border border-outline px-6 py-16 text-center text-on-surface/70 dark:border-outline-dark dark:text-on-surface-dark/70">
|
||||
{{ t(key="search-empty", lang=lang | default(value='sk')) }} <span class="font-medium text-on-surface-strong dark:text-on-surface-dark-strong">{{ query }}</span>
|
||||
{# Results region: swapped in by htmx on each query/filter change and rendered
|
||||
server-side on first load. Holds the result summary, the product grid and
|
||||
pagination. #}
|
||||
{% set L = lang | default(value='sk') %}
|
||||
<div class="space-y-4">
|
||||
<p class="text-sm text-on-surface/70 dark:text-on-surface-dark/70" aria-live="polite">
|
||||
{{ t(key="results-count", lang=L, count=total) }}{% if query and query != "" %} · “{{ query }}”{% endif %}
|
||||
</p>
|
||||
|
||||
{% if products | length > 0 %}
|
||||
{% include "shop/_product_grid.html" %}
|
||||
|
||||
{% if pages > 1 %}
|
||||
<nav class="flex items-center justify-center gap-2 pt-2" aria-label="{{ t(key='pagination', lang=L) }}">
|
||||
{% if has_prev %}
|
||||
<button type="button"
|
||||
hx-get="/search?{% if query_base %}{{ query_base }}&{% endif %}page={{ prev_page }}"
|
||||
hx-target="#shop-results" hx-swap="innerHTML" hx-push-url="true"
|
||||
class="rounded-radius border border-outline px-3 py-1.5 text-sm font-medium text-on-surface transition hover:bg-primary/5 hover:text-primary dark:border-outline-dark dark:text-on-surface-dark dark:hover:text-primary-dark">
|
||||
{{ t(key="prev", lang=L) }}
|
||||
</button>
|
||||
{% endif %}
|
||||
<span class="px-2 text-sm text-on-surface/70 dark:text-on-surface-dark/70">
|
||||
{{ t(key="page-of", lang=L, page=page, pages=pages) }}
|
||||
</span>
|
||||
{% if has_next %}
|
||||
<button type="button"
|
||||
hx-get="/search?{% if query_base %}{{ query_base }}&{% endif %}page={{ next_page }}"
|
||||
hx-target="#shop-results" hx-swap="innerHTML" hx-push-url="true"
|
||||
class="rounded-radius border border-outline px-3 py-1.5 text-sm font-medium text-on-surface transition hover:bg-primary/5 hover:text-primary dark:border-outline-dark dark:text-on-surface-dark dark:hover:text-primary-dark">
|
||||
{{ t(key="next", lang=L) }}
|
||||
</button>
|
||||
{% endif %}
|
||||
</nav>
|
||||
{% endif %}
|
||||
|
||||
{% elif query and query != "" %}
|
||||
<div class="rounded-radius border border-outline px-6 py-16 text-center text-on-surface/70 dark:border-outline-dark dark:text-on-surface-dark/70">
|
||||
{{ t(key="search-empty", lang=L) }} <span class="font-medium text-on-surface-strong dark:text-on-surface-dark-strong">{{ query }}</span>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="rounded-radius border border-outline px-6 py-16 text-center text-on-surface/70 dark:border-outline-dark dark:text-on-surface-dark/70">
|
||||
{{ t(key="shop-empty", lang=L) }}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="rounded-radius border border-outline px-6 py-16 text-center text-on-surface/70 dark:border-outline-dark dark:text-on-surface-dark/70">
|
||||
{{ t(key="shop-empty", lang=lang | default(value='sk')) }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
@@ -4,39 +4,103 @@
|
||||
{% block title %}{{ t(key="nav-shop", lang=lang | default(value='sk')) }}{% endblock title %}
|
||||
|
||||
{% block content %}
|
||||
<div class="space-y-8">
|
||||
<header class="space-y-4">
|
||||
<div class="space-y-2">
|
||||
<h1 class="text-3xl font-bold text-on-surface-strong dark:text-on-surface-dark-strong">{{ t(key="shop-title", lang=lang | default(value='sk')) }}</h1>
|
||||
<p class="text-on-surface/70 dark:text-on-surface-dark/70">{{ t(key="shop-subtitle", lang=lang | default(value='sk')) }}</p>
|
||||
</div>
|
||||
{% set L = lang | default(value='sk') %}
|
||||
<div class="space-y-6">
|
||||
<header class="space-y-1">
|
||||
<h1 class="text-3xl font-bold text-on-surface-strong dark:text-on-surface-dark-strong">{{ t(key="shop-title", lang=L) }}</h1>
|
||||
<p class="text-on-surface/70 dark:text-on-surface-dark/70">{{ t(key="shop-subtitle", lang=L) }}</p>
|
||||
</header>
|
||||
|
||||
{# Live search: htmx GETs /search as the customer types (debounced) and
|
||||
swaps only the results below. hx-push-url keeps the URL shareable; the
|
||||
spinner shows while a request is in flight. Degrades to a normal GET form
|
||||
submit when JS/htmx is unavailable. #}
|
||||
<form action="/search" method="get" role="search"
|
||||
class="relative max-w-md"
|
||||
hx-get="/search" hx-target="#shop-results" hx-swap="innerHTML"
|
||||
hx-trigger="input changed delay:300ms from:input[name='q'], submit"
|
||||
hx-push-url="true" hx-indicator="#search-spinner">
|
||||
{# One form drives the whole listing. htmx re-runs /search and swaps only the
|
||||
results region; the toolbar keeps its own DOM state. Triggers: live (debounced)
|
||||
typing in the search box, immediate on any select/checkbox change, and submit
|
||||
(Enter / Apply) for the price band. Degrades to a plain GET form without JS. #}
|
||||
<form action="/search" method="get" role="search"
|
||||
hx-get="/search" hx-target="#shop-results" hx-swap="innerHTML"
|
||||
hx-push-url="true" hx-indicator="#search-spinner"
|
||||
hx-trigger="submit, change, keyup changed delay:350ms from:input[name='q']"
|
||||
class="space-y-3">
|
||||
|
||||
<!-- search box -->
|
||||
<div class="relative max-w-xl">
|
||||
<span class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3 text-on-surface/50 dark:text-on-surface-dark/50">
|
||||
{{ ui::icon(name="search", size="size-5") }}
|
||||
</span>
|
||||
<input type="search" name="q" value="{{ query | default(value='') }}"
|
||||
autocomplete="off"
|
||||
placeholder="{{ t(key='search-placeholder', lang=lang | default(value='sk')) }}"
|
||||
aria-label="{{ t(key='search-placeholder', lang=lang | default(value='sk')) }}"
|
||||
<input type="search" name="q" value="{{ query | default(value='') }}" autocomplete="off"
|
||||
placeholder="{{ t(key='search-placeholder', lang=L) }}"
|
||||
aria-label="{{ t(key='search-placeholder', lang=L) }}"
|
||||
class="w-full rounded-radius border border-outline bg-surface py-2 pl-10 pr-10 text-sm text-on-surface placeholder:text-on-surface/50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary dark:border-outline-dark dark:bg-surface-dark-alt dark:text-on-surface-dark dark:placeholder:text-on-surface-dark/50 dark:focus-visible:outline-primary-dark" />
|
||||
<span id="search-spinner"
|
||||
class="htmx-indicator pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3 text-on-surface/50 dark:text-on-surface-dark/50">
|
||||
<span id="search-spinner" class="htmx-indicator pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3 text-on-surface/50 dark:text-on-surface-dark/50">
|
||||
<svg class="size-4 animate-spin" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" aria-hidden="true">
|
||||
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
|
||||
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 0 1 8-8V0C5.4 0 0 5.4 0 12h4Z"></path>
|
||||
</svg>
|
||||
</span>
|
||||
</form>
|
||||
</header>
|
||||
</div>
|
||||
|
||||
<!-- filter toolbar -->
|
||||
<div class="flex flex-wrap items-end gap-3 rounded-radius border border-outline bg-surface-alt p-3 dark:border-outline-dark dark:bg-surface-dark-alt">
|
||||
<!-- category -->
|
||||
<label class="flex flex-col gap-1 text-xs font-medium text-on-surface/70 dark:text-on-surface-dark/70">
|
||||
{{ t(key="filter-category", lang=L) }}
|
||||
<select name="category"
|
||||
class="rounded-radius border border-outline bg-surface px-2 py-1.5 text-sm text-on-surface focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary dark:border-outline-dark dark:bg-surface-dark dark:text-on-surface-dark">
|
||||
<option value="all"{% if selected_category == "all" %} selected{% endif %}>{{ t(key="filter-all-categories", lang=L) }}</option>
|
||||
{% for g in category_groups %}
|
||||
<option value="{{ g.id }}"{% if selected_category_id == g.id %} selected{% endif %}>{{ g.name }} ({{ g.count }})</option>
|
||||
{% for ch in g.children %}
|
||||
<option value="{{ ch.id }}"{% if selected_category_id == ch.id %} selected{% endif %}> — {{ ch.name }} ({{ ch.count }})</option>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% if uncategorized_count > 0 %}
|
||||
<option value="none"{% if selected_category == "none" %} selected{% endif %}>{{ t(key="filter-uncategorized", lang=L) }} ({{ uncategorized_count }})</option>
|
||||
{% endif %}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<!-- sort -->
|
||||
<label class="flex flex-col gap-1 text-xs font-medium text-on-surface/70 dark:text-on-surface-dark/70">
|
||||
{{ t(key="sort-label", lang=L) }}
|
||||
<select name="sort"
|
||||
class="rounded-radius border border-outline bg-surface px-2 py-1.5 text-sm text-on-surface focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary dark:border-outline-dark dark:bg-surface-dark dark:text-on-surface-dark">
|
||||
{% for opt in ["relevance", "newest", "price_asc", "price_desc", "name_asc", "name_desc"] %}
|
||||
<option value="{{ opt }}"{% if sort == opt %} selected{% endif %}>{{ t(key="sort-" ~ opt, lang=L) }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
<!-- price band -->
|
||||
<label class="flex flex-col gap-1 text-xs font-medium text-on-surface/70 dark:text-on-surface-dark/70">
|
||||
{{ t(key="filter-price", lang=L) }}
|
||||
<span class="flex items-center gap-1">
|
||||
<input type="number" name="min_price" min="0" step="0.01" inputmode="decimal"
|
||||
value="{{ min_price | default(value='') }}" placeholder="{{ price_floor }}"
|
||||
aria-label="{{ t(key='filter-price-from', lang=L) }}"
|
||||
class="w-24 rounded-radius border border-outline bg-surface px-2 py-1.5 text-sm text-on-surface dark:border-outline-dark dark:bg-surface-dark dark:text-on-surface-dark" />
|
||||
<span class="text-on-surface/50 dark:text-on-surface-dark/50">–</span>
|
||||
<input type="number" name="max_price" min="0" step="0.01" inputmode="decimal"
|
||||
value="{{ max_price | default(value='') }}" placeholder="{{ price_ceil }}"
|
||||
aria-label="{{ t(key='filter-price-to', lang=L) }}"
|
||||
class="w-24 rounded-radius border border-outline bg-surface px-2 py-1.5 text-sm text-on-surface dark:border-outline-dark dark:bg-surface-dark dark:text-on-surface-dark" />
|
||||
</span>
|
||||
</label>
|
||||
|
||||
<!-- in stock -->
|
||||
<label class="flex items-center gap-2 pb-1.5 text-sm text-on-surface dark:text-on-surface-dark">
|
||||
<input type="checkbox" name="in_stock" value="1"{% if in_stock %} checked{% endif %}
|
||||
class="size-4 rounded border-outline text-primary focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary dark:border-outline-dark dark:text-primary-dark" />
|
||||
{{ t(key="filter-in-stock", lang=L) }}
|
||||
</label>
|
||||
|
||||
<div class="ml-auto flex items-end gap-2">
|
||||
{{ ui::button(label=t(key="filter-apply", lang=L), type="submit", variant="secondary") }}
|
||||
<a href="/shop" hx-get="/search" hx-target="#shop-results" hx-push-url="true"
|
||||
class="self-end pb-1.5 text-sm font-medium text-on-surface/70 underline-offset-2 transition hover:text-primary hover:underline dark:text-on-surface-dark/70 dark:hover:text-primary-dark">
|
||||
{{ t(key="filter-clear", lang=L) }}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div id="shop-results">
|
||||
{% include "shop/_results.html" %}
|
||||
|
||||
Reference in New Issue
Block a user