Created
February 1, 2019 14:29
-
-
Save kitze/6c1dae5052ce4dde66867946622242bc to your computer and use it in GitHub Desktop.
react static config
This file contains hidden or 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 React from 'react'; | |
import { GraphQLClient } from 'graphql-request'; | |
import homeQuery from './queries/home'; | |
import orderBy from 'lodash/orderBy'; | |
import isSameDay from 'date-fns/is_same_day'; | |
import format from 'date-fns/format'; | |
const path = require('path'); | |
const client = new GraphQLClient( | |
'', | |
{ | |
headers: { | |
Authorization: | |
'' | |
} | |
} | |
); | |
const getWorkshopData = workshop => { | |
const { workshopType } = workshop; | |
const dateFormat = 'DD MMMM YYYY'; | |
const shortFormat = 'DD.MM.YY'; | |
const timeFormat = 'HH:mm'; | |
const showEndDate = !isSameDay(workshop.dateStart, workshop.dateEnd); | |
const timeStart = format(workshop.dateStart, timeFormat); | |
const timeEnd = format(workshop.dateEnd, timeFormat); | |
let dates = []; | |
const hasMultipleDates = | |
workshop.workshopDates.length > 0 && | |
workshop.workshopDates.every(d => d.date !== null); | |
if (hasMultipleDates) { | |
const orderedDates = orderBy(workshop.workshopDates, d => d.date); | |
dates = orderedDates.map(d => format(d.date, shortFormat)); | |
} else { | |
dates.push( | |
`${format(workshop.dateStart, dateFormat)} ${ | |
showEndDate ? `- ${format(workshop.dateEnd, dateFormat)}` : '' | |
}` | |
); | |
} | |
const metaTitle = `${workshopType.title} Workshop @ ${ | |
workshop.conferenceName | |
} ${format(workshop.dateStart, 'YYYY')}`; | |
const metaDescription = ` | |
${dates} - ${workshop.city}, ${workshop.country}. | |
${workshopType.description} | |
`; | |
return { | |
meta: { | |
title: metaTitle, | |
description: metaDescription | |
}, | |
dates, | |
// timeStart: timeStart === '00:00' ? '09:00' : timeStart, | |
// timeEnd: timeEnd === '00:00' ? '09:00' : timeEnd, | |
timeStart: '09:00', | |
timeEnd: '17:00' | |
}; | |
}; | |
const getWorkshopTypeData = workshopType => { | |
const metaTitle = `${workshopType.title}`; | |
const metaDescription = `${workshopType.description}`; | |
return { | |
meta: { | |
title: metaTitle, | |
description: metaDescription | |
} | |
}; | |
}; | |
export default { | |
disablePreload: true, | |
plugins: [ | |
'react-static-plugin-emotion' | |
], | |
webpack: config => ({ | |
...config, | |
resolve: { | |
...config.resolve, | |
modules: [...config.resolve.modules, path.join(__dirname, 'src')] | |
} | |
}), | |
Document: ({ Html, Head, Body, children }) => ( | |
<Html> | |
<Head> | |
<meta name="viewport" content="width=device-width, initial-scale=1" /> | |
</Head> | |
<Body> | |
<div id="___modal" /> | |
{children} | |
</Body> | |
</Html> | |
), | |
getRoutes: async () => { | |
const data = await client.request(homeQuery); | |
const { allWorkshops, allWorkshopTypes, allSettingses } = data; | |
const settings = allSettingses[0]; | |
return [ | |
{ | |
path: '/', | |
component: 'src/pages/Home', | |
getData: () => ({ | |
data | |
}), | |
getSiteData: () => {}, | |
children: [ | |
...allWorkshops | |
.filter(workshop => workshop.createPage === true) | |
.map(workshop => ({ | |
path: `/workshop/${workshop.slug}`, | |
component: 'src/pages/Workshop', | |
getData: () => ({ | |
workshop, | |
workshopExtra: getWorkshopData(workshop), | |
settings | |
}) | |
})), | |
...allWorkshops | |
.filter(workshop => workshop.createPage === true) | |
.map(workshop => ({ | |
path: `/workshop/${workshop.slug}/image`, | |
component: 'src/pages/WorkshopImage', | |
getData: () => ({ | |
workshop, | |
workshopExtra: getWorkshopData(workshop), | |
settings | |
}) | |
})), | |
...allWorkshops | |
.filter(workshop => workshop.createPage === true) | |
.map(workshop => ({ | |
path: `/workshop/${workshop.slug}/printable`, | |
component: 'src/printable/WorkshopPrintable', | |
getData: () => ({ | |
workshop, | |
workshopExtra: getWorkshopData(workshop), | |
settings | |
}) | |
})), | |
...allWorkshopTypes.map(workshopType => ({ | |
path: `/workshop-type/${workshopType.slug}`, | |
component: 'src/pages/WorkshopType', | |
getData: () => ({ | |
workshopType, | |
settings, | |
workshopTypeExtra: getWorkshopTypeData(workshopType) | |
}) | |
})), | |
...allWorkshopTypes.map(workshopType => ({ | |
path: `/workshop-type/${workshopType.slug}/printable`, | |
component: 'src/printable/WorkshopTypePrintable', | |
getData: () => ({ | |
workshopType, | |
settings, | |
workshopTypeExtra: getWorkshopTypeData(workshopType) | |
}) | |
})) | |
] | |
} | |
]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment