Skip to content

Commit

Permalink
Add feature: pass settings to fetching script
Browse files Browse the repository at this point in the history
  • Loading branch information
usulpro committed Jan 14, 2020
1 parent 28ffeb7 commit 6d9439a
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 83 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# GitNation Content

package for fetching conference content from GraphCMS


## List of specific things that should behave differently

**Image transformations**

Can have various dimensions for different sections / sites

To check: can we set 500x500 as default for all images?
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@focus-reactive/graphql-content-layer",
"version": "1.0.0",
"version": "1.0.1",
"private": false,
"main": "dist/index.js",
"scripts": {
Expand Down
20 changes: 11 additions & 9 deletions src/content.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ const TagColor = ({ title, tag }) => (
*/
const contentPromise = getContent();

queriesData.forEach(({ queryPages, getData, story }) => {
allStories[story] = Query({
name: story,
query: queryPages,
vars: { conferenceTitle, eventYear },
searchVars: { search: '' },
getData,
});
});
queriesData.forEach(
({ queryPages, getData, story, vars }) => {
allStories[story] = Query({
name: story,
query: queryPages,
vars: { conferenceTitle, eventYear, ...vars },
searchVars: { search: '' },
getData,
});
},
);

module.exports = {
default: {
Expand Down
29 changes: 12 additions & 17 deletions src/fetch-committee.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
const { prepareSpeakers } = require('./utils');
const { prepareSpeakers, trySelectSettings } = require('./utils');
const { personFragment } = require('./fragments');

const selectSettings = trySelectSettings(s => s.speakerAvatar.dimensions, {
avatarWidth: 500,
avatarHeight: 500,
});

const queryPages = /* GraphQL */ `
query($conferenceTitle: ConferenceTitle, $eventYear: EventYear) {
query($conferenceTitle: ConferenceTitle, $eventYear: EventYear, $avatarWidth: Int, $avatarHeight: Int) {
conf: conferenceBrand(where: { title: $conferenceTitle }) {
id
status
year: conferenceEvents(where: { year: $eventYear }) {
id
status
committee {
id
name
company
country
bio
githubUrl
twitterUrl
avatar {
url(
transformation: {
image: { resize: { width: 500, height: 500, fit: crop } }
document: { output: { format: jpg } }
}
)
}
...person
}
}
}
}
${personFragment}
`;

const fetchData = async (client, vars) => {
Expand All @@ -46,6 +40,7 @@ const fetchData = async (client, vars) => {

module.exports = {
fetchData,
selectSettings,
queryPages,
getData: data => data.conf.year[0].committee,
story: 'Programme Committee',
Expand Down
10 changes: 8 additions & 2 deletions src/fetch-mc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const { prepareSpeakers } = require('./utils');
const { prepareSpeakers, trySelectSettings } = require('./utils');
const { personFragment } = require('./fragments');

const selectSettings = trySelectSettings(s => s.speakerAvatar.dimensions, {
avatarWidth: 500,
avatarHeight: 500,
});

const queryPages = /* GraphQL */ `
query($conferenceTitle: ConferenceTitle, $eventYear: EventYear) {
query($conferenceTitle: ConferenceTitle, $eventYear: EventYear, $avatarWidth: Int, $avatarHeight: Int) {
conf: conferenceBrand(where: { title: $conferenceTitle }) {
id
status
Expand Down Expand Up @@ -36,6 +41,7 @@ const fetchData = async(client, vars) => {

module.exports = {
fetchData,
selectSettings,
queryPages,
getData: data => data.conf.year[0].mcs,
story: 'MCs',
Expand Down
10 changes: 8 additions & 2 deletions src/fetch-performance.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const { prepareSpeakers } = require('./utils');
const { prepareSpeakers, trySelectSettings } = require('./utils');
const { personFragment } = require('./fragments');

const selectSettings = trySelectSettings(s => s.speakerAvatar.dimensions, {
avatarWidth: 500,
avatarHeight: 500,
});

const queryPages = /* GraphQL */ `
query($conferenceTitle: ConferenceTitle, $eventYear: EventYear) {
query($conferenceTitle: ConferenceTitle, $eventYear: EventYear, $avatarWidth: Int, $avatarHeight: Int) {
conf: conferenceBrand(where: { title: $conferenceTitle }) {
id
status
Expand Down Expand Up @@ -35,6 +40,7 @@ const fetchData = async (client, vars) => {

module.exports = {
fetchData,
selectSettings,
queryPages,
getData: data => data.conf.year[0].performanceTeam,
story: 'performance',
Expand Down
20 changes: 12 additions & 8 deletions src/fetch-speakers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const { prepareSpeakers } = require('./utils');
const { prepareSpeakers, trySelectSettings } = require('./utils');
const { speakerInfoFragment } = require('./fragments');

const selectSettings = trySelectSettings(s => s.speakerAvatar.dimensions, {
avatarWidth: 500,
avatarHeight: 500,
});

const queryPages = /* GraphQL */ `
query($conferenceTitle: ConferenceTitle, $eventYear: EventYear) {
query($conferenceTitle: ConferenceTitle, $eventYear: EventYear, $avatarWidth: Int, $avatarHeight: Int) {
conf: conferenceBrand(where: { title: $conferenceTitle }) {
id
status
Expand All @@ -21,12 +26,10 @@ const queryPages = /* GraphQL */ `
`;

const fetchData = async (client, vars) => {
const data = await client
.request(queryPages, vars)
.then(res => ({
speakers: res.conf.year[0].speakers,
openForTalks: res.conf.year[0].openForTalks,
}));
const data = await client.request(queryPages, vars).then(res => ({
speakers: res.conf.year[0].speakers,
openForTalks: res.conf.year[0].openForTalks,
}));

const { openForTalks } = data;

Expand All @@ -40,6 +43,7 @@ const fetchData = async (client, vars) => {

module.exports = {
fetchData,
selectSettings,
queryPages,
getData: data => data.conf.year[0].speakers,
story: 'speakers',
Expand Down
10 changes: 8 additions & 2 deletions src/fetch-workshops.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const { markdownToHtml } = require('./markdown');
const { prepareSpeakers } = require('./utils');
const { prepareSpeakers, trySelectSettings } = require('./utils');
const { speakerInfoFragment } = require('./fragments');

const selectSettings = trySelectSettings(s => s.speakerAvatar.dimensions, {
avatarWidth: 500,
avatarHeight: 500,
});

const queryPages = /* GraphQL */ `
query ($conferenceTitle: ConferenceTitle, $eventYear: EventYear) {
query ($conferenceTitle: ConferenceTitle, $eventYear: EventYear, $avatarWidth: Int, $avatarHeight: Int) {
conf: conferenceBrand(where: {title: $conferenceTitle}) {
id
status
Expand Down Expand Up @@ -78,6 +83,7 @@ const fetchData = async (client, vars) => {

module.exports = {
fetchData,
selectSettings,
queryPages,
getData: data => data.conf.year[0].schedule,
story: 'schedule/workshops',
Expand Down
12 changes: 6 additions & 6 deletions src/fragments.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const personAvatarFragment = /* GraphQL */ `
fragment avatarUrl on Speaker {
avatar {
url #(
# transformation: {
# image: { resize: { width: 500, height: 500, fit: crop } }
# document: { output: { format: jpg } }
# }
#)
url (
transformation: {
image: { resize: { width: $avatarWidth, height: $avatarHeight, fit: crop } }
document: { output: { format: jpg } }
}
)
}
}
`;
Expand Down
16 changes: 11 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ const createClient = ({ endpoint, token }) => {
const client = createClient(credentials);
const queriesData = [];

const getQueriesData = content => {
const getQueriesData = (content, conferenceSettings) => {
try {
const { fetchData, ...data } = content;
const { fetchData, selectSettings = () => undefined, ...data } = content;
if (!Object.keys(data).length) return;
data.vars = selectSettings(conferenceSettings);
queriesData.push(data);
} catch (err) {}
};

const getContent = async () => {
const getContent = async conferenceSettings => {
const fetchAll = [
textContent,
pageContent,
Expand All @@ -51,8 +52,13 @@ const getContent = async () => {
committeeContent,
].map(async content => {
try {
getQueriesData(content);
return await content.fetchData(client, { conferenceTitle, eventYear });
getQueriesData(content, conferenceSettings);
const getVarsFromSettings = content.selectSettings || (() => undefined);
return await content.fetchData(client, {
conferenceTitle,
eventYear,
...getVarsFromSettings(conferenceSettings),
});
} catch (err) {
console.error(err);
}
Expand Down
71 changes: 40 additions & 31 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
const { markdownToHtml } = require('./markdown');

const tagColors = {
'NodeJS': {
NodeJS: {
tagBG: '#7AB464',
color: '#fff',
},
'WebGL': {
WebGL: {
tagBG: '#ff7302',
color: '#ffffff'
color: '#ffffff',
},
'Ecosystem': {
Ecosystem: {
tagBG: '#fff40d',
color: '#ae4f01'
color: '#ae4f01',
},
'Language': {
Language: {
tagBG: '#fff40d',
color: '#ae4f01'
color: '#ae4f01',
},
'GraphQL': {
GraphQL: {
tagBG: '#f200fa',
color: '#400042',
},
'VueJS': {
VueJS: {
tagBG: '#4EBA87',
color: '#fff',
},
'Performance': {
Performance: {
tagBG: '#00ed24',
color: '#00410a',
},
'TypeScript': {
TypeScript: {
tagBG: '#61DAFB',
color: '#030303'
color: '#030303',
},
default: {
tagBG: 'black',
color: 'white'
}
color: 'white',
},
};

const getSocials = speaker => {
Expand All @@ -47,8 +47,14 @@ const getSocials = speaker => {
ownSite: 'site',
};
const { githubUrl, twitterUrl, mediumUrl, ownSite, companySite } = speaker;
const socials = Object.entries({ githubUrl, twitterUrl, mediumUrl, ownSite, companySite })
.map(([key, val]) => (val && { link: val, icon: ICONS[key] }))
const socials = Object.entries({
githubUrl,
twitterUrl,
mediumUrl,
ownSite,
companySite,
})
.map(([key, val]) => val && { link: val, icon: ICONS[key] })
.filter(Boolean);
return socials;
};
Expand All @@ -58,30 +64,33 @@ const getLabelColor = label => {
return colors;
};

const prepareSpeakers = speakers => speakers
.map(item => ({
...item.speaker,
...item,
avatar: item.speaker.avatar || {},
}))
.map(
async ({
bio,
speaker,
avatar,
...item
}) => ({
const prepareSpeakers = speakers =>
speakers
.map(item => ({
...item.speaker,
...item,
avatar: item.speaker.avatar || {},
}))
.map(async ({ bio, speaker, avatar, ...item }) => ({
...item,
company: `${item.company}, ${item.country}`,
avatar: avatar.url,
bio: await markdownToHtml(bio),
socials: getSocials(item),
...getLabelColor(item.label),
})
);
}));

const trySelectSettings = (selector, defaultSettings) => settings => {
try {
return selector(settings) || defaultSettings;
} catch {
return defaultSettings;
}
};

module.exports = {
getLabelColor,
prepareSpeakers,
tagColors,
trySelectSettings,
};

0 comments on commit 6d9439a

Please sign in to comment.