From ac63a0fa7381dce3643aed80368d8fb5ce0d1120 Mon Sep 17 00:00:00 2001 From: Bettenbuk Zoltan Date: Thu, 28 Jun 2018 11:09:25 +0200 Subject: [PATCH] Calendar feature disabled state getter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- react/features/calendar-sync/constants.js | 11 ++++++++ react/features/calendar-sync/functions.js | 18 +++++++++++++ react/features/calendar-sync/index.js | 1 + react/features/calendar-sync/reducer.js | 33 ++++++++++++++--------- 4 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 react/features/calendar-sync/functions.js diff --git a/react/features/calendar-sync/constants.js b/react/features/calendar-sync/constants.js index 657e36869..b7743ecd0 100644 --- a/react/features/calendar-sync/constants.js +++ b/react/features/calendar-sync/constants.js @@ -10,6 +10,17 @@ import { NativeModules } from 'react-native'; */ export const CALENDAR_ENABLED = _isCalendarEnabled(); +/** + * The default state of the calendar. + * + * NOTE: This is defined here, to be reusable by functions.js as well (see file + * for details). + */ +export const DEFAULT_STATE = { + authorization: undefined, + events: [] +}; + /** * Determines whether the calendar feature is enabled by the app. For * example, Apple through its App Store requires diff --git a/react/features/calendar-sync/functions.js b/react/features/calendar-sync/functions.js new file mode 100644 index 000000000..b55ddfb3e --- /dev/null +++ b/react/features/calendar-sync/functions.js @@ -0,0 +1,18 @@ +// @flow +import { toState } from '../base/redux'; + +import { CALENDAR_ENABLED, DEFAULT_STATE } from './constants'; + +/** + * Returns the calendar state, considering the enabled/disabled state of the + * feature. Since that is the normal Redux behaviour, this function will always + * return an object (the default state if the feature is disabled). + * + * @param {Object | Function} stateful - An object or a function that can be + * resolved to a Redux state by {@code toState}. + * @returns {Object} + */ +export function getCalendarState(stateful: Object | Function) { + return CALENDAR_ENABLED + ? toState(stateful)['features/calendar-sync'] : DEFAULT_STATE; +} diff --git a/react/features/calendar-sync/index.js b/react/features/calendar-sync/index.js index daaa5d02a..65787823f 100644 --- a/react/features/calendar-sync/index.js +++ b/react/features/calendar-sync/index.js @@ -1,4 +1,5 @@ export * from './components'; +export * from './functions'; import './middleware'; import './reducer'; diff --git a/react/features/calendar-sync/reducer.js b/react/features/calendar-sync/reducer.js index f4e3ebb85..b305bdb4f 100644 --- a/react/features/calendar-sync/reducer.js +++ b/react/features/calendar-sync/reducer.js @@ -8,22 +8,29 @@ import { SET_CALENDAR_AUTHORIZATION, SET_CALENDAR_EVENTS } from './actionTypes'; -import { CALENDAR_ENABLED } from './constants'; - -const DEFAULT_STATE = { - /** - * Note: If features/calendar-sync ever gets persisted, do not persist the - * authorization value as it's needed to remain a runtime value to see if we - * need to re-request the calendar permission from the user. - */ - authorization: undefined, - events: [] -}; +import { CALENDAR_ENABLED, DEFAULT_STATE } from './constants'; +/** + * Constant for the Redux subtree of the calendar feature. + * + * NOTE: Please do not access this subtree directly outside of this feature. + * This feature can be disabled (see {@code constants.js} for details), and in + * that case, accessing this subtree directly will return undefined and will + * need a bunch of repetitive type checks in other features. Use the + * {@code getCalendarState} function instead, or make sure you take care of + * those checks, or consider using the {@code CALENDAR_ENABLED} const to gate + * features if needed. + */ const STORE_NAME = 'features/calendar-sync'; -// XXX For legacy purposes, read any {@code knownDomains} persisted by the -// feature calendar-sync. +/** + * NOTE 1: For legacy purposes, read any {@code knownDomains} persisted by the + * feature calendar-sync. + * + * NOTE 2: Never persist the authorization value as it's needed to remain a + * runtime value to see if we need to re-request the calendar permission from + * the user. + */ CALENDAR_ENABLED && PersistenceRegistry.register(STORE_NAME, { knownDomains: true