Skip to content

Commit

Permalink
refactor: bookstable-disable active and all buttons if there are no a…
Browse files Browse the repository at this point in the history
…rchived books
  • Loading branch information
webofpies committed Jan 24, 2025
1 parent a3f587d commit 82c7f32
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
46 changes: 34 additions & 12 deletions frontend/src/features/book/components/BooksTable/BooksTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -193,13 +202,17 @@ function BooksTable({ languageChoices, tagChoices }) {
],

renderBottomToolbarCustomActions: () => (
<ShelfSwitch shelf={shelf} onSetShelf={setShelf} />
<ShelfSwitch
shelf={shelf}
onSetShelf={setShelf}
archivedCount={books.archivedCount}
/>
),
});

return (
<>
{data && <MantineReactTable table={table} />}
{books && <MantineReactTable table={table} />}
<EditModal
row={editedRow}
onClose={() => setEditedRow(null)}
Expand Down Expand Up @@ -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 (
<Box pl={5}>
<SegmentedControl
Expand All @@ -236,8 +250,16 @@ function ShelfSwitch({ shelf, onSetShelf }) {
onChange={onSetShelf}
data={[
{ label: "Active", value: "active" },
{ label: "All", value: "all" },
{ label: "Archived", value: "archived" },
{
label: "All",
value: "all",
disabled: showActiveOnly,
},
{
label: "Archived",
value: "archived",
disabled: showActiveOnly,
},
]}
/>
</Box>
Expand Down
33 changes: 30 additions & 3 deletions lute/api/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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("/<int:bookid>", methods=["GET"])
Expand Down Expand Up @@ -369,13 +376,33 @@ 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)
book.archived = False

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()
Expand All @@ -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("/<int:bookid>", methods=["DELETE"])
Expand Down

0 comments on commit 82c7f32

Please sign in to comment.