-
Notifications
You must be signed in to change notification settings - Fork 2
/
connectIModel.ts
108 lines (95 loc) · 4.03 KB
/
connectIModel.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module State
*/
import { connect } from "react-redux";
import { UiFramework } from "../UiFramework";
/** Private function that will map store's iModelConnection to the 'iModelConnection', 'iModel', and 'imodel' properties of props. This
* is not ideal but it is a result of not having standard prop name for an iModelConnection.
*/
const iModeMapStateToProps = (mapStateToProps: any) => {
return (state: any, ownProps: any) => {
const frameworkState = state[UiFramework.frameworkStateKey]; // since app sets up key, don't hard-code name
/* istanbul ignore next */
if (!frameworkState) {
if (mapStateToProps) return mapStateToProps(state, ownProps);
return {};
}
const storeProps = {
iModelConnection: frameworkState.sessionState.iModelConnection,
imodel: frameworkState.sessionState.iModelConnection,
iModel: frameworkState.sessionState.iModelConnection,
};
// istanbul ignore else
if (mapStateToProps) {
return {
...storeProps,
...mapStateToProps(state, ownProps),
};
}
// istanbul ignore next
return storeProps;
};
};
/** Function that will map store's iModelConnection and defaultViewState to the props 'iModelConnection', 'iModel', 'imodel',
* and 'viewState' property of props
*/
const iModelAndViewMapStateToProps = (mapStateToProps: any) => {
// istanbul ignore next
return (state: any, ownProps: any) => {
const frameworkState = state[UiFramework.frameworkStateKey]; // since app sets up key, don't hard-code name
/* istanbul ignore next */
if (!frameworkState) return {};
const props = {
...ownProps,
iModelConnection: frameworkState.sessionState.iModelConnection, // this prop is required by Unified Selection (TODO: standardize on use)
iModel: frameworkState.sessionState.iModelConnection, // this prop is needed by many components
imodel: frameworkState.sessionState.iModelConnection, // this prop is needed by ViewportProps
viewState: frameworkState.sessionState.defaultViewState,
};
if (mapStateToProps) return mapStateToProps(state, props);
return props;
};
};
/** Function that will connect a component to the IModelConnection data in the Redux store.
* @param mapStateToProps optional user function that, if defined, will be executed to provide additional properties from store.
* @param mapDispatchToProps data passed to Redux connect function.
* @public
*/
export const connectIModelConnection = (
mapStateToProps?: any,
mapDispatchToProps?: any
) => {
return connect(iModeMapStateToProps(mapStateToProps), mapDispatchToProps);
};
/** Function that will connect a component to the IModelConnection data in the Redux store
* @param mapStateToProps optional user function that, if defined, will be executed to provide additional properties from store.
* @param mapDispatchToProps data passed to Redux connect function.
* @beta
*
* @example
* // connect component for both getting and setting ImodelConnection data from Redux store.
* import { connectIModelConnection, sessionStateMapDispatchToProps, SessionStateActionsProps } from "@itwin/appui-react";
*
* export interface ComponentProps extends SessionStateActionsProps {
* myData: string;
* }
*
* export const ConnectControl = connectIModelConnection(null, sessionStateMapDispatchToProps)(ComponentClass);
*
* // this then allows connected control to update the store using a call like shown below.
* this.props.setNumItemsSelected(30);
*
*/
export const connectIModelConnectionAndViewState = (
mapStateToProps?: any,
mapDispatchToProps?: any
) => {
return connect(
iModelAndViewMapStateToProps(mapStateToProps),
mapDispatchToProps
);
};