Skip to content

Commit

Permalink
[F] Callouts use new DAM plugin
Browse files Browse the repository at this point in the history
Includes:
 - Main Callout Image
 - TwoTone Callout Background Image
 - Glossary Term Image
 - Investigation Hero Images
  • Loading branch information
Blake Mason authored and blnkt committed Nov 22, 2023
1 parent 30a0978 commit d023507
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 96 deletions.
8 changes: 4 additions & 4 deletions components/atomic/Tile/patterns/InvestigationTile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { useUID } from "react-uid";
import { Image } from "@rubin-epo/epo-react-lib";
import * as Styled from "./styles";
import { useTranslation } from "react-i18next";
import { useDamAssetAsImage } from "@/lib/utils";
import { imageShaper } from "@/lib/utils";

const InvestigationTile = ({ investigation, useExternalLink }) => {
const uid = useUID();

const { t } = useTranslation();
const { damAsset, status, title, externalUrl, landingPage } =
const { cantoAssetSingle, status, title, externalUrl, landingPage } =
investigation || {};
const image = useDamAssetAsImage(damAsset?.[0]);
const image = imageShaper("EN", cantoAssetSingle?.[0]);
const url = useExternalLink ? externalUrl : landingPage?.[0]?.uri;
const isInactive = status === "inactive";

Expand Down Expand Up @@ -43,7 +43,7 @@ InvestigationTile.propTypes = {
investigation: PropTypes.shape({
uri: PropTypes.string,
title: PropTypes.string,
damAsset: PropTypes.array,
cantoAssetSingle: PropTypes.array,
externalUrl: PropTypes.string,
status: PropTypes.string,
landingPage: PropTypes.arrayOf(
Expand Down
9 changes: 4 additions & 5 deletions components/content-blocks/Callout/CalloutMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

import PropTypes from "prop-types";
import classNames from "classnames";
import imageShape from "@/shapes/image";
import { linksShape } from "@/shapes/link";
import Wrapper from "./Wrapper";
import { Header, Text, StyledMixedLink, StyledImage } from "./styles";
import TempList from "@/components/dynamic/TempList";
import { useDamAssetAsImage } from "@/lib/utils";
import { imageShaper } from "@/lib/utils";

export default function CalloutMain({ callout }) {
const {
dynamicComponent,
header,
damAsset,
cantoAssetSingle,
links,
padImage,
text,
...wrapperProps
} = callout;

const calloutImage = useDamAssetAsImage(damAsset[0]);
const calloutImage = imageShaper("EN", cantoAssetSingle[0]);

return (
<Wrapper
Expand Down Expand Up @@ -65,7 +64,7 @@ CalloutMain.propTypes = {
callout: PropTypes.shape({
header: PropTypes.string,
text: PropTypes.node,
damAsset: PropTypes.arrayOf(imageShape).isRequired,
cantoAssetSingle: PropTypes.array,
padImage: PropTypes.bool,
links: linksShape,
backgroundColor: PropTypes.string,
Expand Down
8 changes: 4 additions & 4 deletions components/content-blocks/Callout/CalloutTwoTone.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
StyledTwoToneImage,
} from "./styles";
import { ResponsiveImage } from "@rubin-epo/epo-react-lib";
import { useDamAssetAsImage } from "@/lib/utils";
import { imageShaper } from "@/lib/utils";

export default function CalloutTwoTone({ callout }) {
const {
Expand All @@ -26,7 +26,7 @@ export default function CalloutTwoTone({ callout }) {
text,
} = callout;

const calloutBackgroundImage = useDamAssetAsImage(backgroundImage[0]);
const calloutBackgroundImage = imageShaper("EN", backgroundImage[0]);

return (
<>
Expand Down Expand Up @@ -73,10 +73,10 @@ export default function CalloutTwoTone({ callout }) {
CalloutTwoTone.propTypes = {
callout: PropTypes.shape({
backgroundColor: PropTypes.string,
backgroundImage: PropTypes.arrayOf(imageShape).isRequired,
backgroundImage: PropTypes.array,
captionRichText: PropTypes.node,
header: PropTypes.string,
image: PropTypes.arrayOf(imageShape).isRequired,
image: PropTypes.arrayOf(imageShape),
links: linksShape,
text: PropTypes.node,
}).isRequired,
Expand Down
7 changes: 4 additions & 3 deletions components/layout/InvestigationHero/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import PropTypes from "prop-types";
import * as Styled from "./styles";
import { Button, Image, IconComposer } from "@rubin-epo/epo-react-lib";
import T from "@/page/Translate";
import { useDamAssetAsImage } from "@/lib/utils";
import { imageShaper } from "@/lib/utils";

export default function InvestigationHero({ investigation }) {
const { damAsset, title, externalUrl, duration, status } =
const { cantoAssetSingle, title, externalUrl, duration, status } =
investigation || {};
const image = useDamAssetAsImage(damAsset?.[0]);
const image = imageShaper("EN", cantoAssetSingle?.[0]);

if (!investigation) return null;

Expand Down Expand Up @@ -52,5 +52,6 @@ InvestigationHero.propTypes = {
title: PropTypes.string,
})
),
cantoAssetSingle: PropTypes.array,
}),
};
8 changes: 4 additions & 4 deletions components/templates/GlossaryPage/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PropTypes from "prop-types";
import Body from "@/global/Body";
import Breadcrumbs from "@/page/Breadcrumbs";
import { useCustomBreadcrumbs, useDamAssetAsImage } from "@/lib/utils";
import { useCustomBreadcrumbs, imageShaper } from "@/lib/utils";
import {
Container,
ResponsiveImage,
Expand All @@ -13,15 +13,15 @@ import { useTranslation } from "react-i18next";
export default function GlossaryPage({ data }) {
const { t } = useTranslation();

const { id, title, description, uri, text, damAsset, caption } = data;
const { id, title, description, uri, text, cantoAssetSingle, caption } = data;

const customBreadcrumbs = useCustomBreadcrumbs("Glossary");

const glossaryCrumb = customBreadcrumbs.find((b) =>
b.uri.includes("glossary")
);

const image = useDamAssetAsImage(damAsset);
const image = imageShaper("EN", cantoAssetSingle[0]);

const bodyProps = {
description,
Expand Down Expand Up @@ -68,7 +68,7 @@ GlossaryPage.propTypes = {
description: PropTypes.string,
uri: PropTypes.string,
text: PropTypes.node,
damAsset: PropTypes.array,
cantoAssetSingle: PropTypes.array,
caption: PropTypes.string,
}),
};
38 changes: 5 additions & 33 deletions lib/api/fragments/content-blocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getImageFields } from "@/api/fragments/image";
import { getImageFields, cantoSingleAsset } from "@/api/fragments/image";
import { getLinkFields } from "@/api/fragments/link";

/// ///////////////////////////////////////////
Expand Down Expand Up @@ -31,14 +31,7 @@ export const calloutBlockFragment = `
backgroundColor
dynamicComponent
header
damAsset {
height
width
damMetadata {
metadataKey
metadataValue
}
}
${cantoSingleAsset}
links {
...links
}
Expand All @@ -51,14 +44,7 @@ export const calloutBlockFragment = `
}
...on callouts_calloutTwoTone_Entry {
backgroundColor
backgroundImage: damAsset {
height
width
damMetadata {
metadataKey
metadataValue
}
}
backgroundImage: ${cantoSingleAsset}
captionRichText
header
image: contentImage {
Expand Down Expand Up @@ -277,14 +263,7 @@ export const investigationGridBlockFragment = `
title
externalUrl: externalUrlTranslatable
status: investigationStatus
damAsset {
height
width
damMetadata {
metadataKey
metadataValue
}
}
${cantoSingleAsset}
landingPage {
uri
title
Expand Down Expand Up @@ -381,14 +360,7 @@ export const relatedContentFragment = `
uri
status: investigationStatus
description: investigationDescription
damAsset {
height
width
damMetadata {
metadataKey
metadataValue
}
}
${cantoSingleAsset}
landingPage {
uri
}
Expand Down
10 changes: 2 additions & 8 deletions lib/api/fragments/glossary-term.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fullBaseFields } from "@/lib/api/fragments/shared";
import { cantoSingleAsset } from "@/api/fragments/image";

export const glossaryTermFragment = `
fragment glossaryTermFragment on glossaryTerms_glossaryTerm_Entry {
Expand All @@ -10,14 +11,7 @@ export const glossaryTermFragmentFull = `
fragment glossaryTermFragmentFull on glossaryTerms_glossaryTerm_Entry {
...on glossaryTerms_glossaryTerm_Entry {
text
damAsset {
height
width
damMetadata {
metadataKey
metadataValue
}
}
${cantoSingleAsset}
caption
}
ancestors {
Expand Down
21 changes: 21 additions & 0 deletions lib/api/fragments/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@ export function getImageFields(mode = "fit", width = 900, height) {
url ${transform(mode, width, height)}
`;
}

export const cantoSingleAsset = gql`
cantoAssetSingle {
url {
directUrlPreview
directUrlOriginal
PNG
HighJPG
LowJPG
preview
}
width
height
metadata: additional {
AltTextEN
AltTextES
CaptionEN
CaptionES
Credit
}
}`;
12 changes: 3 additions & 9 deletions lib/api/fragments/investigation.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { cantoSingleAsset } from "@/api/fragments/image";

export const investigationFragment = `
fragment investigationFragment on investigations_investigation_Entry {
id
title
uri
...on investigations_investigation_Entry {
date: dateUpdated
# description
damAsset {
height
width
damMetadata {
metadataKey
metadataValue
}
}
${cantoSingleAsset}
}
}
`;
10 changes: 2 additions & 8 deletions lib/api/investigation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { gql } from "graphql-request";
import { queryAPI } from "@/lib/fetch";
import { cantoSingleAsset } from "@/api/fragments/image";

export async function addRelatedInvestigation(entryData, site) {
if (!entryData || !entryData.id) return null;
Expand Down Expand Up @@ -36,14 +37,7 @@ export async function getRelatedInvestigation(entryData, site = "default") {
title
}
}
damAsset {
height
width
damMetadata {
metadataKey
metadataValue
}
}
${cantoSingleAsset}
}
}
}
Expand Down
43 changes: 25 additions & 18 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,29 +408,36 @@ export function getLocaleString(siteString) {
}

// DAM METADATA
export const useDamAssetAsImage = (damAsset) => {
if (!damAsset || damAsset.length === 0) {
return null;
}
export const imageShaper = (site, data, className) => {
if (!data) return undefined;

const localeKeys = {
default: "EN",
es: "ES",
};

const { height, width, damMetadata: rawMetadata } = damAsset;
const key = localeKeys[site];

const damMetadata = rawMetadata.reduce((previous, current) => {
const { metadataKey, metadataValue } = current;
previous[metadataKey] = metadataValue;
const { metadata, url, width, height } = data;
const { directUrlPreview = "", directUrlOriginal = "", preview = "" } = url;

return previous;
}, {});
const urlWithoutConstraint = directUrlPreview.slice(0, -3);
const constraint = Math.max(width, height);

const { directImageUrl: url, altText, thumbnailUrl: thumb } = damMetadata;
const altText = metadata[`AltText${key}`];
const caption = metadata[`Caption${key}`];
const credit = metadata.Credit;

return {
url,
url2x: url,
url3x: url,
height,
width,
altText,
thumb,
url: `${urlWithoutConstraint}${Math.floor(constraint / 3)}`,
url2x: `${urlWithoutConstraint}${Math.floor(constraint / 2)}`,
url3x: directUrlOriginal,
thumb: preview,
width: Number(width),
height: Number(height),
className,
altText: altText === null ? undefined : altText,
caption: caption === null ? undefined : caption,
credit: credit === null ? undefined : credit,
};
};

0 comments on commit d023507

Please sign in to comment.