Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port blog page to ts #2676

Merged
merged 13 commits into from
Nov 6, 2024
18 changes: 10 additions & 8 deletions src/app/components/dialog/dialog.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import React from 'react';
import ReactModal from 'react-modal';

type DialogProps = React.PropsWithChildren<{
isOpen?: boolean;
title?: string;
onPutAway?: () => void;
className?: string;
closeOnOutsideClick?: boolean;
}>

export default function Dialog({
isOpen,
title,
onPutAway,
children,
className,
closeOnOutsideClick
}: React.PropsWithChildren<{
isOpen: boolean;
title: string;
onPutAway: () => void;
className?: string;
closeOnOutsideClick?: boolean;
}>): React.ReactNode;
}: DialogProps): React.ReactNode;

export function useDialog(
initallyOpen?: boolean
): [
BoundDialog: ({
children
}: React.PropsWithChildren<object & {aria: ReactModal.Aria}>) => React.ReactNode,
}: DialogProps & {aria?: ReactModal.Aria}) => React.ReactNode,
open: () => void,
close: () => void,
showDialog: boolean
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/explore-by-subject/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type Category = {
id: string;
id: number;
name: string;
subjectIcon?: string;
};
8 changes: 8 additions & 0 deletions src/app/components/form-input/form-input.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function FormInput({label, longLabel, inputProps, suggestions}:
{
label: string;
longLabel?: string;
inputProps: object;
suggestions?: boolean;
}
): React.ReactNode;
6 changes: 5 additions & 1 deletion src/app/components/toggle/toggle-control-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function ToggleControlBar({ Indicator, children }) {
[isOpen]
);
const focusRef = useRefToFocusAfterClose();
const listboxId = `lbid-${Math.floor(Math.random() * 1010101)}`;

React.useEffect(() => {
if (isOpen) {
Expand All @@ -41,8 +42,11 @@ export default function ToggleControlBar({ Indicator, children }) {
onClick={() => toggle()}
onKeyDown={onKeyDown}
ref={focusRef}
role="combobox"
aria-controls={listboxId}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was an accessibility fix needed because the testing library identifies accessible objects and this wasn't one.

aria-haspopup={listboxId}
>
<div>{children}</div>
<div role="listbox" id={listboxId}>{children}</div>
<Indicator isOpen={isOpen} />
</div>
);
Expand Down
5 changes: 4 additions & 1 deletion src/app/helpers/use-document-head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,17 @@ type ArticleSubject = {name: string} | {value: {subject: {name: string}}[]};

type Collection = {name: string} | {value: {collection: {name: string}}[]};

type BookData = {
export type BookData = {
meta?: object;
description?: string;
bookSubjects?: Subject[];
bookCategories?: Category[];
title?: string;
articleSubjects?: ArticleSubject[];
collections?: Collection[];
error?: {
message: string;
}
};

// eslint-disable-next-line complexity
Expand Down
86 changes: 48 additions & 38 deletions src/app/pages/blog/article-summary/article-summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,53 @@ import React from 'react';
import RawHTML from '~/components/jsx-helpers/raw-html';
import Byline from '~/components/byline/byline';
import ClippedImage from '~/components/clipped-image/clipped-image';
import type {
ArticleSummary as BlurbDataBase,
CollectionEntry,
SubjectEntry
} from '~/pages/blog/blog-context';

type Collection = {
name?: string;
value: {collection: Collection}[];
};
type CollectionVariant =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These types were simply wrong. They shouldn't have been recursive, they just have an optional wrapping layer.

| CollectionEntry
| {
value: {collection: CollectionEntry}[];
};

type ArticleSubjectVariant =
| SubjectEntry
| {
value: {subject: SubjectEntry}[];
};

type BlurbData =
| null
| (Omit<BlurbDataBase, 'collections' | 'articleSubjects'> & {
meta?: {slug: string};
collections: CollectionVariant[];
articleSubjects: ArticleSubjectVariant[];
});

type ArticleSubject = {
name?: string;
value: {subject: ArticleSubject}[];
};

type BlurbData = null | {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

BlurbData and ArticleSummaryData were defined independently, but BlurbData is really an extension of ArticleSummaryData.

id: string;
collections: Collection[];
articleSubjects: ArticleSubject[];
heading: string;
export type ArticleSummaryData = {
id?: number;
articleSlug: string;
image: string;
altText?: string;
headline: string;
subheading: string;
articleImage: string;
articleImageAlt: string;
bodyBlurb: string;
author: string;
body: string;
date: string;
slug?: string;
meta: {slug: string};
author: string;
collectionNames: string[];
articleSubjectNames: string[];
openInNewWindow?: boolean;
HeadTag?: keyof JSX.IntrinsicElements;
setPath?: (href: string) => void;
};

export function blurbModel(data: BlurbData) {
export function blurbModel(data: BlurbData): ArticleSummaryData | Record<string, never> {
if (!data) {
return {};
return {} as Record<string, never>;
}

return {
Expand All @@ -56,24 +74,14 @@ export function blurbModel(data: BlurbData) {
body: data.bodyBlurb,
author: data.author,
date: data.date,
articleSlug: data.slug || data.meta.slug
articleSlug: data.slug || (data.meta?.slug as string)
};
}

export type ArticleSummaryData = {
articleSlug: string;
image: string;
headline: string;
subheading: string;
body: string;
date: string;
author: string;
collectionNames: string[];
articleSubjectNames: string[];
setPath: (href: string) => void;
openInNewWindow: boolean;
HeadTag?: keyof JSX.IntrinsicElements;
};
export type PopulatedBlurbModel = Exclude<
ReturnType<typeof blurbModel>,
Record<string, never>
>;

export default function ArticleSummary({
articleSlug,
Expand All @@ -100,8 +108,10 @@ export default function ArticleSummary({
if (target.getAttribute('target') === '_blank') {
return;
}
event.preventDefault();
setPath(target.href);
if (setPath) {
event.preventDefault();
setPath(target.href);
}
},
[setPath]
);
Expand Down
Loading
Loading