2018-02-08 18:50:19 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import { ReducerRegistry } from '../base/redux';
|
2018-02-14 16:21:52 +00:00
|
|
|
import { PersistenceRegistry } from '../base/storage';
|
2018-02-08 18:50:19 +00:00
|
|
|
|
2018-03-20 13:51:51 +00:00
|
|
|
import {
|
2018-04-16 02:04:57 +00:00
|
|
|
ADD_KNOWN_DOMAIN,
|
|
|
|
SET_CALENDAR_AUTHORIZATION,
|
|
|
|
SET_CALENDAR_EVENTS
|
2018-03-20 13:51:51 +00:00
|
|
|
} from './actionTypes';
|
2018-04-16 16:39:26 +00:00
|
|
|
import { CALENDAR_ENABLED } from './constants';
|
2018-02-08 18:50:19 +00:00
|
|
|
|
|
|
|
const DEFAULT_STATE = {
|
2018-03-20 13:51:51 +00:00
|
|
|
/**
|
|
|
|
* Note: If features/calendar-sync ever gets persisted, do not persist the
|
2018-04-16 02:04:57 +00:00
|
|
|
* 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.
|
2018-03-20 13:51:51 +00:00
|
|
|
*/
|
2018-04-16 02:04:57 +00:00
|
|
|
authorization: undefined,
|
2018-02-14 16:21:52 +00:00
|
|
|
events: [],
|
|
|
|
knownDomains: []
|
2018-02-08 18:50:19 +00:00
|
|
|
};
|
2018-02-14 16:21:52 +00:00
|
|
|
|
|
|
|
const MAX_DOMAIN_LIST_SIZE = 10;
|
|
|
|
|
2018-02-08 18:50:19 +00:00
|
|
|
const STORE_NAME = 'features/calendar-sync';
|
|
|
|
|
2018-04-16 16:39:26 +00:00
|
|
|
CALENDAR_ENABLED
|
|
|
|
&& PersistenceRegistry.register(STORE_NAME, {
|
|
|
|
knownDomains: true
|
|
|
|
});
|
|
|
|
|
|
|
|
CALENDAR_ENABLED
|
|
|
|
&& ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case ADD_KNOWN_DOMAIN:
|
|
|
|
return _addKnownDomain(state, action);
|
|
|
|
|
|
|
|
case SET_CALENDAR_AUTHORIZATION:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
authorization: action.status
|
|
|
|
};
|
|
|
|
|
|
|
|
case SET_CALENDAR_EVENTS:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
events: action.events
|
|
|
|
};
|
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
});
|
2018-02-14 16:21:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new domain to the known domain list if not present yet.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @param {Object} action - The redux action.
|
2018-04-16 02:04:57 +00:00
|
|
|
* @private
|
2018-02-14 16:21:52 +00:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2018-04-16 02:04:57 +00:00
|
|
|
function _addKnownDomain(state, action) {
|
|
|
|
let { knownDomain } = action;
|
|
|
|
|
|
|
|
if (knownDomain) {
|
|
|
|
knownDomain = knownDomain.toLowerCase();
|
|
|
|
|
|
|
|
let { knownDomains } = state;
|
2018-02-14 16:21:52 +00:00
|
|
|
|
2018-04-16 02:04:57 +00:00
|
|
|
if (knownDomains.indexOf(knownDomain) === -1) {
|
|
|
|
// Add the specified known domain and at the same time avoid
|
|
|
|
// modifying the knownDomains Array instance referenced by the
|
|
|
|
// current redux state.
|
|
|
|
knownDomains = [
|
|
|
|
...state.knownDomains,
|
|
|
|
knownDomain
|
|
|
|
];
|
2018-02-14 16:21:52 +00:00
|
|
|
|
|
|
|
// Ensure the list doesn't exceed a/the maximum size.
|
|
|
|
knownDomains.splice(0, knownDomains.length - MAX_DOMAIN_LIST_SIZE);
|
2018-04-16 02:04:57 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
knownDomains
|
|
|
|
};
|
2018-02-14 16:21:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 02:04:57 +00:00
|
|
|
return state;
|
2018-02-14 16:21:52 +00:00
|
|
|
}
|