Przeglądaj źródła

Add Dialog utilities

In order to accommodate the requirements of the work on supporting XMPP
authentication on mobile/react-native, make dealing with Dialog a
little more generic and a little easier.
j8
Lyubo Marinov 8 lat temu
rodzic
commit
9c47a7e972

+ 23
- 10
react/features/base/dialog/actions.js Wyświetl plik

@@ -1,15 +1,24 @@
1
+/* @flow */
2
+
1 3
 import { HIDE_DIALOG, OPEN_DIALOG } from './actionTypes';
4
+import { isDialogOpen } from './functions';
2 5
 
3 6
 /**
4 7
  * Signals Dialog to close its dialog.
5 8
  *
9
+ * @param {Object} [component] - The <tt>Dialog</tt> component to close/hide. If
10
+ * <tt>undefined</tt>, closes/hides <tt>Dialog</tt> regardless of which
11
+ * component it's rendering; otherwise, closes/hides <tt>Dialog</tt> only if
12
+ * it's rendering the specified <tt>component</tt>.
6 13
  * @returns {{
7
- *     type: HIDE_DIALOG
14
+ *     type: HIDE_DIALOG,
15
+ *     component: (React.Component | undefined)
8 16
  * }}
9 17
  */
10
-export function hideDialog() {
18
+export function hideDialog(component: ?Object) {
11 19
     return {
12
-        type: HIDE_DIALOG
20
+        type: HIDE_DIALOG,
21
+        component
13 22
     };
14 23
 }
15 24
 
@@ -19,9 +28,13 @@ export function hideDialog() {
19 28
  * @param {Object} component - The component to display as dialog.
20 29
  * @param {Object} [componentProps] - The React <tt>Component</tt> props of the
21 30
  * specified <tt>component</tt>.
22
- * @returns {Object}
31
+ * @returns {{
32
+ *     type: OPEN_DIALOG,
33
+ *     component: React.Component,
34
+ *     componentProps: (Object | undefined)
35
+ * }}
23 36
  */
24
-export function openDialog(component, componentProps) {
37
+export function openDialog(component: Object, componentProps: ?Object) {
25 38
     return {
26 39
         type: OPEN_DIALOG,
27 40
         component,
@@ -37,12 +50,12 @@ export function openDialog(component, componentProps) {
37 50
  * @param {Object} component - The component to display as dialog.
38 51
  * @param {Object} [componentProps] - The React <tt>Component</tt> props of the
39 52
  * specified <tt>component</tt>.
40
- * @returns {Object}
53
+ * @returns {Function}
41 54
  */
42
-export function toggleDialog(component, componentProps) {
43
-    return (dispatch, getState) => {
44
-        if (getState()['features/base/dialog'].component === component) {
45
-            dispatch(hideDialog());
55
+export function toggleDialog(component: Object, componentProps: ?Object) {
56
+    return (dispatch: Dispatch, getState: Function) => {
57
+        if (isDialogOpen(getState, component)) {
58
+            dispatch(hideDialog(component));
46 59
         } else {
47 60
             dispatch(openDialog(component, componentProps));
48 61
         }

+ 17
- 0
react/features/base/dialog/functions.js Wyświetl plik

@@ -0,0 +1,17 @@
1
+/* @flow */
2
+
3
+import { toState } from '../redux';
4
+
5
+/**
6
+ * Checks if a <tt>Dialog</tt> with a specific <tt>component</tt> is currently
7
+ * open.
8
+ *
9
+ * @param {Function|Object} stateful - The redux store, the redux
10
+ * <tt>getState</tt> function, or the redux state itself.
11
+ * @param {React.Component} component - The <tt>component</tt> of a
12
+ * <tt>Dialog</tt> to be checked.
13
+ * @returns {boolean}
14
+ */
15
+export function isDialogOpen(stateful: Function | Object, component: Object) {
16
+    return toState(stateful)['features/base/dialog'].component === component;
17
+}

+ 1
- 0
react/features/base/dialog/index.js Wyświetl plik

@@ -1,5 +1,6 @@
1 1
 export * from './actions';
2 2
 export * from './actionTypes';
3 3
 export * from './components';
4
+export * from './functions';
4 5
 
5 6
 import './reducer';

+ 13
- 5
react/features/base/dialog/reducer.js Wyświetl plik

@@ -1,3 +1,5 @@
1
+/* @flow */
2
+
1 3
 import { assign, ReducerRegistry } from '../redux';
2 4
 
3 5
 import { HIDE_DIALOG, OPEN_DIALOG } from './actionTypes';
@@ -13,11 +15,17 @@ import { HIDE_DIALOG, OPEN_DIALOG } from './actionTypes';
13 15
  */
14 16
 ReducerRegistry.register('features/base/dialog', (state = {}, action) => {
15 17
     switch (action.type) {
16
-    case HIDE_DIALOG:
17
-        return assign(state, {
18
-            component: undefined,
19
-            componentProps: undefined
20
-        });
18
+    case HIDE_DIALOG: {
19
+        const { component } = action;
20
+
21
+        if (typeof component === 'undefined' || state.component === component) {
22
+            return assign(state, {
23
+                component: undefined,
24
+                componentProps: undefined
25
+            });
26
+        }
27
+        break;
28
+    }
21 29
 
22 30
     case OPEN_DIALOG:
23 31
         return assign(state, {

+ 21
- 11
react/features/base/redux/functions.js Wyświetl plik

@@ -112,18 +112,28 @@ function _set(
112 112
 /* eslint-enable max-params */
113 113
 
114 114
 /**
115
- * If the specified <tt>stateOrGetState</tt> is a function, it is presumed to be
116
- * the redux {@link getState} function, it is invoked, and its return value is
117
- * returned; otherwise, <tt>stateOrGetState</tt> is presumed to be the redux
118
- * state and is returned.
115
+ * Returns redux state from the specified <tt>stateful</tt> which is presumed to
116
+ * be related to the redux state (e.g. the redux store, the redux
117
+ * <tt>getState</tt> function).
119 118
  *
120
- * @param {Object|Function} stateOrGetState - The redux state or
121
- * {@link getState} function.
119
+ * @param {Function|Object} stateful - The entity such as the redux store or the
120
+ * redux <tt>getState</tt> function from which the redux state is to be
121
+ * returned.
122 122
  * @returns {Object} The redux state.
123 123
  */
124
-export function toState(stateOrGetState: Object | Function) {
125
-    return (
126
-        typeof stateOrGetState === 'function'
127
-            ? stateOrGetState()
128
-            : stateOrGetState);
124
+export function toState(stateful: Function | Object) {
125
+    if (stateful) {
126
+        if (typeof stateful === 'function') {
127
+            return stateful();
128
+        }
129
+
130
+        const { getState } = stateful;
131
+
132
+        if (typeof getState === 'function'
133
+                && typeof stateful.dispatch === 'function') {
134
+            return getState();
135
+        }
136
+    }
137
+
138
+    return stateful;
129 139
 }

Ładowanie…
Anuluj
Zapisz