1 | import { isEmpty } from 'lodash/lang';
|
2 |
|
3 | import merge from './merge';
|
4 | import { urlFromPage } from './pageUtils';
|
5 | import * as actions from './actions';
|
6 |
|
7 | const DEFAULT = {
|
8 | currentPageIndex: -1,
|
9 | history: [],
|
10 | currentPage: {},
|
11 | shell: false,
|
12 | };
|
13 |
|
14 | export default (state=DEFAULT, action={}) => {
|
15 | switch (action.type) {
|
16 | case actions.SET_STATUS: {
|
17 | const pageData = merge(state.currentPage, { status: action.payload.status });
|
18 | const history = [...state.history];
|
19 | history[state.currentPageIndex] = pageData;
|
20 |
|
21 | return { ...state, history, currentPage: pageData};
|
22 | }
|
23 |
|
24 | case actions.SET_PAGE: {
|
25 | const historyLen = state.history.length;
|
26 | const referrerFromHistory = !isEmpty(state.currentPage) && historyLen > 1
|
27 | ? urlFromPage(state.currentPage)
|
28 | : '';
|
29 |
|
30 | const {
|
31 | url,
|
32 | urlParams,
|
33 | queryParams,
|
34 | hashParams,
|
35 | referrer,
|
36 | status=200,
|
37 | } = action.payload;
|
38 |
|
39 | const routeReferrer = referrer ? referrer : referrerFromHistory;
|
40 |
|
41 | const relevantHistory = state.history.slice(0, state.currentPageIndex + 1);
|
42 | const pageData = {
|
43 | url,
|
44 | urlParams,
|
45 | queryParams,
|
46 | hashParams,
|
47 | status,
|
48 | referrer: routeReferrer,
|
49 | };
|
50 |
|
51 | return {
|
52 | ...state,
|
53 | currentPageIndex: state.currentPageIndex + 1,
|
54 | history: relevantHistory.concat([pageData]),
|
55 | currentPage: pageData,
|
56 | };
|
57 | }
|
58 |
|
59 | case actions.GOTO_PAGE_INDEX: {
|
60 | const { pageIndex } = action.payload;
|
61 |
|
62 | return {
|
63 | ...state,
|
64 | currentPageIndex: pageIndex,
|
65 | currentPage: state.history[pageIndex],
|
66 | };
|
67 | }
|
68 |
|
69 | case actions.SET_SHELL: {
|
70 | return merge(state, {
|
71 | shell: action.shell,
|
72 | });
|
73 | }
|
74 |
|
75 | default: return state;
|
76 | }
|
77 | };
|