Skip to content

Commit

Permalink
Move getUrl functionality to separate helper file
Browse files Browse the repository at this point in the history
To keep the component a bit more tidy
  • Loading branch information
spaceo committed Apr 16, 2024
1 parent 4bb102d commit c822181
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 48 deletions.
50 changes: 2 additions & 48 deletions src/components/cover/cover.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import React, { useCallback, useState } from "react";
import clsx from "clsx";
import { first } from "lodash";
import { useGetCoverCollection } from "../../core/cover-service-api/cover-service";
import {
Cover as CoverType,
CoverImageUrls,
GetCoverCollectionType
} from "../../core/cover-service-api/model";
import { GetCoverCollectionType } from "../../core/cover-service-api/model";
import { Pid } from "../../core/utils/types/ids";
import LinkNoStyle from "../atoms/links/LinkNoStyle";
import CoverImage from "./cover-image";
import { Manifestation } from "../../core/utils/types/entities";

type CoverServiceSizes = keyof CoverImageUrls;
import { getCoverUrl } from "./helper";

export type CoverProps = {
animate: boolean;
Expand All @@ -27,46 +21,6 @@ export type CoverProps = {
linkAriaLabelledBy?: string;
};

const getUrl = (cover: CoverType, size: CoverServiceSizes) =>
cover.imageUrls?.[size]?.url;

const getCoverUrl = ({
coverData,
bestRepresentation,
size
}: {
coverData: CoverType[] | null | undefined;
bestRepresentation: CoverProps["bestRepresentation"];
size: CoverServiceSizes;
}) => {
if (!coverData) {
return null;
}

const firstCover = first(coverData);

if (!bestRepresentation && firstCover && getUrl(firstCover, size)) {
return getUrl(firstCover, size);
}

const bestRepresentationCover = first(
coverData.filter(
(item: CoverType) =>
bestRepresentation && item.id === bestRepresentation.pid
)
);

if (bestRepresentationCover && getUrl(bestRepresentationCover, size)) {
return getUrl(bestRepresentationCover, size);
}

if (firstCover && getUrl(firstCover, size)) {
return getUrl(firstCover, size);
}

return null;
};

export const Cover = ({
url,
alt,
Expand Down
50 changes: 50 additions & 0 deletions src/components/cover/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { first } from "lodash";
import {
CoverImageUrls,
Cover as CoverType
} from "../../core/cover-service-api/model";
import { CoverProps } from "./cover";

type CoverServiceSizes = keyof CoverImageUrls;

const getUrl = (cover: CoverType, size: CoverServiceSizes) =>
cover.imageUrls?.[size]?.url;

export const getCoverUrl = ({
coverData,
bestRepresentation,
size
}: {
coverData: CoverType[] | null | undefined;
bestRepresentation: CoverProps["bestRepresentation"];
size: CoverServiceSizes;
}) => {
if (!coverData) {
return null;
}

const firstCover = first(coverData);

if (!bestRepresentation && firstCover && getUrl(firstCover, size)) {
return getUrl(firstCover, size);
}

const bestRepresentationCover = first(
coverData.filter(
(item: CoverType) =>
bestRepresentation && item.id === bestRepresentation.pid
)
);

if (bestRepresentationCover && getUrl(bestRepresentationCover, size)) {
return getUrl(bestRepresentationCover, size);
}

if (firstCover && getUrl(firstCover, size)) {
return getUrl(firstCover, size);
}

return null;
};

export default {};

0 comments on commit c822181

Please sign in to comment.