summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-search/src/reducers
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-plugin-search/src/reducers')
-rw-r--r--packages/gitbook-plugin-search/src/reducers/index.js3
-rw-r--r--packages/gitbook-plugin-search/src/reducers/search.js56
2 files changed, 59 insertions, 0 deletions
diff --git a/packages/gitbook-plugin-search/src/reducers/index.js b/packages/gitbook-plugin-search/src/reducers/index.js
new file mode 100644
index 0000000..bfce2bd
--- /dev/null
+++ b/packages/gitbook-plugin-search/src/reducers/index.js
@@ -0,0 +1,3 @@
+const GitBook = require('gitbook-core');
+
+module.exports = GitBook.createReducer('search', require('./search'));
diff --git a/packages/gitbook-plugin-search/src/reducers/search.js b/packages/gitbook-plugin-search/src/reducers/search.js
new file mode 100644
index 0000000..b960a77
--- /dev/null
+++ b/packages/gitbook-plugin-search/src/reducers/search.js
@@ -0,0 +1,56 @@
+const GitBook = require('gitbook-core');
+const { Record, List, OrderedMap } = GitBook.Immutable;
+
+const TYPES = require('../actions/types');
+
+const SearchState = Record({
+ // Is the search being processed
+ loading: Boolean(false),
+ // Current query
+ query: String(''),
+ // Current list of results
+ results: List(),
+ // Search handlers
+ handlers: OrderedMap()
+});
+
+module.exports = (state = SearchState(), action) => {
+ switch (action.type) {
+
+ case TYPES.CLEAR:
+ return state.merge({
+ loading: false,
+ query: '',
+ results: List()
+ });
+
+ case TYPES.START:
+ return state.merge({
+ loading: true,
+ query: action.query
+ });
+
+ case TYPES.END:
+ if (action.query !== state.query) {
+ return state;
+ }
+
+ return state.merge({
+ loading: false,
+ results: action.results
+ });
+
+ case TYPES.REGISTER_HANDLER:
+ return state.merge({
+ handlers: state.handlers.set(action.name, action.handler)
+ });
+
+ case TYPES.UNREGISTER_HANDLER:
+ return state.merge({
+ handlers: state.handlers.remove(action.name)
+ });
+
+ default:
+ return state;
+ }
+};