Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/app/helpers/use-optimized-image.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const distributionUrl = 'https://images.openstax.org';
const version = 'v1';

export function maxDimIfNarrowerThan(width: number) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was something duplicated in several modules, so I made it a utility here.

return window.innerWidth < width ? width : undefined;
}

export default function useOptimizedImage(src: string, maxDim?: number) {
if (!src) {
return src;
Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/about/about.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import LoaderPage from '~/components/jsx-helpers/loader-page';
import LazyLoad from 'react-lazyload';
import RawHTML from '~/components/jsx-helpers/raw-html';
import useOptimizedImage from '~/helpers/use-optimized-image';
import useOptimizedImage, {maxDimIfNarrowerThan} from '~/helpers/use-optimized-image';
import './about.scss';

const slug = 'pages/about';
Expand Down Expand Up @@ -86,7 +86,7 @@ function About({
const mapAlt =
whereMapAlt ||
'animated map suggesting where our books are being adopted';
const maxDim = window.innerWidth < 1920 ? 1015 : undefined;
const maxDim = maxDimIfNarrowerThan(1920);
const optimizedWhoImage = useOptimizedImage(whoImageUrl, maxDim);

return (
Expand Down

This file was deleted.

119 changes: 119 additions & 0 deletions src/app/pages/institutional-partnership/institutional-partnership.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, even the initial conversion came out as delete-and-recreate. :(

Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import React from 'react';
import LoaderPage from '~/components/jsx-helpers/loader-page';
import LazyLoad from 'react-lazyload';
import {camelCaseKeys} from '~/helpers/page-data-utils';
import Banner, {BannerProps} from './sections/banner/banner';
import OverlappingQuote, {
OverlappingQuoteProps
} from './sections/overlapping-quote/overlapping-quote';
import About, {AboutProps} from './sections/about/about';
import Promoting, {PromotingProps} from './sections/promoting/promoting';
import BigQuote, {BigQuoteProps} from './sections/big-quote/big-quote';
import Speaking, {SpeakingProps} from './sections/speaking/speaking';
import Results, {ResultsProps} from './sections/results/results';
import Participants, {
ParticipantsProps
} from './sections/participants/participants';
import SmallQuote, {SmallQuoteProps} from './sections/small-quote/small-quote';
import SignUp, {SignUpProps} from './sections/sign-up/sign-up';
import './institutional-partnership.scss';

type DataObject = Record<string, unknown>;

type ImageMeta = {
download_url: string;
};

type SectionData = {
section_4_background_image: {
meta: ImageMeta;
};
} & DataObject;

function unprefixKey(
newObject: DataObject,
oldKey: string,
prefix: string,
data: DataObject
) {
const newKey = oldKey.replace(prefix, '');

newObject[newKey] = data[oldKey];
return newObject;
}

function sectionData(data: DataObject, sectionNumber: string | number) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parses out the data for each section, so its output is simply cast to the appropriate props type. We're trusting the CMS to serve the expected format.

const sectionPrefix = `section_${sectionNumber}_`;

return camelCaseKeys(
Reflect.ownKeys(data)
.filter((k) => String(k).startsWith(sectionPrefix))
.reduce(
(a, oldKey) =>
unprefixKey(a, String(oldKey), sectionPrefix, data),
{}
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have other CMS data that needs this transformation? I can see this being a reusable helperDo we have other CMS data that needs this transformation? I can see this being a reusable helper

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhat to my surprise, I do not find other pages doing this transformation.

);
}

function quoteData(data: DataObject) {
return Reflect.ownKeys(data)
.filter((k) => String(k).startsWith('quote'))
.reduce(
(a, oldKey) => unprefixKey(a, String(oldKey), 'quote_', data),
{}
);
}

function InstitutionalPartnership({data}: {data: SectionData}) {
return (
<React.Fragment>
<Banner {...(sectionData(data, 1) as BannerProps)} />
<OverlappingQuote {...(quoteData(data) as OverlappingQuoteProps)} />
<About {...(sectionData(data, 2) as AboutProps)} />
<LazyLoad>
<Promoting {...(sectionData(data, 3) as PromotingProps)} />
<BigQuote
{...{
backgroundImage:
data.section_4_background_image.meta.download_url,
...(sectionData(data, '4_quote') as Omit<
BigQuoteProps,
'backgroundImage'
>)
}}
/>
</LazyLoad>
<LazyLoad>
<Speaking {...(sectionData(data, 5) as SpeakingProps)} />
<Results {...(sectionData(data, 6) as ResultsProps)} />
</LazyLoad>
<LazyLoad>
<Participants
{...(sectionData(data, 7) as ParticipantsProps)}
/>
<SmallQuote
{...(sectionData(data, '8_quote') as SmallQuoteProps)}
/>
</LazyLoad>
<LazyLoad>
<SignUp {...(sectionData(data, 9) as SignUpProps)} />
</LazyLoad>
</React.Fragment>
);
}

const slug = 'pages/institutional-partners';

export default function PageLoader() {
return (
<main className="institutional-partnership page">
<LoaderPage
slug={slug}
Child={InstitutionalPartnership}
doDocumentSetup
noCamelCase
/>
</main>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@ import RawHTML from '~/components/jsx-helpers/raw-html';
import useOptimizedImage from '~/helpers/use-optimized-image';
import './about.scss';

export type AboutProps = {
heading: string;
description: string;
altText: string;
image: {
meta: {
downloadUrl: string;
};
};
};

export default function About({
heading, description, altText,
image: {meta: {downloadUrl: image}}
}) {
heading,
description,
altText,
image: {
meta: {downloadUrl: image}
}
}: AboutProps) {
const optimizedImage = useOptimizedImage(image);

return (
Expand Down
23 changes: 0 additions & 23 deletions src/app/pages/institutional-partnership/sections/banner/banner.js

This file was deleted.

47 changes: 47 additions & 0 deletions src/app/pages/institutional-partnership/sections/banner/banner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react';
import RawHTML from '~/components/jsx-helpers/raw-html';
import useOptimizedImage, {
maxDimIfNarrowerThan
} from '~/helpers/use-optimized-image';
import './banner.scss';

export type BannerProps = {
heading: string;
description: string;
linkText: string;
link: string;
backgroundImage: {
meta: {
downloadUrl: string;
};
};
};

export default function Banner({
heading,
description,
linkText,
link,
backgroundImage: {
meta: {downloadUrl: backgroundImage}
}
}: BannerProps) {
const maxDim = maxDimIfNarrowerThan(1920);
const optimizedImage = useOptimizedImage(backgroundImage, maxDim);

return (
<section className="banner">
<div
className="background-image"
style={{backgroundImage: `url(${optimizedImage})`}}
/>
<div className="content-block">
<h1>{heading}</h1>
<RawHTML html={description} />
<a className="btn primary" href={link}>
{linkText}
</a>
</div>
</section>
);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import React from 'react';
import useOptimizedImage from '~/helpers/use-optimized-image';
import useOptimizedImage, {
maxDimIfNarrowerThan
} from '~/helpers/use-optimized-image';
import './big-quote.scss';

export default function BigQuote({text, name, title, school, backgroundImage}) {
const maxDim = window.innerWidth < 1920 ? 1920 : null;
export type BigQuoteProps = {
text: string;
name: string;
title: string;
school: string;
backgroundImage: string;
};

export default function BigQuote({
text,
name,
title,
school,
backgroundImage
}: BigQuoteProps) {
const maxDim = maxDimIfNarrowerThan(1920);
const optimizedImage = useOptimizedImage(backgroundImage, maxDim);

return (
<section className="big-quote">
<div className="background-image" style={{backgroundImage: `url(${optimizedImage})`}} />
<div
className="background-image"
style={{backgroundImage: `url(${optimizedImage})`}}
/>
<div className="gradient-overlay" />
<div className="content">
<div className="big-quote-mark"></div>
<div className="big-quote-mark">&quot;</div>
<div className="text-block">
<div className="quote">{text}</div>
<div className="name">- {name}</div>
Expand Down
Loading