From a2e5aa2e6f005ab5333bcbdfd547920940e72ae2 Mon Sep 17 00:00:00 2001 From: Jonas Braathen Date: Sun, 24 Feb 2019 13:06:05 +0100 Subject: [PATCH] Add associations --- gatsby-config.js | 1 + gatsby-node.js | 66 +++++++++++++++++++++++-- src/components/AssociationList.js | 70 +++++++++++++++++++++++++++ src/templates/association-list.js | 38 +++++++++++++++ src/templates/association.js | 80 +++++++++++++++++++++++++++++++ src/templates/event.js | 8 ++-- 6 files changed, 255 insertions(+), 8 deletions(-) create mode 100644 src/components/AssociationList.js create mode 100644 src/templates/association-list.js create mode 100644 src/templates/association.js diff --git a/gatsby-config.js b/gatsby-config.js index 8a7c1cd..2f55704 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -22,6 +22,7 @@ module.exports = { // Set to true to debug endpoints on 'gatsby build' verboseOutput: true, includedRoutes: [ + '**/*/*/associations', '**/*/*/categories', '**/*/*/events', '**/*/*/event_types', diff --git a/gatsby-node.js b/gatsby-node.js index 6d2dd41..26d8c12 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -198,6 +198,60 @@ exports.createPages = ({ actions, graphql }) => { } ) }) + .then(() => { + return graphql(` + { + allWordpressWpAssociations { + edges { + node { + id + slug + status + fields { + link + } + } + } + } + } + `) + }) + .then(result => { + if (result.errors) { + result.errors.forEach(e => console.error(e.toString())) + return Promise.reject(result.errors) + } + + const associationTemplate = path.resolve(`./src/templates/association.js`) + + // In production builds, filter for only published associations. + const allAssociations = result.data.allWordpressWpAssociations.edges + const associations = + process.env.NODE_ENV === 'production' + ? getOnlyPublished(allAssociations) + : allAssociations + + // Iterate over the array of associations + _.each(associations, ({ node: association }) => { + // Create the Gatsby page for this WordPress event + createPage({ + path: association.fields.link, + component: associationTemplate, + context: { + id: association.id, + }, + }) + }) + + // Create the list of associations + const associationListTemplate = path.resolve( + `./src/templates/association-list.js` + ) + createPage({ + path: `/foreningene/`, + component: associationListTemplate, + }) + }) .then(() => { return graphql(` { @@ -315,11 +369,13 @@ exports.onCreateNode = ({ node, actions, getNode }) => { }) } - if ( - node.internal.type === 'wordpress__POST' || - node.internal.type === 'wordpress__wp_events' || - node.internal.type === 'wordpress__wp_event_types' - ) { + relativeLinksFor = [ + 'wordpress__POST', + 'wordpress__wp_associations', + 'wordpress__wp_events', + 'wordpress__wp_event_types', + ] + if (relativeLinksFor.includes(node.internal.type)) { const link = new URL(node.link).pathname createNodeField({ node, diff --git a/src/components/AssociationList.js b/src/components/AssociationList.js new file mode 100644 index 0000000..2ea6de2 --- /dev/null +++ b/src/components/AssociationList.js @@ -0,0 +1,70 @@ +import React from 'react' +import PropTypes from 'prop-types' +import { Link, graphql } from 'gatsby' +import Img from 'gatsby-image' + +export default class AssociationList extends React.Component { + render_association(association) { + return ( +
+ + {association.featured_media && association.featured_media.localFile && ( +
+ +
+ )} +

{association.title}

+ + +
+
+ ) + } + + render() { + const { associations, title } = this.props + + return ( +
+

{title}

+
+ {associations.map(({ node: association }) => + this.render_association(association) + )} +
+
+ ) + } +} + +AssociationList.propTypes = { + associations: PropTypes.arrayOf(PropTypes.object), + title: PropTypes.string, +} + +export const pageQuery = graphql` + fragment AssociationListFields on wordpress__wp_associations { + id + title + excerpt + featured_media { + localFile { + ...FluidImage + } + } + fields { + link + } + } +` diff --git a/src/templates/association-list.js b/src/templates/association-list.js new file mode 100644 index 0000000..f99af45 --- /dev/null +++ b/src/templates/association-list.js @@ -0,0 +1,38 @@ +import React from 'react' +import PropTypes from 'prop-types' +import { graphql } from 'gatsby' +import Layout from '../components/Layout' +import AssociationList from '../components/AssociationList' + +export default class AssociationListPage extends React.Component { + render() { + const { data } = this.props + const { edges: associations } = data.allWordpressWpAssociations + + return ( + + + + ) + } +} + +AssociationListPage.propTypes = { + data: PropTypes.shape({ + allWordpressWpEvents: PropTypes.shape({ + edges: PropTypes.array, + }), + }), +} + +export const pageQuery = graphql` + query { + allWordpressWpAssociations(sort: { fields: title, order: ASC }) { + edges { + node { + ...AssociationListFields + } + } + } + } +` diff --git a/src/templates/association.js b/src/templates/association.js new file mode 100644 index 0000000..81fdca3 --- /dev/null +++ b/src/templates/association.js @@ -0,0 +1,80 @@ +import React from 'react' +import PropTypes from 'prop-types' +import { graphql } from 'gatsby' +import Layout from '../components/Layout' +import Img from 'gatsby-image' + +export const AssociationTemplate = data => { + const { + title, + content, + hasFeaturedMedia, + featuredMedia, + type, + homepage, + } = data + return ( +
+

{title}

+ {hasFeaturedMedia && ( +
+ +
+ )} + {homepage && ( +
+ Nettside +
+ )} +
Type: {type}
+
+
+ ) +} + +AssociationTemplate.propTypes = { + title: PropTypes.string.isRequired, + content: PropTypes.string, +} + +const Association = ({ data }) => { + const { wordpressWpAssociations: association } = data + + return ( + + + + ) +} + +Association.propTypes = { + data: PropTypes.object.isRequired, +} + +export default Association + +export const associationQuery = graphql` + query AssociationById($id: String!) { + wordpressWpAssociations(id: { eq: $id }) { + title + content + association_type + association_homepage + featured_media { + localFile { + ...FluidImage + } + } + } + } +` diff --git a/src/templates/event.js b/src/templates/event.js index 374c8ff..36b675f 100644 --- a/src/templates/event.js +++ b/src/templates/event.js @@ -94,9 +94,11 @@ export const EventTemplate = ({ )}
-
- -
+ {hasFeaturedMedia && ( +
+ +
+ )}