jiti-meet/react/features/calendar-sync/reducer.js

93 lines
2.3 KiB
JavaScript
Raw Normal View History

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
import {
2018-04-16 02:04:57 +00:00
ADD_KNOWN_DOMAIN,
SET_CALENDAR_AUTHORIZATION,
SET_CALENDAR_EVENTS
} 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 = {
/**
* 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-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
}