소스 검색

Calendar feature disabled state getter

This commit adds a state getter that considers checking the enabled/disabled state of the calendar feature, so then other features don’t have to do it manually.
master
Bettenbuk Zoltan 6 년 전
부모
커밋
ac63a0fa73

+ 11
- 0
react/features/calendar-sync/constants.js 파일 보기

@@ -10,6 +10,17 @@ import { NativeModules } from 'react-native';
10 10
  */
11 11
 export const CALENDAR_ENABLED = _isCalendarEnabled();
12 12
 
13
+/**
14
+ * The default state of the calendar.
15
+ *
16
+ * NOTE: This is defined here, to be reusable by functions.js as well (see file
17
+ * for details).
18
+ */
19
+export const DEFAULT_STATE = {
20
+    authorization: undefined,
21
+    events: []
22
+};
23
+
13 24
 /**
14 25
  * Determines whether the calendar feature is enabled by the app. For
15 26
  * example, Apple through its App Store requires

+ 18
- 0
react/features/calendar-sync/functions.js 파일 보기

@@ -0,0 +1,18 @@
1
+// @flow
2
+import { toState } from '../base/redux';
3
+
4
+import { CALENDAR_ENABLED, DEFAULT_STATE } from './constants';
5
+
6
+/**
7
+ * Returns the calendar state, considering the enabled/disabled state of the
8
+ * feature. Since that is the normal Redux behaviour, this function will always
9
+ * return an object (the default state if the feature is disabled).
10
+ *
11
+ * @param {Object | Function} stateful - An object or a function that can be
12
+ * resolved to a Redux state by {@code toState}.
13
+ * @returns {Object}
14
+ */
15
+export function getCalendarState(stateful: Object | Function) {
16
+    return CALENDAR_ENABLED
17
+        ? toState(stateful)['features/calendar-sync'] : DEFAULT_STATE;
18
+}

+ 1
- 0
react/features/calendar-sync/index.js 파일 보기

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

+ 21
- 14
react/features/calendar-sync/reducer.js 파일 보기

@@ -8,22 +8,29 @@ import {
8 8
     SET_CALENDAR_AUTHORIZATION,
9 9
     SET_CALENDAR_EVENTS
10 10
 } from './actionTypes';
11
-import { CALENDAR_ENABLED } from './constants';
12
-
13
-const DEFAULT_STATE = {
14
-    /**
15
-     * Note: If features/calendar-sync ever gets persisted, do not persist the
16
-     * authorization value as it's needed to remain a runtime value to see if we
17
-     * need to re-request the calendar permission from the user.
18
-     */
19
-    authorization: undefined,
20
-    events: []
21
-};
22
-
11
+import { CALENDAR_ENABLED, DEFAULT_STATE } from './constants';
12
+
13
+/**
14
+ * Constant for the Redux subtree of the calendar feature.
15
+ *
16
+ * NOTE: Please do not access this subtree directly outside of this feature.
17
+ * This feature can be disabled (see {@code constants.js} for details), and in
18
+ * that case, accessing this subtree directly will return undefined and will
19
+ * need a bunch of repetitive type checks in other features. Use the
20
+ * {@code getCalendarState} function instead, or make sure you take care of
21
+ * those checks, or consider using the {@code CALENDAR_ENABLED} const to gate
22
+ * features if needed.
23
+ */
23 24
 const STORE_NAME = 'features/calendar-sync';
24 25
 
25
-// XXX For legacy purposes, read any {@code knownDomains} persisted by the
26
-// feature calendar-sync.
26
+/**
27
+ * NOTE 1: For legacy purposes, read any {@code knownDomains} persisted by the
28
+ * feature calendar-sync.
29
+ *
30
+ * NOTE 2: Never persist the authorization value as it's needed to remain a
31
+ * runtime value to see if we need to re-request the calendar permission from
32
+ * the user.
33
+ */
27 34
 CALENDAR_ENABLED
28 35
     && PersistenceRegistry.register(STORE_NAME, {
29 36
         knownDomains: true

Loading…
취소
저장