forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redux_decorator.tsx
63 lines (52 loc) · 1.81 KB
/
redux_decorator.tsx
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import { createStore } from 'redux';
import { Provider as ReduxProvider } from 'react-redux';
import { cloneDeep } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { CanvasWorkpad, CanvasElement, CanvasAsset, CanvasPage } from '../../types';
// @ts-expect-error untyped local
import { elementsRegistry } from '../../public/lib/elements_registry';
import { image } from '../../canvas_plugin_src/elements/image';
elementsRegistry.register(image);
import { getInitialState, getReducer, getMiddleware, patchDispatch } from '../addon/state';
export { ADDON_ID, ACTIONS_PANEL_ID } from '../addon/constants';
export interface Params {
workpad?: CanvasWorkpad;
pages?: CanvasPage[];
elements?: CanvasElement[];
assets?: CanvasAsset[];
}
export const reduxDecorator = (params: Params = {}) => {
const state = cloneDeep(getInitialState());
const { workpad, elements, assets, pages } = params;
if (workpad) {
set(state, 'persistent.workpad', workpad);
}
if (pages) {
set(state, 'persistent.workpad.pages', pages);
}
if (elements) {
set(state, 'persistent.workpad.pages.0.elements', elements);
}
if (assets) {
set(
state,
'assets',
assets.reduce((obj: Record<string, CanvasAsset>, item) => {
obj[item.id] = item;
return obj;
}, {})
);
}
return (story: Function) => {
const store = createStore(getReducer(), state, getMiddleware());
store.dispatch = patchDispatch(store, store.dispatch);
return <ReduxProvider store={store}>{story()}</ReduxProvider>;
};
};