From 71901bb04144a5a3820d7b29f886b20f27a720bb Mon Sep 17 00:00:00 2001 From: AlexSciFier <66871322+AlexSciFier@users.noreply.github.com> Date: Wed, 5 Jun 2024 15:19:38 +0300 Subject: [PATCH] fix: Sidebar now shows only active tags #104 --- frontend/src/context/tagsList.jsx | 2 +- server/db/sqlite/stores/tags.js | 9 +++++++-- server/routes/api/tags/index.js | 10 ++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/frontend/src/context/tagsList.jsx b/frontend/src/context/tagsList.jsx index 0760ef7..b33df6d 100644 --- a/frontend/src/context/tagsList.jsx +++ b/frontend/src/context/tagsList.jsx @@ -17,7 +17,7 @@ export function TagsListProvider({ children }) { setError(undefined); setIsLoading(true); abortController.current = new AbortController(); - let res = await getJSON(`/api/tags`, abortController.current.signal); + let res = await getJSON(`/api/tags/active`, abortController.current.signal); if (res.ok) { setTags(await res.json()); } else { diff --git a/server/db/sqlite/stores/tags.js b/server/db/sqlite/stores/tags.js index a1b60cb..de9dd75 100644 --- a/server/db/sqlite/stores/tags.js +++ b/server/db/sqlite/stores/tags.js @@ -27,9 +27,9 @@ export default class TagsStore { return result && result.count > 0; } - getAll(name, userId) { + getAll(name, userId, onlyActive = false) { let selectQuery = `SELECT - id, name + tags.id, tags.name FROM tags`; let selectParams = {}; @@ -44,6 +44,10 @@ export default class TagsStore { selectParams.name = `%${name}%`; } + if (onlyActive) { + selectQuery += ` RIGHT JOIN bookmarksTags ON tags.id = bookmarksTags.tagId`; + } + if (conditions.length > 0) { selectQuery += ` WHERE ${conditions.join(" AND ")}`; } @@ -51,6 +55,7 @@ export default class TagsStore { selectQuery += ` GROUP BY tags.id ORDER BY tags.name`; + console.log(selectQuery); return this.db.prepare(selectQuery).all(selectParams); } diff --git a/server/routes/api/tags/index.js b/server/routes/api/tags/index.js index ddb760d..5ae9f7b 100644 --- a/server/routes/api/tags/index.js +++ b/server/routes/api/tags/index.js @@ -31,6 +31,16 @@ export default async function (fastify, opts) { } ); + fastify.get( + "/active", + { preHandler: requireSession(true, true, false) }, + async function (request, reply) { + let { q } = request.query; + const user = appContext.request.get(appRequestsKeys.Session); + return appContext.stores.tags.getAll(q, user.userId, true); + } + ); + fastify.get( "/:id", { preHandler: requireSession(true, true, false) },