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
8 changes: 7 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Prerelease

### Updated

- Bib page state optimizations (SCC-4193)

## [1.3.2] 2024-10-7

### Fixed
Expand Down Expand Up @@ -35,8 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Updated

- Bump DS version to 3.4.0
- Bump nvmrc version to Node 20.
- Bump DS package to 3.4.0.
- 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)
- Replaced travis with github actions (SCC-4218)
Expand Down
36 changes: 24 additions & 12 deletions pages/bib/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,24 @@ export default function BibPage({
const { push, query } = useRouter()
const metadataTitle = `Item Details | ${SITE_NAME}`

const [bib, setBib] = useState(new Bib(discoveryBibResult))
const [itemsLoading, setItemsLoading] = useState(false)
const [itemFetchError, setItemFetchError] = useState(false)
// We do not need to preserve the whole bib in state since the bib details are constant
// and do not need to be updated in memory every time the item table data is refetched
const bib = new Bib(discoveryBibResult)
dgcohen marked this conversation as resolved.
Show resolved Hide resolved

const [viewAllExpanded, setViewAllExpanded] = useState(viewAllItems)
// Item table data / filters state
const [itemTableData, setItemTableTata] = useState(bib.itemTableData)
const [appliedFilters, setAppliedFilters] = useState(
parseItemFilterQueryParams(query)
)
const filtersAreApplied = areFiltersApplied(appliedFilters)
const [numItems, setNumItems] = useState(bib.numItems(filtersAreApplied))

// Item table status state
const [itemsLoading, setItemsLoading] = useState(false)
const [itemFetchError, setItemFetchError] = useState(false)

// Pagination state
const [viewAllExpanded, setViewAllExpanded] = useState(viewAllItems)
const [itemTablePage, setItemTablePage] = useState(itemPage)

const itemTableHeadingRef = useRef<HTMLDivElement>(null)
Expand All @@ -87,8 +97,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 +148,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 +280,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 +299,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
7 changes: 0 additions & 7 deletions src/models/Bib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,6 @@ export default class Bib {
}${this.resourceType.toLowerCase()}${totalItems !== 1 ? "s" : ""}`
}

getItemsViewAllLoadingMessage(filtersAreApplied = false) {
// We don't want to show the number of filtered items since this may change during loading.
return `Loading all ${
filtersAreApplied ? "matching" : this.numPhysicalItems
} items. This may take a few moments...`
}

getTitleDisplayFromResult(result: DiscoveryBibResult) {
if (!result.titleDisplay || !result.titleDisplay.length) {
const author =
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