From 569b6cb35d62ec3fcc13a4beedf18e3bbefaf959 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Sat, 11 Jul 2020 10:29:42 +0200 Subject: [PATCH] cleanup location --- gatsby-node.js | 2 +- gatsby/createPages.js | 6 +++--- gatsby/feeds.js | 7 +++---- src/components/Layout.tsx | 7 +------ src/components/molecules/PostTeaser.tsx | 7 +------ src/components/templates/Archive.test.tsx | 9 +-------- src/components/templates/Archive.tsx | 4 +--- src/components/templates/Page.module.scss | 2 +- src/components/templates/Page.tsx | 7 ++++--- src/pages/404.tsx | 8 +++----- src/pages/index.tsx | 17 +++-------------- src/pages/photos.tsx | 19 ++++--------------- src/pages/tags.tsx | 9 ++++----- 13 files changed, 30 insertions(+), 74 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index 8515ad1b..c25ce4ae 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -81,7 +81,7 @@ exports.onPostBuild = async ({ graphql }) => { // JSON Feed query const result = await graphql(` { - allMarkdownRemark { + allMarkdownRemark(sort: { order: DESC, fields: [fields___date] }) { edges { node { html diff --git a/gatsby/createPages.js b/gatsby/createPages.js index 8071bf05..75e9049b 100644 --- a/gatsby/createPages.js +++ b/gatsby/createPages.js @@ -1,7 +1,9 @@ const path = require('path') -const archiveTemplate = path.resolve('src/components/templates/Posts.tsx') const { itemsPerPage } = require('../config') +const postTemplate = path.resolve('src/components/templates/Post/index.tsx') +const archiveTemplate = path.resolve('src/components/templates/Archive.tsx') + const redirects = [ { f: '/feed', t: '/feed.xml' }, { f: '/feed/', t: '/feed.xml' }, @@ -24,8 +26,6 @@ function getPaginationData(i, numPages, slug) { } exports.generatePostPages = (createPage, posts) => { - const postTemplate = path.resolve('src/components/templates/Post/index.tsx') - // Create Post pages posts.forEach((post) => { createPage({ diff --git a/gatsby/feeds.js b/gatsby/feeds.js index d9ac24c5..a9e8b457 100644 --- a/gatsby/feeds.js +++ b/gatsby/feeds.js @@ -35,7 +35,7 @@ async function jsonItems(posts) { }) } -const createJsonFeed = (posts) => ({ +const createJsonFeed = async (posts) => ({ version: 'https://jsonfeed.org/version/1', title: siteTitle, description: siteDescription, @@ -49,19 +49,18 @@ const createJsonFeed = (posts) => ({ name: author.name, url: author.uri }, - items: jsonItems(posts) + items: await jsonItems(posts) }) const generateJsonFeed = async (posts) => { await writeFile( path.join('./public', 'feed.json'), - JSON.stringify(createJsonFeed(posts)), + JSON.stringify(await createJsonFeed(posts)), 'utf8' ).catch((err) => { throw Error('\nFailed to write JSON Feed file: ', err) }) - // eslint-disable-next-line no-console console.log('\nsuccess Generating JSON feed') } diff --git a/src/components/Layout.tsx b/src/components/Layout.tsx index eac664c7..63c76e65 100644 --- a/src/components/Layout.tsx +++ b/src/components/Layout.tsx @@ -11,12 +11,7 @@ import styles from './Layout.module.scss' // whyDidYouRender(React) // } -export default function Layout({ - children -}: { - location?: Location - children: any -}): ReactElement { +export default function Layout({ children }: { children: any }): ReactElement { return ( <> diff --git a/src/components/molecules/PostTeaser.tsx b/src/components/molecules/PostTeaser.tsx index d9ee477e..477c4d60 100644 --- a/src/components/molecules/PostTeaser.tsx +++ b/src/components/molecules/PostTeaser.tsx @@ -10,10 +10,6 @@ export const postTeaserQuery = graphql` fileAbsolutePath frontmatter { title - type - linkurl - tags - featured image { childImageSharp { ...ImageFluidThumb @@ -22,7 +18,6 @@ export const postTeaserQuery = graphql` } fields { slug - date(formatString: "MMMM DD, YYYY") } } ` @@ -31,7 +26,7 @@ export default function PostTeaser({ post, toggleSearch }: { - post: Post + post: Partial toggleSearch?: () => void }): ReactElement { const { image, title } = post.frontmatter diff --git a/src/components/templates/Archive.test.tsx b/src/components/templates/Archive.test.tsx index 0088983a..8a33b1ed 100644 --- a/src/components/templates/Archive.test.tsx +++ b/src/components/templates/Archive.test.tsx @@ -1,13 +1,10 @@ import React from 'react' import { render } from '@testing-library/react' -import { createHistory, createMemorySource } from '@reach/router' import Archive from './Archive' import data from '../../../jest/__fixtures__/posts.json' describe('Archive', () => { - const history = createHistory(createMemorySource('/photos')) - const pageContext = { tag: 'hello', slug: '/hello', @@ -17,11 +14,7 @@ describe('Archive', () => { it('renders without crashing', () => { const { container } = render( - + ) expect(container.firstChild).toBeInTheDocument() }) diff --git a/src/components/templates/Archive.tsx b/src/components/templates/Archive.tsx index b422f36b..a9ba28a6 100644 --- a/src/components/templates/Archive.tsx +++ b/src/components/templates/Archive.tsx @@ -8,16 +8,14 @@ import PostContent from './Post/Content' import PostMore from './Post/More' import PostLinkActions from './Post/LinkActions' import SEO from '../atoms/SEO' -import styles from './Posts.module.scss' +import styles from './Archive.module.scss' import { Image } from '../atoms/Image' export default function Archive({ data, - location, pageContext }: { data: any - location: Location pageContext: { tag: string slug: string diff --git a/src/components/templates/Page.module.scss b/src/components/templates/Page.module.scss index ad08fe03..0a9b7e68 100644 --- a/src/components/templates/Page.module.scss +++ b/src/components/templates/Page.module.scss @@ -1,6 +1,6 @@ @import 'variables'; .pageTitle { - composes: archivetitle from './Posts.module.scss'; + composes: archivetitle from './Archive.module.scss'; margin-bottom: $spacer * 2; } diff --git a/src/components/templates/Page.tsx b/src/components/templates/Page.tsx index cc65516a..e1cc8e20 100644 --- a/src/components/templates/Page.tsx +++ b/src/components/templates/Page.tsx @@ -1,12 +1,12 @@ import React, { ReactElement } from 'react' import { Helmet } from 'react-helmet' +import { useLocation } from '@reach/router' import { Post } from '../../@types/Post' import SEO from '../atoms/SEO' import styles from './Page.module.scss' export default function Page({ title, - location, section, children, post @@ -14,13 +14,14 @@ export default function Page({ title: string children: any section?: string - location: Location post?: Post }): ReactElement { + const { pathname } = useLocation() + return ( <> - +

{title}

{section ?
{children}
: children} diff --git a/src/pages/404.tsx b/src/pages/404.tsx index b6c11c39..0bdd5747 100644 --- a/src/pages/404.tsx +++ b/src/pages/404.tsx @@ -1,5 +1,5 @@ import React, { ReactElement } from 'react' -import { Link } from 'gatsby' +import { Link, PageProps } from 'gatsby' import Page from '../components/templates/Page' import styles from './404.module.scss' @@ -9,14 +9,12 @@ const page = { } } -const NotFound = ({ location }: { location: Location }): ReactElement => ( - +const NotFound = (props: PageProps): ReactElement => ( +
- {/* eslint-disable-next-line quotes */}

{"I'm sorry Dave"}

{' '} - {/* eslint-disable-next-line quotes */}

{"I'm afraid I can't do that"}

Back to homepage
diff --git a/src/pages/index.tsx b/src/pages/index.tsx index d254efdf..dcfbf3e3 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,5 +1,5 @@ import React, { ReactElement } from 'react' -import { graphql, Link } from 'gatsby' +import { graphql, Link, PageProps } from 'gatsby' import Page from '../components/templates/Page' import { Post } from '../@types/Post' import { Image } from '../components/atoms/Image' @@ -13,20 +13,9 @@ const page = { } } -export default function Home({ - data, - location -}: { - data: any - location: Location -}): ReactElement { +export default function Home(props: PageProps): ReactElement { return ( - + Latest Posts & Links
diff --git a/src/pages/photos.tsx b/src/pages/photos.tsx index a8e864df..93f5a902 100644 --- a/src/pages/photos.tsx +++ b/src/pages/photos.tsx @@ -1,5 +1,5 @@ import React, { ReactElement } from 'react' -import { graphql, Link } from 'gatsby' +import { graphql, Link, PageProps } from 'gatsby' import Page from '../components/templates/Page' import { Post } from '../@types/Post' import { Image } from '../components/atoms/Image' @@ -28,21 +28,10 @@ const PhotoThumb = ({ photo }: { photo: Post }): ReactElement => { ) } -export default function Photos({ - data, - location -}: { - data: any - location: Location -}): ReactElement { +export default function Photos(props: PageProps): ReactElement { return ( - - {data.photos.edges.map(({ node }: { node: Post }) => ( + + {(props.data as any).photos.edges.map(({ node }: { node: Post }) => ( ))} diff --git a/src/pages/tags.tsx b/src/pages/tags.tsx index 7e6b9334..7dd2dbdd 100644 --- a/src/pages/tags.tsx +++ b/src/pages/tags.tsx @@ -1,5 +1,5 @@ import React, { ReactElement } from 'react' -import { graphql } from 'gatsby' +import { graphql, PageProps } from 'gatsby' import Page from '../components/templates/Page' import Tag from '../components/atoms/Tag' import styles from './tags.module.scss' @@ -16,15 +16,14 @@ interface Tag { totalCount: number } -interface TagsPageProps { +interface TagsPageProps extends PageProps { data: { allMarkdownRemark: { group: Tag[] } } - location: Location } -const TagsPage = ({ data, location }: TagsPageProps): ReactElement => ( - +const TagsPage = ({ data }: TagsPageProps): ReactElement => ( +
    {data.allMarkdownRemark.group .sort((a, b) => b.totalCount - a.totalCount)