From 097acd59546a428c7a11d20625368fe58648d66f Mon Sep 17 00:00:00 2001 From: make-github-pseudonymous-again <5165674+make-github-pseudonymous-again@users.noreply.github.com> Date: Wed, 25 Dec 2024 14:03:48 +0100 Subject: [PATCH] :recycle: refactor(books): Move hooks to `/ui`. To minimize server refreshes when developing. --- imports/api/books.ts | 11 ----------- imports/ui/books/BookRenamingDialog.tsx | 4 +++- imports/ui/books/BooksList.tsx | 2 +- imports/ui/books/useBooks.ts | 7 +++++++ imports/ui/books/useBooksFind.ts | 8 ++++++++ imports/ui/consultations/ConsultationForm.tsx | 3 ++- 6 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 imports/ui/books/useBooks.ts create mode 100644 imports/ui/books/useBooksFind.ts diff --git a/imports/api/books.ts b/imports/api/books.ts index 462d3a797..03c71dd3f 100644 --- a/imports/api/books.ts +++ b/imports/api/books.ts @@ -3,8 +3,6 @@ import addYears from 'date-fns/addYears'; import schema from '../lib/schema'; -import makeQuery from './makeQuery'; -import makeObservedQueryHook from './makeObservedQueryHook'; import { type NormalizedLine, normalizedLine, @@ -13,17 +11,8 @@ import { } from './string'; import {Books, collection} from './collection/books'; -import {BooksCache} from './collection/books/cache'; - -import publication from './publication/books/find'; -import cachePublication from './publication/books/observe'; import type TransactionDriver from './transaction/TransactionDriver'; -export const useBooks = makeQuery(Books, publication); - -// TODO rename to useObservedBooks -export const useBooksFind = makeObservedQueryHook(BooksCache, cachePublication); - const sanitizeInput = normalizedLineInput; const sanitize = normalizedLine; diff --git a/imports/ui/books/BookRenamingDialog.tsx b/imports/ui/books/BookRenamingDialog.tsx index 824119a22..88d96f749 100644 --- a/imports/ui/books/BookRenamingDialog.tsx +++ b/imports/ui/books/BookRenamingDialog.tsx @@ -4,9 +4,11 @@ import TagRenamingDialog from '../tags/TagRenamingDialog'; import rename from '../../api/endpoint/books/rename'; -import {books, useBooksFind} from '../../api/books'; +import {books} from '../../api/books'; import {type BookDocument} from '../../api/collection/books'; +import useBooksFind from './useBooksFind'; + type Props = { readonly open: boolean; readonly onClose: () => void; diff --git a/imports/ui/books/BooksList.tsx b/imports/ui/books/BooksList.tsx index 79dec1145..72c27f931 100644 --- a/imports/ui/books/BooksList.tsx +++ b/imports/ui/books/BooksList.tsx @@ -8,7 +8,6 @@ import dateFormat from 'date-fns/format'; import TagList from '../tags/TagList'; -import {useBooks} from '../../api/books'; import {useSettingCached} from '../settings/hooks'; import YearJumper from '../navigation/YearJumper'; @@ -17,6 +16,7 @@ import FixedFab from '../button/FixedFab'; import BookCard from './BookCard'; import BooksDownloadDialog from './BooksDownloadDialog'; +import useBooks from './useBooks'; type Props = { readonly type?: string; diff --git a/imports/ui/books/useBooks.ts b/imports/ui/books/useBooks.ts new file mode 100644 index 000000000..9e1b23e71 --- /dev/null +++ b/imports/ui/books/useBooks.ts @@ -0,0 +1,7 @@ +import makeQuery from '../../api/makeQuery'; +import publication from '../../api/publication/books/find'; +import {Books} from '../../api/collection/books'; + +const useBooks = makeQuery(Books, publication); + +export default useBooks; diff --git a/imports/ui/books/useBooksFind.ts b/imports/ui/books/useBooksFind.ts new file mode 100644 index 000000000..ed059d393 --- /dev/null +++ b/imports/ui/books/useBooksFind.ts @@ -0,0 +1,8 @@ +import makeObservedQueryHook from '../../api/makeObservedQueryHook'; +import cachePublication from '../../api/publication/books/observe'; +import {BooksCache} from '../../api/collection/books/cache'; + +// TODO: rename to useObservedBooks +const useBooksFind = makeObservedQueryHook(BooksCache, cachePublication); + +export default useBooksFind; diff --git a/imports/ui/consultations/ConsultationForm.tsx b/imports/ui/consultations/ConsultationForm.tsx index fea7f542b..9faa47f69 100644 --- a/imports/ui/consultations/ConsultationForm.tsx +++ b/imports/ui/consultations/ConsultationForm.tsx @@ -20,11 +20,12 @@ import CircularProgress from '@mui/material/CircularProgress'; import TextField from '../input/TextField'; import useBookStats from '../books/useBookStats'; -import {books, useBooksFind} from '../../api/books'; +import {books} from '../../api/books'; import AutocompleteWithSuggestions from '../input/AutocompleteWithSuggestions'; import makeSubstringSuggestions from '../input/makeSubstringSuggestions'; import CurrencyAmountInput from '../input/CurrencyAmountInput'; import {parsePositiveIntegerStrictOrUndefined} from '../../api/string'; +import useBooksFind from '../books/useBooksFind'; const useInBookNumberCollides = ( consultationId: string | undefined,