-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
useGetWorkUrlFromIdentifier
to support material linking from loans
Since all data from loans comes from Publizon, we don’t have a `workId`, which is required to link to the material. Therefore, I use the `useComplexSearchWithPaginationQuery` to find the work using the ISBN (identifier) and construct the `workId` into a URL, e.g., `/work/work-of:870970-basis:62986115`. Additionally, if it finds the material type, it adds that as a parameter.
- Loading branch information
1 parent
016b064
commit a23e109
Showing
3 changed files
with
74 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useMemo } from "react"; | ||
import { useComplexSearchWithPaginationQuery } from "../dbc-gateway/generated/graphql"; | ||
import { useUrls } from "./url"; | ||
import { constructMaterialUrl } from "./helpers/url"; | ||
import { WorkId } from "./types/ids"; | ||
import { getMaterialTypes } from "./helpers/general"; | ||
import { Manifestation } from "./types/entities"; | ||
|
||
type UseGetWorkUrlFromIdentifierResult = { | ||
workUrl: URL | null; | ||
isLoading: boolean; | ||
isError: boolean; | ||
}; | ||
|
||
const useGetWorkUrlFromIdentifier = ( | ||
identifier: string | undefined | null | ||
): UseGetWorkUrlFromIdentifierResult => { | ||
const u = useUrls(); | ||
const materialUrl = u("materialUrl"); | ||
|
||
const { data, isLoading, isError } = useComplexSearchWithPaginationQuery( | ||
{ | ||
cql: `term.isbn=${identifier}`, | ||
offset: 0, | ||
limit: 1, | ||
filters: {} | ||
}, | ||
{ | ||
enabled: Boolean(identifier) | ||
} | ||
); | ||
|
||
const workUrl = useMemo(() => { | ||
if (!data || !identifier) return null; | ||
|
||
const work = data.complexSearch?.works?.[0]; | ||
if (!work) return null; | ||
|
||
const workId = work.workId as WorkId; | ||
const manifestationWithSameIdentifier = work.manifestations?.all?.find( | ||
(manifestation) => | ||
manifestation.identifiers?.some((id) => id.value === identifier) | ||
); | ||
const materialType = manifestationWithSameIdentifier | ||
? getMaterialTypes([manifestationWithSameIdentifier] as Manifestation[]) | ||
: undefined; | ||
|
||
return constructMaterialUrl( | ||
materialUrl, | ||
workId, | ||
materialType ? String(materialType) : undefined | ||
); | ||
}, [data, identifier, materialUrl]); | ||
|
||
return { workUrl, isLoading, isError }; | ||
}; | ||
|
||
export default useGetWorkUrlFromIdentifier; |