Skip to content

Commit

Permalink
chore(gatsby): migrate pages reducer to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
hiwelo committed May 5, 2020
1 parent a694d43 commit 5d2bb27
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@types/lodash": "^4.14.149",
"@types/node": "^12.12.30",
"@types/node-fetch": "^2.5.5",
"@types/normalize-path": "^3.0.0",
"@types/react": "^16.9.31",
"@types/semver": "^7.1.0",
"@types/signal-exit": "^3.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/redux/__tests__/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock(`fs-extra`, () => {
})
const glob = require(`glob`)

const reducer = require(`../reducers/pages`)
const { pagesReducer: reducer } = require(`../reducers/pages`)
const { actions } = require(`../actions`)
const { readFile } = require(`fs-extra`)

Expand Down Expand Up @@ -174,7 +174,7 @@ describe(`Add pages`, () => {
path: `/hi/`,
component: `/whatever/index.js`,
},
{ name: `test` }
{ id: `test`, name: `test` }
)
const action2 = actions.deletePage({ path: `/hi/` })

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/plugin-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface ICreatePageAction {
id: string
}
updatedAt: number
pluginCreator__NODE: string
pluginCreator___NODE: string
pluginCreatorId: string
componentPath: string
}
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/src/redux/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const reduxNodes = require(`./nodes`)
const lokiNodes = require(`../../db/loki/nodes`).reducer
import { pagesReducer } from "./pages"
import { redirectsReducer } from "./redirects"
import { schemaReducer } from "./schema"
import { staticQueryComponentsReducer } from "./static-query-components"
Expand Down Expand Up @@ -59,8 +60,8 @@ module.exports = {
lastAction: require(`./last-action`),
flattenedPlugins: require(`./flattened-plugins`),
config: require(`./config`),
pages: require(`./pages`),
schema: schemaReducer,
pages: pagesReducer,
status: statusReducer,
componentDataDependencies: require(`./component-data-dependencies`),
components: require(`./components`),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
const normalize = require(`normalize-path`)
import normalize from "normalize-path"
import { ActionsUnion, IGatsbyPage, IGatsbyState } from "../types"

module.exports = (state = new Map(), action) => {
export const pagesReducer = (
state: IGatsbyState["pages"] = new Map<string, IGatsbyPage>(),
action: ActionsUnion
): IGatsbyState["pages"] => {
switch (action.type) {
case `DELETE_CACHE`:
return new Map()

case `CREATE_PAGE`: {
action.payload.component = normalize(action.payload.component)
if (!action.plugin && !action.plugin.name) {

// throws an error if the page is not created by a plugin
if (!action.plugin?.name) {
console.log(``)
console.error(JSON.stringify(action, null, 4))
console.log(``)

throw new Error(
`Pages can only be created by plugins. There wasn't a plugin set
when creating this page.`
`Pages can only be created by plugins. There wasn't a plugin set when creating this page.`
)
}

// Link page to its plugin.
action.payload.pluginCreator___NODE = action.plugin.id
action.payload.pluginCreatorId = action.plugin.id
action.payload.pluginCreator___NODE = action.plugin.id ?? ``
action.payload.pluginCreatorId = action.plugin.id ?? ``

// Add page to the state with the path as key
state.set(action.payload.path, action.payload)

return state
}

case `DELETE_PAGE`: {
state.delete(action.payload.path)

return state
}

default:
return state
}
Expand Down
16 changes: 15 additions & 1 deletion packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface IGatsbyPage {
isCreatedByStatefulCreatePages: boolean
context: {}
updatedAt: number
pluginCreator__NODE: Identifier
pluginCreator___NODE: Identifier
pluginCreatorId: Identifier
componentPath: SystemPath
}
Expand Down Expand Up @@ -76,6 +76,7 @@ export interface IGatsbyNode {
}

export interface IGatsbyPlugin {
id: Identifier
name: string
version: string
}
Expand Down Expand Up @@ -224,10 +225,12 @@ export interface ICachedReduxState {
export type ActionsUnion =
| IAddThirdPartySchema
| ICreateFieldExtension
| ICreatePageAction
| ICreatePageDependencyAction
| ICreateTypes
| IDeleteCacheAction
| IDeleteComponentDependenciesAction
| IDeletePageAction
| IPageQueryRunAction
| IPrintTypeDefinitions
| IQueryExtractedAction
Expand Down Expand Up @@ -381,6 +384,12 @@ export interface ICreateResolverContext {
| { [camelCasedPluginNameWithoutPrefix: string]: IGatsbyPluginContext }
}

export interface ICreatePageAction {
type: `CREATE_PAGE`
payload: IGatsbyPage
plugin?: IGatsbyPlugin
}

export interface ICreateRedirectAction {
type: `CREATE_REDIRECT`
payload: IRedirect
Expand Down Expand Up @@ -410,6 +419,11 @@ export interface ISetPageDataAction {
}
}

export interface IDeletePageAction {
type: `DELETE_PAGE`
payload: IGatsbyPage
}

export interface IReplaceStaticQueryAction {
type: `REPLACE_STATIC_QUERY`
payload: IGatsbyStaticQueryComponents
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4145,6 +4145,11 @@
version "2.4.0"
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"

"@types/normalize-path@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/normalize-path/-/normalize-path-3.0.0.tgz#bb5c46cab77b93350b4cf8d7ff1153f47189ae31"
integrity sha512-Nd8y/5t/7CRakPYiyPzr/IAfYusy1FkcZYFEAcoMZkwpJv2n4Wm+olW+e7xBdHEXhOnWdG9ddbar0gqZWS4x5Q==

"@types/opn@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@types/opn/-/opn-5.1.0.tgz#bff7bc371677f4bdbb37884400e03fd81f743927"
Expand Down

0 comments on commit 5d2bb27

Please sign in to comment.