-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
133 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/components/paragraphs/sup-pre-built/filtering-author-list.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"use client"; | ||
|
||
import {twMerge} from "tailwind-merge"; | ||
import {HTMLAttributes, JSX, useEffect, useMemo, useState} from "react"; | ||
import PagedList from "@components/elements/paged-list"; | ||
import {useRouter, useSearchParams} from "next/navigation"; | ||
|
||
type Props = HTMLAttributes<HTMLDivElement> & { | ||
authors: Map<string, JSX.Element[]> | ||
} | ||
const FilteringAuthorList = ({authors, ...props}: Props) => { | ||
const searchParams = useSearchParams(); | ||
const router = useRouter(); | ||
const [alphaChosen, setAlphaChosen] = useState<string>(searchParams.get("author") || "A") | ||
|
||
const displayedAuthors = useMemo(() => { | ||
const displayedAuthorMap = new Map<string, JSX.Element[]>(); | ||
[...authors.keys()].map(authorName => { | ||
if (authorName.charAt(0).toUpperCase() === alphaChosen) displayedAuthorMap.set(authorName, authors.get(authorName) as JSX.Element[]); | ||
}); | ||
return displayedAuthorMap; | ||
}, [authors, alphaChosen]); | ||
|
||
const alphaChoices = useMemo(() => { | ||
let choices: string[] = []; | ||
[...authors.keys()].map(authorName => { | ||
choices.push(authorName.charAt(0).toUpperCase()); | ||
}) | ||
choices = [...new Set(choices)].sort((a, b) => a.localeCompare(b)); | ||
return choices; | ||
}, [authors]); | ||
|
||
useEffect(() => { | ||
// Use search params to retain any other parameters. | ||
const params = new URLSearchParams(searchParams.toString()); | ||
|
||
if (alphaChosen !== "A") { | ||
params.set("author", alphaChosen) | ||
} else { | ||
params.delete("author") | ||
} | ||
router.replace(`?${params.toString()}`, {scroll: false}) | ||
}, [router, searchParams, alphaChosen]); | ||
|
||
return ( | ||
<div {...props} className={twMerge("flex justify-between", props?.className)}> | ||
<div className="sr-only" aria-live="polite">Showing authors that start with {alphaChosen}</div> | ||
<a href="#author-filter" className="skiplink">Skip to filter</a> | ||
|
||
<PagedList itemsPerPage={50} ulProps={{className: "list-unstyled"}} pageKey={false} key={alphaChosen}> | ||
{[...displayedAuthors.keys()].sort().map(authorName => | ||
<span key={authorName} className="flex flex-wrap gap-2"> | ||
<span>{authorName}</span> | ||
{authors.get(authorName)} | ||
</span> | ||
)} | ||
</PagedList> | ||
<nav id="author-filter" aria-label="Author name filtering"> | ||
<ul className="list-unstyled"> | ||
{alphaChoices.map(choice => | ||
<li key={choice}> | ||
<button | ||
className="hocus:underline" | ||
onClick={() => setAlphaChosen(choice)} aria-label={"Show authors that start with " + choice} | ||
> | ||
{choice} | ||
</button> | ||
</li> | ||
)} | ||
</ul> | ||
</nav> | ||
</div> | ||
) | ||
} | ||
export default FilteringAuthorList |
36 changes: 32 additions & 4 deletions
36
src/components/paragraphs/sup-pre-built/pre-built-author-list.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,41 @@ | ||
import {HTMLAttributes} from "react"; | ||
import {HTMLAttributes, JSX} from "react"; | ||
import {graphqlClient} from "@lib/gql/gql-client"; | ||
import {BooksQuery, NodeSupBook} from "@lib/gql/__generated__/drupal"; | ||
import Link from "@components/elements/link"; | ||
import FilteringAuthorList from "@components/paragraphs/sup-pre-built/filtering-author-list"; | ||
|
||
type Props = HTMLAttributes<HTMLDivElement>; | ||
|
||
const PreBuiltAuthorList = async ({...props}: Props) => { | ||
// Fetch all the books, sort by authors, and then build pagination and side alpha selection. | ||
let fetchMore = true; | ||
let query: BooksQuery; | ||
let afterCursor = null; | ||
let books: NodeSupBook[] = []; | ||
|
||
while (fetchMore) { | ||
query = await graphqlClient({next: {tags: ["paths"]}}).Books({after: afterCursor}) | ||
fetchMore = query.nodeSupBooks.pageInfo.hasNextPage | ||
afterCursor = query.nodeSupBooks.pageInfo.endCursor; | ||
books = [...books, ...query.nodeSupBooks.nodes as NodeSupBook[]]; | ||
} | ||
|
||
const authors = new Map<string, JSX.Element[]>(); | ||
|
||
books.map(book => { | ||
book.supBookAuthors?.map(author => { | ||
if (author.credentials && author.credentials.length > 0) { | ||
const authorName = ([author.family, author.given + " " + author.middle].filter(a => !!a).join(", ") + ` [${author.credentials}]`).trim() | ||
|
||
const authorsBooks = authors.get(authorName) || []; | ||
authors.set(authorName, [...authorsBooks, | ||
<Link key={book.id} prefetch={false} href={book.path}>{book.title}</Link>]); | ||
} | ||
}) | ||
}) | ||
|
||
return ( | ||
<div {...props}> | ||
Authoring list | ||
</div> | ||
<FilteringAuthorList authors={authors} {...props}/> | ||
) | ||
} | ||
export default PreBuiltAuthorList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.