Skip to content

Commit

Permalink
Merge pull request #6066 from guardian/mxdvl/card-refactor
Browse files Browse the repository at this point in the history
refactor(Card): extract image
  • Loading branch information
mxdvl authored Oct 3, 2022
2 parents bf348c7 + 6f7655d commit 07066a8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
83 changes: 44 additions & 39 deletions dotcom-rendering/src/web/components/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ const CommentFooter = ({
);
};

const getImage = ({
imageUrl,
avatarUrl,
}: {
imageUrl?: string;
avatarUrl?: string;
}): { type: CardImageType; src: string } | undefined => {
if (avatarUrl) return { type: 'avatar', src: avatarUrl };
if (imageUrl) return { type: 'mainMedia', src: imageUrl };
return undefined;
};

export const Card = ({
linkTo,
format,
Expand Down Expand Up @@ -280,13 +292,10 @@ export const Card = ({
return <Snap snapData={snapData} />;
}

// Decide what type of image to show, main media, avatar or none
let imageType: CardImageType | undefined;
if (imageUrl && avatarUrl) {
imageType = 'avatar';
} else if (imageUrl) {
imageType = 'mainMedia';
}
const image = getImage({
imageUrl,
avatarUrl,
});

return (
<CardWrapper
Expand All @@ -301,41 +310,37 @@ export const Card = ({
dataLinkName={dataLinkName}
/>
<CardLayout
imagePosition={imageUrl !== undefined ? imagePosition : 'top'}
imagePositionOnMobile={
imageUrl !== undefined ? imagePositionOnMobile : 'top'
}
imagePosition={imagePosition}
imagePositionOnMobile={imagePositionOnMobile}
minWidthInPixels={minWidthInPixels}
imageType={imageType}
imageType={image?.type}
>
<ImageWrapper
imageSize={imageSize}
imageType={imageType}
imagePosition={
imageUrl !== undefined ? imagePosition : 'top'
}
imagePositionOnMobile={
imageUrl !== undefined ? imagePositionOnMobile : 'top'
}
>
{imageType === 'avatar' && !!avatarUrl ? (
<AvatarContainer
imageSize={imageSize}
imagePosition={imagePosition}
>
<Avatar
imageSrc={avatarUrl}
imageAlt={byline ?? ''}
containerPalette={containerPalette}
format={format}
/>
</AvatarContainer>
) : (
<img src={imageUrl} alt="" role="presentation" />
)}
</ImageWrapper>
{image && (
<ImageWrapper
imageSize={imageSize}
imageType={image.type}
imagePosition={imagePosition}
imagePositionOnMobile={imagePositionOnMobile}
>
{image.type === 'avatar' ? (
<AvatarContainer
imageSize={imageSize}
imagePosition={imagePosition}
>
<Avatar
imageSrc={image.src}
imageAlt={byline ?? ''}
containerPalette={containerPalette}
format={format}
/>
</AvatarContainer>
) : (
<img src={image.src} alt="" role="presentation" />
)}
</ImageWrapper>
)}
<ContentWrapper
imageType={imageType}
imageType={image?.type}
imageSize={imageSize}
imagePosition={imagePosition}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Props = {
minWidthInPixels?: number;
};

const decideDirection = (imagePosition?: ImagePositionType) => {
const decideDirection = (imagePosition: ImagePositionType) => {
switch (imagePosition) {
case 'top':
return 'column';
Expand All @@ -20,7 +20,7 @@ const decideDirection = (imagePosition?: ImagePositionType) => {
case 'right':
return 'row-reverse';
// If there's no image (so no imagePosition) default to top down
default:
case 'none':
return 'column';
}
};
Expand Down

0 comments on commit 07066a8

Please sign in to comment.