Skip to content

Commit

Permalink
Fix bugs in adding page dependencies plus only add if new
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews committed Apr 19, 2017
1 parent 6d54f28 commit d9d60d2
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 42 deletions.
9 changes: 2 additions & 7 deletions packages/gatsby/lib/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,9 @@ exports.loadNodeContent = node => {
}

exports.getNodeAndSavePathDependency = (id, path) => {
const { addPageDependency } = require("./actions/add-page-dependency")
const node = getNode(id)
store.dispatch({
type: `ADD_PAGE_DEPENDENCY`,
payload: {
path,
nodeId: id,
},
})
addPageDependency({ path, nodeId: id })
return node
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,52 @@ describe(`add page data dependency`, () => {
},
})
})
it(`lets you add a node dependency to multiple paths`, () => {
const action = {
type: `ADD_PAGE_DEPENDENCY`,
payload: {
path: `/hi/`,
nodeId: `1.2.3`,
},
}
const action2 = {
type: `ADD_PAGE_DEPENDENCY`,
payload: {
path: `/hi2/`,
nodeId: `1.2.3`,
},
}
const action3 = {
type: `ADD_PAGE_DEPENDENCY`,
payload: {
path: `/blog/`,
nodeId: `1.2.3`,
},
}

let state = reducer(undefined, action)
state = reducer(state, action2)
state = reducer(state, action3)

expect(state).toEqual({
connections: {},
nodes: {
"1.2.3": [`/hi/`, `/hi2/`, `/blog/`],
},
})
})
it(`lets you add a connection dependency`, () => {
const action = {
type: `ADD_PAGE_DEPENDENCY`,
payload: {
path: `/hi/`,
connection: `MarkdownRemark`,
connection: `Markdown.Remark`,
},
}

expect(reducer(undefined, action)).toEqual({
connections: {
MarkdownRemark: ["/hi/"],
"Markdown.Remark": ["/hi/"],
},
nodes: {},
})
Expand Down
33 changes: 10 additions & 23 deletions packages/gatsby/lib/redux/reducers/page-data-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,22 @@ module.exports = (state = { nodes: {}, connections: {} }, action) => {

// If this nodeId not set yet.
if (action.payload.nodeId) {
if (!_.has(state, `nodes.${action.payload.nodeId}`)) {
state.nodes[action.payload.nodeId] = [action.payload.path]
} else {
if (
_.includes(state.nodes[action.payload.nodeId], action.payload.path)
) {
state.nodes[action.payload.nodeId] = state.nodes[
action.payload.nodeId
].concat([action.payload.path])
}
let existingPaths = []
if (state.nodes[action.payload.nodeId]) {
existingPaths = state.nodes[action.payload.nodeId]
}
const newPaths = _.uniq(existingPaths.concat(action.payload.path))
state.nodes[action.payload.nodeId] = newPaths
}

// If this connection not set yet.
if (action.payload.connection) {
if (!_.has(state, `connections.${action.payload.connection}`)) {
state.connections[action.payload.connection] = [action.payload.path]
} else {
if (
!_.includes(
state.connections[action.payload.connection],
action.payload.path
)
) {
state.connections[action.payload.connection] = state.connections[
action.payload.connection
].concat([action.payload.path])
}
let existingPaths = []
if (state.connections[action.payload.connection]) {
existingPaths = state.connections[action.payload.connection]
}
const newPaths = _.uniq(existingPaths.concat(action.payload.path))
state.connections[action.payload.connection] = newPaths
}

return state
Expand Down
3 changes: 1 addition & 2 deletions packages/gatsby/lib/schema/build-node-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ const {
getNodeAndSavePathDependency,
} = require("../redux")

const { boundActionCreators } = require("../redux/actions")
const { addPageDependency } = boundActionCreators
const { addPageDependency } = require("../redux/actions/add-page-dependency")

module.exports = async () =>
new Promise(resolve => {
Expand Down
4 changes: 1 addition & 3 deletions packages/gatsby/lib/schema/infer-graphql-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const parseFilepath = require("parse-filepath")
const mime = require("mime")
const isRelative = require("is-relative-url")
const { store, getNodes } = require("../redux")
const { boundActionCreators } = require("../redux/actions")
const { addPageDependency } = boundActionCreators
const { addPageDependency } = require("../redux/actions/add-page-dependency")

const inferGraphQLType = ({ value, fieldName, ...otherArgs }) => {
if (Array.isArray(value)) {
Expand Down Expand Up @@ -172,7 +171,6 @@ const inferObjectStructureFromNodes = (exports.inferObjectStructureFromNodes = (
n => n.type === linkedType && n.id === fieldValue
)
if (linkedNode) {
console.log(`ADD_PAGE_DEPENDENCY`, path, linkedNode.id)
addPageDependency({ path, nodeId: linkedNode.id })
return linkedNode
}
Expand Down
3 changes: 1 addition & 2 deletions packages/gatsby/lib/schema/run-sift.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const sift = require("sift")
const _ = require("lodash")
const { connectionFromArray } = require("graphql-skip-limit")
const { store } = require("../redux/")
const { boundActionCreators } = require("../redux/actions")
const { addPageDependency } = boundActionCreators
const { addPageDependency } = require("../redux/actions/add-page-dependency")

type Node = {
id: String,
Expand Down
9 changes: 7 additions & 2 deletions packages/gatsby/lib/utils/query-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ const q = queue(async ({ file, graphql, directory }, callback) => {
result.pathContext = pathInfo

// Add result to page object.
const page = store.getState().pages.find(p => p.path === pathInfo.path)
const oldPage = store.getState().pages.find(p => p.path === pathInfo.path)
const page = _.clone(oldPage)
let jsonName = `${_.kebabCase(pathInfo.path)}.json`
let internalComponentName = `Component${pascalCase(pathInfo.path)}`
if (jsonName === `.json`) {
Expand All @@ -387,7 +388,11 @@ const q = queue(async ({ file, graphql, directory }, callback) => {
}
page.jsonName = jsonName
page.internalComponentName = internalComponentName
boundActionCreators.upsertPage(page)

// If the page has changed, send it to the store.
if (!_.isEqual(oldPage, page)) {
boundActionCreators.upsertPage(page)
}

// Save result to file.
const resultJSON = JSON.stringify(clonedResult, null, 4)
Expand Down
2 changes: 1 addition & 1 deletion www/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const IndexRoute = React.createClass({
render() {
return (
<div>
<h1>Gatsby website 0.0.0.1</h1>
<h1>Gatsby website 0.0.0.2</h1>
<Link to="/docs/">
Docs
</Link>
Expand Down

0 comments on commit d9d60d2

Please sign in to comment.