Skip to content

Commit

Permalink
Refactor useGetHoldings to use `useGetExternalAgencyidCatalogHoldin…
Browse files Browse the repository at this point in the history
…gsLogisticsV1`

`useGetExternalAgencyidCatalogHoldingsLogisticsV1` is our new hook to fetch data from `/external/agencyid/catalog/holdingsLogistics/v1`.

My initial plan was to have a setting in Drupal where the library could enable the use of this API instead of `/external/agencyid/catalog/holdings/v3`. However, it is now possible to utilize a hook within an `if` statement. Therefore, I now call both APIs and, if the required data comes from `useGetExternalAgencyidCatalogHoldingsLogisticsV1`, I simply switch the data (`section`, `location`, `sublocation`, `department`) from that API into the existing data.

Squash commit: should be the same
  • Loading branch information
kasperbirch1 committed Sep 20, 2024
1 parent ce9fb42 commit 7dcf07f
Showing 1 changed file with 81 additions and 4 deletions.
85 changes: 81 additions & 4 deletions src/apps/material/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import {
} from "../../core/dbc-gateway/generated/graphql";
import {
getAvailabilityV3,
getExternalAgencyidCatalogHoldingsLogisticsV1,
getHoldingsV3,
useGetExternalAgencyidCatalogHoldingsLogisticsV1,
useGetHoldingsV3
} from "../../core/fbs/fbs";
import {
HoldingsForBibliographicalRecordLogisticsV1,
HoldingsForBibliographicalRecordV3,
HoldingsV3
} from "../../core/fbs/model";
Expand Down Expand Up @@ -540,24 +543,98 @@ export const getAvailability = async ({
}) =>
getAvailabilityV3(getBlacklistedQueryArgs(faustIds, config, "availability"));

type TransformHoldingsDataType = {
logisticsV1Data: HoldingsForBibliographicalRecordLogisticsV1[];
getHoldingsV3Data: HoldingsForBibliographicalRecordV3[];
};

const transformHoldingsData = ({
logisticsV1Data,
getHoldingsV3Data
}: TransformHoldingsDataType) => {
const hasLogisticsPlacement = logisticsV1Data.some(({ holdings }) =>
holdings.some(({ logisticsPlacement }) => logisticsPlacement?.length)
);

const hasLmsPlacement = logisticsV1Data.some(({ holdings }) =>
holdings.some(({ lmsPlacement }) => lmsPlacement)
);

if (hasLogisticsPlacement) {
// Do something with logisticsPlacement
}

if (hasLmsPlacement) {
const extractedLmsPlacements = logisticsV1Data.map(({ holdings }) =>
holdings.map(({ lmsPlacement }) => lmsPlacement)
);

const holdingsWithLmsPlacements = getHoldingsV3Data.map(
({ holdings, ...rest }, dataIndex) => ({
...rest,
holdings: holdings.map((holdingItem, holdingIndex) => ({
...holdingItem,
...extractedLmsPlacements[dataIndex][holdingIndex]
}))
})
);

return holdingsWithLmsPlacements;
}

return getHoldingsV3Data;
};

export const useGetHoldings = ({
faustIds,
config,
useAvailabilityBlacklist = false,
options
optionsGetHoldingsV3,
optionsLogisticsV1
}: {
faustIds: FaustId[];
config: UseConfigFunction;
useAvailabilityBlacklist?: boolean;
options?: {
optionsGetHoldingsV3?: {
query?: UseQueryOptions<Awaited<ReturnType<typeof getHoldingsV3>>>;
};
optionsLogisticsV1?: {
query?: UseQueryOptions<
Awaited<ReturnType<typeof getExternalAgencyidCatalogHoldingsLogisticsV1>>
>;
};
}) => {
const blacklistedBranches = useAvailabilityBlacklist ? "both" : "pickup";
const { data, isLoading, isError } = useGetHoldingsV3(

const {
data: logisticsV1Data,
isLoading: logisticsV1IsLoading,
isError: logisticsV1IsError
} = useGetExternalAgencyidCatalogHoldingsLogisticsV1(
getBlacklistedQueryArgs(faustIds, config, blacklistedBranches),
options
optionsLogisticsV1
);

const {
data: getHoldingsV3Data,
isLoading: getHoldingsV3IsLoading,
isError: getHoldingsV3IsError
} = useGetHoldingsV3(
getBlacklistedQueryArgs(faustIds, config, blacklistedBranches),
optionsGetHoldingsV3
);

const isDataReady = logisticsV1Data && getHoldingsV3Data;
const isLoading = logisticsV1IsLoading || getHoldingsV3IsLoading;
const isError = logisticsV1IsError || getHoldingsV3IsError;

const data =
isDataReady &&
transformHoldingsData({
logisticsV1Data,
getHoldingsV3Data
});

return { data, isLoading, isError };
};

Expand Down

0 comments on commit 7dcf07f

Please sign in to comment.