summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-search/src/reducers/search.js
blob: ac496981587e17c53c5a7f731549f60b2a569704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const GitBook = require('gitbook-core');
const { Record, List, OrderedMap } = GitBook.Immutable;

const TYPES = require('../actions/types');

const Result = Record({
    url:   String(''),
    title: String(''),
    body:  String('')
});

const SearchState = Record({
    // Current query
    query:    String(''),
    // Current list of results
    results:  List(),
    // Search handlers
    handlers: OrderedMap()
});

module.exports = (state = SearchState(), action) => {
    switch (action.type) {

    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;
    }
};