summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/reducers
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-core/src/reducers')
-rw-r--r--packages/gitbook-core/src/reducers/components.js20
-rw-r--r--packages/gitbook-core/src/reducers/config.js15
-rw-r--r--packages/gitbook-core/src/reducers/file.js16
-rw-r--r--packages/gitbook-core/src/reducers/history.js82
-rw-r--r--packages/gitbook-core/src/reducers/i18n.js27
-rw-r--r--packages/gitbook-core/src/reducers/index.js15
-rw-r--r--packages/gitbook-core/src/reducers/languages.js12
-rw-r--r--packages/gitbook-core/src/reducers/page.js16
-rw-r--r--packages/gitbook-core/src/reducers/readme.js5
-rw-r--r--packages/gitbook-core/src/reducers/summary.js28
10 files changed, 236 insertions, 0 deletions
diff --git a/packages/gitbook-core/src/reducers/components.js b/packages/gitbook-core/src/reducers/components.js
new file mode 100644
index 0000000..948a3ac
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/components.js
@@ -0,0 +1,20 @@
+const { List } = require('immutable');
+const ACTION_TYPES = require('../actions/TYPES');
+
+function reduceComponents(state, action) {
+ state = state || List();
+ switch (action.type) {
+
+ case ACTION_TYPES.REGISTER_COMPONENT:
+ return state.push({
+ Component: action.Component,
+ descriptor: action.descriptor
+ });
+
+ default:
+ return state;
+
+ }
+}
+
+module.exports = reduceComponents;
diff --git a/packages/gitbook-core/src/reducers/config.js b/packages/gitbook-core/src/reducers/config.js
new file mode 100644
index 0000000..a49c602
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/config.js
@@ -0,0 +1,15 @@
+const { fromJS } = require('immutable');
+const ACTION_TYPES = require('../actions/TYPES');
+
+module.exports = (state, action) => {
+ state = fromJS(state);
+ switch (action.type) {
+
+ case ACTION_TYPES.PAGE_FETCH_END:
+ return fromJS(action.payload.config);
+
+ default:
+ return state;
+
+ }
+};
diff --git a/packages/gitbook-core/src/reducers/file.js b/packages/gitbook-core/src/reducers/file.js
new file mode 100644
index 0000000..82b0f42
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/file.js
@@ -0,0 +1,16 @@
+const ACTION_TYPES = require('../actions/TYPES');
+const File = require('../models/File');
+
+module.exports = (state, action) => {
+ state = File.create(state);
+
+ switch (action.type) {
+
+ case ACTION_TYPES.PAGE_FETCH_END:
+ return state.merge(action.payload.file);
+
+ default:
+ return state;
+
+ }
+};
diff --git a/packages/gitbook-core/src/reducers/history.js b/packages/gitbook-core/src/reducers/history.js
new file mode 100644
index 0000000..be8fe42
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/history.js
@@ -0,0 +1,82 @@
+const { Record, List } = require('immutable');
+const { createBrowserHistory, createMemoryHistory } = require('history');
+const ACTION_TYPES = require('../actions/TYPES');
+const Location = require('../models/Location');
+
+const isServerSide = (typeof window === 'undefined');
+
+const HistoryState = Record({
+ // Current location
+ location: new Location(),
+ // Are we loading a new page
+ loading: Boolean(false),
+ // Did we fail loading a page?
+ error: null,
+ // Listener for history changes
+ listeners: List(),
+ // Function to call to stop listening
+ unlisten: null,
+ // HistoryJS instance
+ client: null
+});
+
+function reduceHistory(state, action) {
+ state = state || HistoryState();
+ switch (action.type) {
+
+ case ACTION_TYPES.PAGE_FETCH_START:
+ return state.merge({
+ loading: true
+ });
+
+ case ACTION_TYPES.PAGE_FETCH_END:
+ return state.merge({
+ loading: false
+ });
+
+ case ACTION_TYPES.PAGE_FETCH_ERROR:
+ return state.merge({
+ loading: false,
+ error: action.error
+ });
+
+ case ACTION_TYPES.HISTORY_ACTIVATE:
+ const client = isServerSide ? createMemoryHistory() : createBrowserHistory();
+ const unlisten = client.listen(action.listener);
+
+ // We can't use .merge since it convert history to an immutable
+ const newState = state
+ // TODO: we should find a way to have the correct location on server side
+ .set('location', isServerSide ? new Location() : Location.fromNative(window.location))
+ .set('client', client)
+ .set('unlisten', unlisten);
+
+ return newState;
+
+ case ACTION_TYPES.HISTORY_DEACTIVATE:
+ if (state.unlisten) {
+ state.unlisten();
+ }
+
+ return state.merge({
+ client: null,
+ unlisten: null
+ });
+
+ case ACTION_TYPES.HISTORY_UPDATE:
+ return state.merge({
+ location: action.location
+ });
+
+ case ACTION_TYPES.HISTORY_LISTEN:
+ return state.merge({
+ listeners: state.listeners.push(action.listener)
+ });
+
+ default:
+ return state;
+
+ }
+}
+
+module.exports = reduceHistory;
diff --git a/packages/gitbook-core/src/reducers/i18n.js b/packages/gitbook-core/src/reducers/i18n.js
new file mode 100644
index 0000000..4ffd129
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/i18n.js
@@ -0,0 +1,27 @@
+const { Record, Map } = require('immutable');
+const ACTION_TYPES = require('../actions/TYPES');
+
+const I18nState = Record({
+ locale: 'en',
+ // Map of locale -> Map<String:String>
+ messages: Map()
+});
+
+function reduceI18n(state, action) {
+ state = state || I18nState();
+ switch (action.type) {
+
+ case ACTION_TYPES.I18N_REGISTER_LOCALE:
+ return state.merge({
+ messages: state.messages.set(action.locale,
+ state.messages.get(action.locale, Map()).merge(action.messages)
+ )
+ });
+
+ default:
+ return state;
+
+ }
+}
+
+module.exports = reduceI18n;
diff --git a/packages/gitbook-core/src/reducers/index.js b/packages/gitbook-core/src/reducers/index.js
new file mode 100644
index 0000000..a211d3b
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/index.js
@@ -0,0 +1,15 @@
+const composeReducer = require('../lib/composeReducer');
+const createReducer = require('../lib/createReducer');
+
+module.exports = composeReducer(
+ createReducer('components', require('./components')),
+ createReducer('history', require('./history')),
+ createReducer('i18n', require('./i18n')),
+ // GitBook JSON
+ createReducer('config', require('./config')),
+ createReducer('file', require('./file')),
+ createReducer('page', require('./page')),
+ createReducer('summary', require('./summary')),
+ createReducer('readme', require('./readme')),
+ createReducer('languages', require('./languages'))
+);
diff --git a/packages/gitbook-core/src/reducers/languages.js b/packages/gitbook-core/src/reducers/languages.js
new file mode 100644
index 0000000..0ec2ae4
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/languages.js
@@ -0,0 +1,12 @@
+const Languages = require('../models/Languages');
+
+module.exports = (state, action) => {
+ state = Languages.create(state);
+
+ switch (action.type) {
+
+ default:
+ return state;
+
+ }
+};
diff --git a/packages/gitbook-core/src/reducers/page.js b/packages/gitbook-core/src/reducers/page.js
new file mode 100644
index 0000000..9b94d1e
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/page.js
@@ -0,0 +1,16 @@
+const ACTION_TYPES = require('../actions/TYPES');
+const Page = require('../models/Page');
+
+module.exports = (state, action) => {
+ state = Page.create(state);
+
+ switch (action.type) {
+
+ case ACTION_TYPES.PAGE_FETCH_END:
+ return state.merge(action.payload.page);
+
+ default:
+ return state;
+
+ }
+};
diff --git a/packages/gitbook-core/src/reducers/readme.js b/packages/gitbook-core/src/reducers/readme.js
new file mode 100644
index 0000000..9e8656a
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/readme.js
@@ -0,0 +1,5 @@
+const Readme = require('../models/Readme');
+
+module.exports = (state, action) => {
+ return Readme.create(state);
+};
diff --git a/packages/gitbook-core/src/reducers/summary.js b/packages/gitbook-core/src/reducers/summary.js
new file mode 100644
index 0000000..60568ef
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/summary.js
@@ -0,0 +1,28 @@
+const { Record, List } = require('immutable');
+
+const File = require('../models/File');
+const SummaryPart = require('../models/SummaryPart');
+
+
+class SummaryState extends Record({
+ file: new File(),
+ parts: List()
+}) {
+ constructor(state = {}) {
+ super({
+ ...state,
+ file: new File(state.file),
+ parts: (new List(state.parts))
+ .map(article => new SummaryPart(article))
+ });
+ }
+
+ static create(state) {
+ return state instanceof SummaryState ?
+ state : new SummaryState(state);
+ }
+}
+
+module.exports = (state, action) => {
+ return SummaryState.create(state);
+};