Skip to content

Commit

Permalink
Refactor authors loading
Browse files Browse the repository at this point in the history
  • Loading branch information
miensol committed May 18, 2021
1 parent 484441d commit c429c51
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 51 deletions.
38 changes: 7 additions & 31 deletions src/components/subcomponents/TeamMembers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from "react"
import styled from "styled-components"
import { routeLinks } from '../../config/routing'
import { GatsbyImage, getImage } from 'gatsby-plugin-image'
import { useAuthors } from '../../use-authors/use-authors'

const TeamMember = styled.article`
border: 1px solid rgba(0, 0, 0, 0.125);
Expand Down Expand Up @@ -69,45 +70,20 @@ const Container = styled.div`
margin-bottom: 122px;
`
const TeamMembers = () => {
const {
allMarkdownRemark: { nodes },
} = useStaticQuery(graphql`
query {
allMarkdownRemark(
filter: { frontmatter: { ex: { ne: true }, author_id: { ne: null } } }
sort: { fields: frontmatter___author_id }
) {
nodes {
frontmatter {
avatar {
childImageSharp {
gatsbyImageData
}
}
author_id
name
short_name
slug
bio
hobby
}
}
}
}
`)
const members = useAuthors()

return (
<Container>
{nodes.map(v => {
const member = v.frontmatter
{members.map(member => {
return (
<TeamMember key={member.author_id}>
<Link to={`${routeLinks.aboutUs}/${member.slug || member.author_id}`}>
<TeamMember key={member.authorId}>
<Link to={routeLinks.aboutUs(member)}>
<figure>
<GatsbyImage image={getImage(member.avatar)!} alt={member.name} />
</figure>
<div>
<p>
<strong>{member.short_name}</strong>
<strong>{member.shortName}</strong>
</p>
<p>{member.bio}</p>
<p>{member?.hobby}</p>
Expand Down
14 changes: 11 additions & 3 deletions src/config/routing.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
export const routeLinks = {
aboutUs: '/about-us',
aboutUs(params?: { authorId: string } | { page: 'story' | 'values' | 'team' }) {
if (!params) {
return `/about-us/`
}
if ('authorId' in params) {
return `/about-us/${params.authorId}/`
}
return `/about-us/${params.page}/`
},
whatWeOffer: '/what-we-offer',
projects: '/projects',
career: '/career',
blog: '/blog',
startProject: '/start-project',
privacyPolicy: '/privacy-policy'
}
privacyPolicy: '/privacy-policy',
}
4 changes: 2 additions & 2 deletions src/layout/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Footer = () => {
</a>
</div>
<SocialIcons className='padded-right' />
{/*
{/*
<div
className='clutch-widget content'
data-url='https://widget.clutch.co'
Expand All @@ -48,7 +48,7 @@ export const Footer = () => {
explore more
</h6>
<p className='content'>
<a href={routeLinks.aboutUs}>about us</a>
<a href={routeLinks.aboutUs()}>about us</a>
</p>
<p className='content'>
<a href={routeLinks.whatWeOffer}>what we offer</a>
Expand Down
2 changes: 1 addition & 1 deletion src/layout/TopNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface MenuElement {
}

const TopMenu: MenuElement[] = [
{ link: `${routeLinks.aboutUs}/story`, title: 'why us' },
{ link: routeLinks.aboutUs({page: 'story'}), title: 'why us' },
{ link: routeLinks.whatWeOffer, title: 'what we do' },
{ link: routeLinks.projects, title: 'case studies' },
{ link: routeLinks.career, title: 'career' },
Expand Down
2 changes: 1 addition & 1 deletion src/pages/about-us/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const AboutUsPage: React.FC = ({ children }) => (
{tabs.map(tab => (
<Link
activeClassName="is-active"
to={`${routeLinks.aboutUs}/${tab.path}`}
to={routeLinks.aboutUs({page: tab.path})}
partiallyActive
key={tab.label}
>
Expand Down
2 changes: 1 addition & 1 deletion src/templates/AboutUsTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function Template({
<p dangerouslySetInnerHTML={{ __html: html }}></p>
</div>
</div>
<BackButton url={`${routeLinks.aboutUs}/team`} label="About us"/>
<BackButton url={routeLinks.aboutUs({page: 'team'})} label="About us"/>
</article>
</div>
{/*
Expand Down
2 changes: 1 addition & 1 deletion src/templates/post/AuthorData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const AuthorData: React.FC<AuthorDataProps> = ({ authorId }) => {
const [{ avatar, name, bio }] = authorId ? useAuthors({ authorId, avatarSize: { width: 64 } }) : []

const LinkComponent = authorId
? (props: { children?: ReactNode }) => <Link to={`${routeLinks.aboutUs}/${authorId}`}>{props.children}</Link>
? (props: { children?: ReactNode }) => <Link to={routeLinks.aboutUs({ authorId })}>{props.children}</Link>
: (props: { children?: ReactNode }) => <span>{props.children}</span>

return (
Expand Down
14 changes: 7 additions & 7 deletions src/use-authors/authors-frontmatter-query-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ interface AuthorsFrontmatterItem {
avatar: IGatsbyImageData
bio: string
name: string
short_name: string
hobby: string | undefined
web: string | undefined
}

export interface Author {
authorId: string
name: string
bio: string
avatar: IGatsbyImageData
}

function toAuthor(frontmatter: AuthorsFrontmatterItem) {
return {
authorId: frontmatter.author_id,
bio: frontmatter.bio,
web: frontmatter.web,
avatar: frontmatter.avatar,
name: frontmatter.name,
shortName: frontmatter.short_name,
hobby: frontmatter.hobby,
}
}

export type Author = ReturnType<typeof toAuthor>

export function toAuthors(queryResult: AuthorsFrontmatterQueryResult): Author[] {
return queryResult.allMarkdownRemark.nodes.map(({ frontmatter }) => toAuthor(frontmatter))
}
Expand Down
30 changes: 30 additions & 0 deletions src/use-authors/use-authors-avatar-default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { graphql, useStaticQuery } from 'gatsby'
import { AuthorsFrontmatterQueryResult, toAuthors } from './authors-frontmatter-query-result'

const useAuthorsAvatarsDefaultQuery: () => AuthorsFrontmatterQueryResult = () =>
useStaticQuery(graphql`
{
allMarkdownRemark(
filter: { frontmatter: { author_id: { ne: null } } }
sort: { fields: frontmatter___author_id }
) {
nodes {
frontmatter {
author_id
avatar {
childImageSharp {
gatsbyImageData
}
}
name
short_name
slug
bio
hobby
}
}
}
}
`)

export const useAuthorsAvatarsDefault = () => toAuthors(useAuthorsAvatarsDefaultQuery())
12 changes: 8 additions & 4 deletions src/use-authors/use-authors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Author } from './authors-frontmatter-query-result'
import { useAuthorsAvatars64 } from './use-authors-avatar-64'
import { useAuthorsAvatarsDefault } from './use-authors-avatar-default'

type UseAuthorsParams = {
authorId?: string
avatarSize: { width: 64 }
avatarSize?: { width: 64 }
}

function toAuthorsFilter(props: UseAuthorsParams) {
Expand All @@ -13,13 +14,16 @@ function toAuthorsFilter(props: UseAuthorsParams) {
return () => false
}

export const useAuthors = (props: UseAuthorsParams) => {
export const useAuthors = (props: UseAuthorsParams = {}) => {
let data: Author[]

switch (props.avatarSize.width) {
switch (props.avatarSize?.width) {
case 64:
default:
data = useAuthorsAvatars64()
break;
default:
data = useAuthorsAvatarsDefault()
break;
}

const authorsFilter = toAuthorsFilter(props)
Expand Down

0 comments on commit c429c51

Please sign in to comment.