From 82c7f328c767f08f29849b13ad5d01e648217705 Mon Sep 17 00:00:00 2001 From: Orkhan Ashrafov <70017511+webofpies@users.noreply.github.com> Date: Fri, 24 Jan 2025 22:30:42 +0400 Subject: [PATCH] refactor: bookstable-disable active and all buttons if there are no archived books --- .../book/components/BooksTable/BooksTable.jsx | 46 ++++++++++++++----- lute/api/book.py | 33 +++++++++++-- 2 files changed, 64 insertions(+), 15 deletions(-) diff --git a/frontend/src/features/book/components/BooksTable/BooksTable.jsx b/frontend/src/features/book/components/BooksTable/BooksTable.jsx index 1ef1b8a2..e159f8bd 100644 --- a/frontend/src/features/book/components/BooksTable/BooksTable.jsx +++ b/frontend/src/features/book/components/BooksTable/BooksTable.jsx @@ -71,7 +71,7 @@ function BooksTable({ languageChoices, tagChoices }) { fetchURL.searchParams.set("globalFilter", globalFilter ?? ""); fetchURL.searchParams.set("sorting", JSON.stringify(sorting ?? [])); - const { data } = useQuery({ + const { data: books } = useQuery({ queryKey: ["books", fetchURL.href], queryFn: async () => { const response = await fetch(fetchURL.href); @@ -102,18 +102,27 @@ function BooksTable({ languageChoices, tagChoices }) { } function handleEdit(id, data) { - editBookMutation.mutate({ - id: id, - data: getFormDataFromObj(data), - }); + editBookMutation.mutate( + { + id: id, + data: getFormDataFromObj(data), + }, + { + onSuccess: (response) => { + if (response.archivedCount === 0) { + setShelf("active"); + } + }, + } + ); } const table = useMantineReactTable({ ...defaultOptions, columns: columns, - data: data?.data || [], - rowCount: data?.total, + data: books?.data || [], + rowCount: books?.total, initialState: { ...defaultOptions.initialState, @@ -193,13 +202,17 @@ function BooksTable({ languageChoices, tagChoices }) { ], renderBottomToolbarCustomActions: () => ( - + ), }); return ( <> - {data && } + {books && } setEditedRow(null)} @@ -227,7 +240,8 @@ function EditModal({ row, onClose, editBookMutation }) { ); } -function ShelfSwitch({ shelf, onSetShelf }) { +function ShelfSwitch({ shelf, onSetShelf, archivedCount }) { + const showActiveOnly = archivedCount === 0 ? true : false; return ( diff --git a/lute/api/book.py b/lute/api/book.py index 16d51cca..893df611 100644 --- a/lute/api/book.py +++ b/lute/api/book.py @@ -8,7 +8,7 @@ from sqlalchemy import text as SQLText from lute.db import db -from lute.models.book import Text +from lute.models.book import Text, Book as BookModel from lute.models.repositories import BookRepository from lute.book.model import Book, Repository from lute.book.service import ( @@ -243,7 +243,14 @@ def get_books(): } ) - return jsonify({"data": response, "total": rowCount}) + return jsonify( + { + "data": response, + "total": rowCount, + "activeCount": activeCount, + "archivedCount": archivedCount, + } + ) @bp.route("/", methods=["GET"]) @@ -369,6 +376,16 @@ def edit_book(bookid): db.session.add(book) db.session.commit() + archived_count = len( + db.session.query(BookModel).filter(BookModel.archived == 1).all() + ) + + return ( + jsonify( + {"id": book.id, "title": book.title, "archivedCount": archived_count} + ), + 200, + ) if action == "unarchive": book = _find_book(bookid) @@ -376,6 +393,16 @@ def edit_book(bookid): db.session.add(book) db.session.commit() + archived_count = len( + db.session.query(BookModel).filter(BookModel.archived == 1).all() + ) + + return ( + jsonify( + {"id": book.id, "title": book.title, "archivedCount": archived_count} + ), + 200, + ) if action == "edit": files_dict = request.files.to_dict() @@ -401,7 +428,7 @@ def edit_book(bookid): _mark_book_as_stale(book) - return jsonify({"id": book.id}), 200 + return jsonify({"id": book.id, "title": book.title}), 200 @bp.route("/", methods=["DELETE"])