Skip to content

Commit

Permalink
Fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pkolt committed Dec 5, 2024
1 parent e3fe6e2 commit 9000b2e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/services/tags/api.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { db } from '../db';
import { Tag, TagWithCount } from './types';
import { deleteWord, getWordsByTag, getCountWordByTag, updateWord, getCountAllWords } from '../words/api';
import { deleteWord, getWordsByTag, getCountWordByTag, updateWord, getWordList } from '../words/api';
import { EMPTY_TAG_ID, EMPTY_TAG_NAME, QueryKey, StoreName } from '../constants';
import { queryClient } from '../queryClient';

// IndexedDB isn't allowing filtering by an empty array
const getCountWordsWithoutTags = async (): Promise<number> => {
const allWords = await getWordList();
return allWords.filter((it) => it.tags.length === 0).length;
};

export interface GetTagListParams {
emptyTag?: boolean;
}

export const getTagList = async ({ emptyTag }: GetTagListParams): Promise<TagWithCount[]> => {
const tags = await db.getAll(StoreName.Tags);
const result: TagWithCount[] = [];
let countWordsWithTags = 0;
for (const tag of tags) {
const count = await getCountWordByTag(tag.id);
result.push({ ...tag, count });
countWordsWithTags += count;
}
if (emptyTag) {
// IndexedDB isn't allowing filtering by an empty array
const countAllWords = await getCountAllWords();
const countWordsWithoutTags = countAllWords - countWordsWithTags;
const countWordsWithoutTags = await getCountWordsWithoutTags();
if (countWordsWithoutTags) {
result.push({
id: EMPTY_TAG_ID,
Expand Down
4 changes: 0 additions & 4 deletions src/services/words/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ export const getCountWordByTag = async (tagId: string): Promise<number> => {
return db.countFromIndex(StoreName.Words, 'by-tags', IDBKeyRange.only(tagId));
};

export const getCountAllWords = async (): Promise<number> => {
return db.count(StoreName.Words);
};

export const getWordsByTag = async (tagId: string): Promise<Word[]> => {
return db.getAllFromIndex(StoreName.Words, 'by-tags', IDBKeyRange.only(tagId));
};

0 comments on commit 9000b2e

Please sign in to comment.