1
import { ThunkAction } from 'redux-thunk';
2

3 1
import * as admin from '../../api/admin';
4 1
import { createAction } from '../../utils/actions';
5 1
import { MAX_EXPLORER_PAGES } from '../../config/wagtailConfig';
6

7
import { State, Action } from './reducers';
8

9
type ThunkActionType = ThunkAction<void, State, unknown, Action>;
10

11 1
const getPageSuccess = createAction('GET_PAGE_SUCCESS', (id: number, data: admin.WagtailPageAPI) => ({ id, data }));
12 1
const getPageFailure = createAction('GET_PAGE_FAILURE', (id: number, error: Error) => ({ id, error }));
13

14
/**
15
 * Gets a page from the API.
16
 */
17 1
function getPage(id: number): ThunkActionType {
18 1
  return (dispatch) => admin.getPage(id).then((data) => {
19 0
    dispatch(getPageSuccess(id, data));
20 1
  }, (error) => {
21 1
    dispatch(getPageFailure(id, error));
22
  });
23
}
24

25 1
const getChildrenStart = createAction('GET_CHILDREN_START', (id: number) => ({ id }));
26 1
const getChildrenSuccess = createAction(
27
  'GET_CHILDREN_SUCCESS',
28 0
  (id, items: admin.WagtailPageAPI[], meta: any) => ({ id, items, meta })
29
);
30 1
const getChildrenFailure = createAction('GET_CHILDREN_FAILURE', (id: number, error: Error) => ({ id, error }));
31

32
/**
33
 * Gets the children of a node from the API.
34
 */
35 1
function getChildren(id: number, offset = 0): ThunkActionType {
36 1
  return (dispatch) => {
37 1
    dispatch(getChildrenStart(id));
38

39 1
    return admin.getPageChildren(id, {
40
      offset: offset,
41 0
    }).then(({ items, meta }) => {
42 0
      const nbPages = offset + items.length;
43 0
      dispatch(getChildrenSuccess(id, items, meta));
44

45
      // Load more pages if necessary. Only one request is created even though
46
      // more might be needed, thus naturally throttling the loading.
47 1
      if (nbPages < meta.total_count && nbPages < MAX_EXPLORER_PAGES) {
48 0
        dispatch(getChildren(id, nbPages));
49
      }
50 1
    }, (error) => {
51 1
      dispatch(getChildrenFailure(id, error));
52
    });
53
  };
54
}
55

56 1
const getTranslationsStart = createAction('GET_TRANSLATIONS_START', id => ({ id }));
57 1
const getTranslationsSuccess = createAction('GET_TRANSLATIONS_SUCCESS', (id, items) => ({ id, items }));
58 1
const getTranslationsFailure = createAction('GET_TRANSLATIONS_FAILURE', (id, error) => ({ id, error }));
59

60
/**
61
 * Gets the translations of a node from the API.
62
 */
63 1
function getTranslations(id) {
64 1
  return (dispatch) => {
65 1
    dispatch(getTranslationsStart(id));
66

67 1
    return admin.getAllPageTranslations(id, { onlyWithChildren: true }).then(items => {
68 0
      dispatch(getTranslationsSuccess(id, items));
69 1
    }, (error) => {
70 1
      dispatch(getTranslationsFailure(id, error));
71
    });
72
  };
73
}
74

75 1
const openExplorer = createAction('OPEN_EXPLORER', id => ({ id }));
76 1
export const closeExplorer = createAction('CLOSE_EXPLORER');
77

78 1
export function toggleExplorer(id: number): ThunkActionType {
79 1
  return (dispatch, getState) => {
80 1
    const { explorer, nodes } = getState();
81

82 1
    if (explorer.isVisible) {
83 1
      dispatch(closeExplorer());
84
    } else {
85 1
      const page = nodes[id];
86

87 1
      dispatch(openExplorer(id));
88

89 1
      if (!page) {
90 1
        dispatch(getChildren(id));
91

92 1
        if (id !== 1) {
93 1
          dispatch(getTranslations(id));
94
        }
95
      }
96

97
      // We need to get the title of the starting page, only if it is not the site's root.
98 1
      const isNotRoot = id !== 1;
99 1
      if (isNotRoot) {
100 1
        dispatch(getPage(id));
101
      }
102
    }
103
  };
104
}
105

106 1
const gotoPagePrivate = createAction('GOTO_PAGE', (id: number, transition: number) => ({ id, transition }));
107

108 1
export function gotoPage(id: number, transition: number): ThunkActionType {
109 1
  return (dispatch, getState) => {
110 1
    const { nodes } = getState();
111 1
    const page = nodes[id];
112

113 1
    dispatch(gotoPagePrivate(id, transition));
114

115 1
    if (page && !page.isFetchingChildren  && !(page.children.count > 0)) {
116 1
      dispatch(getChildren(id));
117
    }
118

119 1
    if (page && !page.isFetchingTranslations && page.translations == null) {
120 1
      dispatch(getTranslations(id));
121
    }
122
  };
123
}

Read our documentation on viewing source code .

Loading