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

add author list to article #75

Merged
merged 2 commits into from
Dec 7, 2023
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
41 changes: 38 additions & 3 deletions lib/strapi/newsGraphQL.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { fetchStrapiGraphQL } from "./fetchStrapiGraphQL";

export const fetchPhotoThumbnailUrl = async (slug) => {
let res = await fetchStrapiGraphQL(`
query {
people(filters: { slug: { eq: "${slug}" }}) {
data {
attributes {
photo {
data {
attributes {
formats
}
}
}
}
}
}
}
`);
return res?.data?.people?.data?.[0]?.attributes?.photo?.data?.[0]?.attributes?.formats?.thumbnail ?? null;
}

export const fetchArticle = async (slug) => {
const articleGql = await fetchStrapiGraphQL(`
fragment PersonAttributes on PersonRelationResponseCollection {
Expand All @@ -8,6 +29,7 @@ export const fetchArticle = async (slug) => {
firstName
lastName
slug
active
}
}
}
Expand Down Expand Up @@ -105,10 +127,23 @@ export const fetchArticle = async (slug) => {

if (articleGql?.data?.posts?.data?.length !== 1) return null;

return articleGql.data.posts.data.map(({ attributes }) => ({
let photos = await Promise.allSettled(articleGql.data.posts.data[0].attributes.renciAuthors.data.map(({ attributes }) => (
fetchPhotoThumbnailUrl(attributes.slug)
)))

console.log(photos);

return await articleGql.data.posts.data.map(({ attributes }) => ({
...attributes,
renciAuthors: attributes.renciAuthors.data.map(({ attributes }) => attributes),
people: attributes.people.data.map(({ attributes }) => ({ ...attributes, name: `${attributes.firstName} ${attributes.lastName}`})),
renciAuthors: attributes.renciAuthors.data.map(({ attributes }, i) => ({
...attributes,
name: `${attributes.firstName} ${attributes.lastName}`,
photo: photos[i].status === "fulfilled" ? photos[i].value : null,
})),
people: attributes.people.data.map(({ attributes }) => ({
...attributes,
name: `${attributes.firstName} ${attributes.lastName}`
})),
researchGroups: attributes.researchGroups.data.map(({ attributes }) => attributes),
collaborations: attributes.collaborations.data.map(({ attributes }) => attributes),
projects: attributes.projects.data.map(({ attributes }) => attributes),
Expand Down
25 changes: 24 additions & 1 deletion pages/news/[year]/[month]/[day]/[slug].js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Fragment } from "react"
import { Page, Section } from "@/components/layout";
import { fetchArticle, fetchStrapiGraphQL } from "@/lib/strapi";
import { Divider, Typography, Stack, styled } from "@mui/material";
import { Divider, Typography, Stack, styled, Avatar } from "@mui/material";
import { Markdown } from "@/components/markdown";
import Image from "next/image";
import { ArticleDate } from "@/components/news/article-date"
Expand All @@ -26,6 +26,11 @@ export default function Article({ article }) {
return `/news?${qs.stringify({[type]: id})}`
}

let authors = [
...article.renciAuthors,
...article.externalAuthors?.split(",").map((a) => a.trim()) ?? []
]

return (
<Page hideTitle title={article.title} description={article.subtitle}>

Expand Down Expand Up @@ -73,6 +78,24 @@ export default function Article({ article }) {
})}
</Stack>

{Boolean(authors.length) && <Stack my={2} flexDirection="row" alignItems="center" gap={1} flexWrap="wrap">
{authors.reduce((acc, a, i) => {
let out;
if (typeof a === "string") out = <span>{a}</span>;
else if (!a.active) out = <span>{a.name}</span>;
else out = <Link to={`/people/${a.slug}`} key={a.slug}>
<Stack flexDirection="row" alignItems="center" sx={{ maxWidth: "fit-content" }}>
{Boolean(a.photo) && <Avatar alt={`A thumbnail photo of ${a.name}`} src={a.photo.url} sx={{ mr: 1, width: '2lh', height: '2lh' }} />}
<span>{a.name}</span>
</Stack>
</Link>

acc.push(out);
if(i < authors.length - 1) acc.push("·");
return acc;
}, [])}
</Stack>}

<Divider sx={{ margin: '1rem 0'}}/>


Expand Down