Instantly share code, notes, and snippets.
Last active
February 7, 2024 16:17
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save Fran-A-Dev/0a5d24513e490205e46fa1b501c85e94 to your computer and use it in GitHub Desktop.
Single Authors Page Exampe/Faust.js wp-template
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
import { gql } from "@apollo/client"; | |
import { useFaustQuery } from "@faustwp/core"; | |
import { useRouter } from "next/router"; | |
import { | |
Container, | |
ContentWrapper, | |
EntryHeader, | |
FeaturedImage, | |
Footer, | |
Header, | |
Main, | |
NavigationMenu, | |
SEO, | |
} from "../components"; | |
import * as MENUS from "../constants/menus"; | |
import { BlogInfoFragment } from "../fragments/GeneralSettings"; | |
const GET_LAYOUT_QUERY = gql` | |
${BlogInfoFragment} | |
${NavigationMenu.fragments.entry} | |
query GetLayout( | |
$headerLocation: MenuLocationEnum | |
$footerLocation: MenuLocationEnum | |
) { | |
generalSettings { | |
...BlogInfoFragment | |
} | |
headerMenuItems: menuItems(where: { location: $headerLocation }) { | |
nodes { | |
...NavigationMenuItemFragment | |
} | |
} | |
footerMenuItems: menuItems(where: { location: $footerLocation }) { | |
nodes { | |
...NavigationMenuItemFragment | |
} | |
} | |
} | |
`; | |
const GET_AUTHOR_INFO = gql` | |
${FeaturedImage.fragments.entry} | |
query GET_AUTHOR_INFO($id: ID!) { | |
user(id: $id, idType: DATABASE_ID) { | |
name | |
description | |
avatar { | |
url | |
} | |
posts { | |
edges { | |
node { | |
id | |
title | |
excerpt | |
date | |
uri | |
...FeaturedImageFragment | |
} | |
} | |
} | |
} | |
} | |
`; | |
export default function Component(props) { | |
const router = useRouter(); | |
const { id } = router.query; | |
const { data, loading, error } = useFaustQuery(GET_AUTHOR_INFO, { | |
variables: { | |
id, | |
}, | |
}); | |
if (props.loading) { | |
return <>Loading...</>; | |
} | |
if (error) { | |
return <>Error{error.message}</>; | |
} | |
const { user } = data || {}; | |
const posts = user?.posts?.edges.map((edge) => edge.node) || []; | |
const { generalSettings, headerMenuItems, footerMenuItems } = | |
useFaustQuery(GET_LAYOUT_QUERY); | |
const { title: siteTitle, description: siteDescription } = generalSettings; | |
const primaryMenu = headerMenuItems?.nodes ?? []; | |
const footerMenu = footerMenuItems?.nodes ?? []; | |
return ( | |
<> | |
<SEO title={siteTitle} description={siteDescription} /> | |
<Header | |
title={siteTitle} | |
description={siteDescription} | |
menuItems={primaryMenu} | |
/> | |
<Main> | |
<> | |
{posts.map((post, index) => ( | |
<article key={index}> | |
<EntryHeader | |
author={user.name} // The name is directly on the user object | |
title={post.title} | |
avatar={avatarUrl} | |
description={user.description} | |
image={post.featuredImage?.node} | |
date={post.date} | |
/> | |
<Container> | |
<ContentWrapper excerpt={post.excerpt} /> | |
</Container> | |
</article> | |
))} | |
</> | |
</Main> | |
<Footer title={siteTitle} menuItems={footerMenu} /> | |
</> | |
); | |
} | |
Component.queries = [ | |
{ | |
query: GET_LAYOUT_QUERY, | |
variables: (seedNode, ctx) => ({ | |
headerLocation: MENUS.PRIMARY_LOCATION, | |
footerLocation: MENUS.FOOTER_LOCATION, | |
}), | |
}, | |
{ | |
query: GET_AUTHOR_INFO, | |
variables: ({ id }, ctx) => ({ | |
id, | |
asPreview: ctx?.asPreview, | |
}), | |
}, | |
]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment