Skip to content

Commit

Permalink
fix: Sidebar now shows only active tags #104
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSciFier committed Jun 5, 2024
1 parent cab6909 commit 71901bb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion frontend/src/context/tagsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions server/db/sqlite/stores/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -44,13 +44,18 @@ 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 ")}`;
}

selectQuery += ` GROUP BY tags.id
ORDER BY tags.name`;

console.log(selectQuery);
return this.db.prepare(selectQuery).all(selectParams);
}

Expand Down
10 changes: 10 additions & 0 deletions server/routes/api/tags/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down

0 comments on commit 71901bb

Please sign in to comment.