Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCC-4193 - Optimize state in bib page #341

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Updated

- Bib page state optimizations (SCC-4193)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this once the merge conflicts have been fixed.

- Bump nvmrc version to Node 20 and DS package to release candidate 3.4.0-rc.
- Update error message in Item Filter Year to include new error message prefix from the DS.
- Integrate view_all query param on client side and remove batched fetch (SCC-4287)
Expand Down
24 changes: 15 additions & 9 deletions pages/bib/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,19 @@ export default function BibPage({
const { push, query } = useRouter()
const metadataTitle = `Item Details | ${SITE_NAME}`

const [bib, setBib] = useState(new Bib(discoveryBibResult))
const bib = new Bib(discoveryBibResult)
dgcohen marked this conversation as resolved.
Show resolved Hide resolved
const [itemTableData, setItemTableTata] = useState(bib.itemTableData)
const [itemsLoading, setItemsLoading] = useState(false)
const [itemFetchError, setItemFetchError] = useState(false)

const [viewAllExpanded, setViewAllExpanded] = useState(viewAllItems)
const [appliedFilters, setAppliedFilters] = useState(
parseItemFilterQueryParams(query)
)
const filtersAreApplied = areFiltersApplied(appliedFilters)

const [itemTablePage, setItemTablePage] = useState(itemPage)
const [numItems, setNumItems] = useState(bib.numItems(filtersAreApplied))

const itemTableHeadingRef = useRef<HTMLDivElement>(null)
const viewAllLoadingTextRef = useRef<HTMLDivElement & HTMLLabelElement>(null)
Expand All @@ -87,8 +91,6 @@ export default function BibPage({

const displayLegacyCatalogLink = isNyplBibID(bib.id)

const filtersAreApplied = areFiltersApplied(appliedFilters)

const refreshItemTable = async (
newQuery: BibQueryParams,
viewAllItems = false,
Expand Down Expand Up @@ -140,7 +142,11 @@ export default function BibPage({
)
if (response?.ok) {
const { discoveryBibResult } = await response.json()
setBib(new Bib(discoveryBibResult))
const refreshedBib = new Bib(discoveryBibResult)

// Set values that need to be updated in state for the refreshed Bib
setNumItems(refreshedBib.numItems(filtersAreApplied))
setItemTableTata(refreshedBib.itemTableData)

setItemsLoading(false)

Expand Down Expand Up @@ -268,17 +274,17 @@ export default function BibPage({
>
{buildItemTableDisplayingString(
itemTablePage,
bib.numItems(filtersAreApplied),
numItems,
viewAllExpanded,
filtersAreApplied
)}
</Heading>
{bib.itemTableData ? (
<ItemTable itemTableData={bib.itemTableData} />
{itemTableData.hasItems ? (
<ItemTable itemTableData={itemTableData} />
) : null}
</>
)}
{bib.itemTableData ? (
{itemTableData.hasItems ? (
<ItemTableControls
bib={bib}
viewAllExpanded={viewAllExpanded}
Expand All @@ -287,7 +293,7 @@ export default function BibPage({
handlePageChange={handlePageChange}
handleViewAllClick={handleViewAllClick}
viewAllLoadingTextRef={viewAllLoadingTextRef}
numItemsTotal={bib.numItems(filtersAreApplied)}
numItemsTotal={numItems}
filtersAreApplied={filtersAreApplied}
/>
) : null}
Expand Down
5 changes: 3 additions & 2 deletions src/components/ItemTable/ItemTableControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ const ItemTableControls = ({
numItemsTotal = 0,
filtersAreApplied = false,
}: ItemTableControlsProps) => {
const viewAllLoadingMessage =
bib.getItemsViewAllLoadingMessage(filtersAreApplied)
const viewAllLoadingMessage = `Loading all ${
filtersAreApplied ? "matching" : numItemsTotal
} items. This may take a few moments...`
Comment on lines -41 to +43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getItemsViewAllLoadingMessage is only used here, why not change it instead of moving the functionality here? Or at least delete the declaration from the Bib model.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, i removed it in the Bib model since it's only used here, but can add it back if you'd like greater separation of logic from presentation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either approach is ok but I brought it up just as a question.


return (
<Box display={{ md: "flex" }} my="xl" justifyContent="space-between">
Expand Down
4 changes: 4 additions & 0 deletions src/models/ItemTableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export default class ItemTableData {
})
}

hasItems(): boolean {
return this.items?.length > 0
}

showVolumeColumn(): boolean {
return this.items?.some((item) => item.volume) && !this.inSearchResult
}
Expand Down