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

90 lines
2.1 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-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-02-14 16:21:52 +00:00
PersistenceRegistry.register(STORE_NAME, {
knownDomains: true
});
2018-04-16 02:04:57 +00:00
ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
switch (action.type) {
case ADD_KNOWN_DOMAIN:
return _addKnownDomain(state, action);
2018-04-16 02:04:57 +00:00
case SET_CALENDAR_AUTHORIZATION:
return {
...state,
authorization: action.status
};
2018-02-08 18:50:19 +00:00
2018-04-16 02:04:57 +00:00
case SET_CALENDAR_EVENTS:
return {
...state,
events: action.events
};
2018-02-14 16:21:52 +00:00
2018-04-16 02:04:57 +00:00
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
}