Refactor feed into a component.

This commit is contained in:
Vishnu KS
2020-08-23 00:28:09 +05:30
parent b4cde4c777
commit defa5cb196
6 changed files with 92 additions and 30 deletions

View File

@@ -2,18 +2,46 @@ const fs = require("fs")
const categories = JSON.parse(fs.readFileSync("src/data/categories.json"))
function createSlug (categoryName) {
const categoryNameLower = categoryName.toLowerCase();
return categoryNameLower.replace(" ", "-");
categoryName = categoryName.toLowerCase();
categoryName = categoryName.replace(/ /g, "-");
categoryName = categoryName.replace(/,/g, "");
return categoryName;
};
exports.createPages = ({ actions }) => {
exports.createPages = async function ({ actions, graphql }) {
const { createPage } = actions
categories.forEach(category => {
categories.forEach(async function(category) {
const data = await graphql(`query categoryBooksQuery($categoryName: String) {
allBooksJson(
filter: {
category: {
eq: $categoryName
}
}
){
edges {
node {
id
title
url
rating
author
year
category
}
}
}
}
`,
{categoryName: category.name})
createPage({
path: createSlug(category.name),
component: require.resolve("./src/templates/categoryTemplate.js"),
context: {
categoryName: category.name
categoryName: category.name,
data: data,
limit: null,
},
})
})