summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/actions/navigation.js
blob: 4afd4fbd9d22f43bc67c1d3f7092df5233d8b538 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const { createBrowserHistory, createMemoryHistory } = require('history');

const ACTION_TYPES = require('./TYPES');
const getPayload = require('../lib/getPayload');
const Location = require('../models/Location');

const isServerSide = (typeof window === 'undefined');

const SUPPORTED = (
    typeof window !== 'undefined' &&
    window.history && window.history.pushState && window.history.replaceState &&
    // pushState isn't reliable on iOS until 5.
    !navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]\D|WebApps\/.+CFNetwork)/)
);

// Create tge history instance
const history = isServerSide ? createMemoryHistory() : createBrowserHistory();

/**
 * Push a new url into the navigation
 * @param {String|Location} location
 * @return {Action} action
 */
function pushURI(location) {
    return () => {
        location = Location.fromNative(location);

        if (SUPPORTED) {
            history.push(location.toNative());
        } else {
            redirect(location.toString());
        }
    };
}

/**
 * Replace current state in navigation
 * @param {String|Location} location
 * @return {Action} action
 */
function replaceURI(location) {
    return () => {
        location = Location.fromNative(location);

        if (SUPPORTED) {
            history.replace(location.toNative());
        } else {
            redirect(location.toString());
        }
    };
}

/**
 * Hard redirection
 * @param {String} uri
 * @return {Action} action
 */
function redirect(uri) {
    return () => {
        window.location.href = uri;
    };
}

/**
 * Listen to url change
 * @param {Function} fn
 * @return {Action} action
 */
function listen(fn) {
    return (dispatch, getState) => {
        history.listen(location => {
            location = Location.fromNative(location);
            fn(location, dispatch, getState);
        });
    };
}

/**
 * Fetch a new page and update the store accordingly
 * @param {String} uri
 * @param {Boolean} options.replace
 * @return {Action} action
 */
function fetchPage(uri, options = {}) {
    const { replace } = options;

    return (dispatch, getState) => {
        const prevURI = location.href;
        dispatch({ type: ACTION_TYPES.PAGE_FETCH_START });

        if (replace) {
            dispatch(replaceURI(uri));
        } else {
            dispatch(pushURI(uri));
        }

        window.fetch(uri, {
            credentials: 'include'
        })
        .then(
            response => {
                return response.text();
            }
        )
        .then(
            html => {
                const payload = getPayload(html);

                if (!payload) {
                    throw new Error('No payload found in page');
                }

                dispatch({ type: ACTION_TYPES.PAGE_FETCH_END, payload });
            }
        )
        .catch(
            error => {
                dispatch(replaceURI(prevURI));
                dispatch(redirect(uri));

                dispatch({ type: ACTION_TYPES.PAGE_FETCH_ERROR, error });
            }
        );
    };
}

/**
 * Fetch a new article
 * @param {SummaryArticle} article
 * @return {Action} action
 */
function fetchArticle(article) {
    return fetchPage(article.path);
}

/**
 * Update anchor for current page
 * @param {String} hash
 * @return {Action} action
 */
function updateAnchor(hash) {
    return pushURI({ hash });
}

/**
 * Update query for current page
 * @param {Object|Map} query
 * @return {Action} action
 */
function updateQuery(query) {
    return pushURI({ query });
}

module.exports = {
    listen,
    pushURI,
    fetchPage,
    fetchArticle,
    updateAnchor,
    updateQuery
};