mirror of
https://github.com/wowlikon/LiB.git
synced 2026-02-04 12:31:09 +00:00
Сохранение фильтра жанров в url
This commit is contained in:
@@ -3,7 +3,7 @@ from typing import Optional
|
|||||||
|
|
||||||
# Конфигурация
|
# Конфигурация
|
||||||
USERNAME = "admin"
|
USERNAME = "admin"
|
||||||
PASSWORD = "n_ElBL9LTfTTgZSqHShqOg"
|
PASSWORD = "TzUlDpUCHutFa-oGCd1cBw"
|
||||||
BASE_URL = "http://localhost:8000"
|
BASE_URL = "http://localhost:8000"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,11 @@ $(document).ready(() => {
|
|||||||
let pageSize = 20;
|
let pageSize = 20;
|
||||||
let totalBooks = 0;
|
let totalBooks = 0;
|
||||||
|
|
||||||
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
|
const genreIdsFromUrl = urlParams.getAll("genre_id");
|
||||||
|
const authorIdsFromUrl = urlParams.getAll("author_id");
|
||||||
|
const searchFromUrl = urlParams.get("q");
|
||||||
|
|
||||||
Promise.all([
|
Promise.all([
|
||||||
fetch("/api/authors").then((response) => response.json()),
|
fetch("/api/authors").then((response) => response.json()),
|
||||||
fetch("/api/genres").then((response) => response.json()),
|
fetch("/api/genres").then((response) => response.json()),
|
||||||
@@ -18,15 +23,25 @@ $(document).ready(() => {
|
|||||||
.attr("data-name", author.name)
|
.attr("data-name", author.name)
|
||||||
.text(author.name)
|
.text(author.name)
|
||||||
.appendTo($dropdown);
|
.appendTo($dropdown);
|
||||||
|
|
||||||
|
if (authorIdsFromUrl.includes(String(author.id))) {
|
||||||
|
selectedAuthors.set(author.id, author.name);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const $list = $("#genres-list");
|
const $list = $("#genres-list");
|
||||||
genresData.genres.forEach((genre) => {
|
genresData.genres.forEach((genre) => {
|
||||||
|
const isChecked = genreIdsFromUrl.includes(String(genre.id));
|
||||||
|
|
||||||
|
if (isChecked) {
|
||||||
|
selectedGenres.set(genre.id, genre.name);
|
||||||
|
}
|
||||||
|
|
||||||
$("<li>")
|
$("<li>")
|
||||||
.addClass("mb-1")
|
.addClass("mb-1")
|
||||||
.html(
|
.html(
|
||||||
`<label class="custom-checkbox flex items-center">
|
`<label class="custom-checkbox flex items-center">
|
||||||
<input type="checkbox" data-id="${genre.id}" data-name="${genre.name}" />
|
<input type="checkbox" data-id="${genre.id}" data-name="${genre.name}" ${isChecked ? 'checked' : ''} />
|
||||||
<span class="checkmark"></span>
|
<span class="checkmark"></span>
|
||||||
${genre.name}
|
${genre.name}
|
||||||
</label>`,
|
</label>`,
|
||||||
@@ -57,6 +72,29 @@ $(document).ready(() => {
|
|||||||
params.append("genre_ids", id);
|
params.append("genre_ids", id);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function updateBrowserUrl() {
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
|
const searchQuery = $("#book-search-input").val().trim();
|
||||||
|
if (searchQuery.length >= 3) {
|
||||||
|
params.append("q", searchQuery);
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedAuthors.forEach((name, id) => {
|
||||||
|
params.append("author_id", id);
|
||||||
|
});
|
||||||
|
|
||||||
|
selectedGenres.forEach((name, id) => {
|
||||||
|
params.append("genre_id", id);
|
||||||
|
});
|
||||||
|
|
||||||
|
const newUrl = params.toString()
|
||||||
|
? `${window.location.pathname}?${params.toString()}`
|
||||||
|
: window.location.pathname;
|
||||||
|
|
||||||
|
window.history.replaceState({}, "", newUrl);
|
||||||
|
}
|
||||||
|
|
||||||
params.append("page", currentPage);
|
params.append("page", currentPage);
|
||||||
params.append("size", pageSize);
|
params.append("size", pageSize);
|
||||||
|
|
||||||
@@ -64,6 +102,8 @@ $(document).ready(() => {
|
|||||||
|
|
||||||
showLoadingState();
|
showLoadingState();
|
||||||
|
|
||||||
|
updateBrowserUrl();
|
||||||
|
|
||||||
fetch(url)
|
fetch(url)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|||||||
@@ -22,7 +22,6 @@
|
|||||||
<li><a href="/" class="hover:text-gray-200">Главная</a></li>
|
<li><a href="/" class="hover:text-gray-200">Главная</a></li>
|
||||||
<li><a href="/books" class="hover:text-gray-200">Книги</a></li>
|
<li><a href="/books" class="hover:text-gray-200">Книги</a></li>
|
||||||
<li><a href="/authors" class="hover:text-gray-200">Авторы</a></li>
|
<li><a href="/authors" class="hover:text-gray-200">Авторы</a></li>
|
||||||
<li><a href="/about" class="hover:text-gray-200">О нас</a></li>
|
|
||||||
<li><a href="/api" class="hover:text-gray-200">API</a></li>
|
<li><a href="/api" class="hover:text-gray-200">API</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
|
|||||||
Reference in New Issue
Block a user