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

70 lines
1.6 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
2018-02-14 16:21:52 +00:00
import { NEW_CALENDAR_ENTRY_LIST, NEW_KNOWN_DOMAIN } from './actionTypes';
2018-02-08 18:50:19 +00:00
/**
* ZB: this is an object, as further data is to come here, like:
* - known domain list
*/
const DEFAULT_STATE = {
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-02-08 18:50:19 +00:00
ReducerRegistry.register(
STORE_NAME,
(state = DEFAULT_STATE, action) => {
switch (action.type) {
case NEW_CALENDAR_ENTRY_LIST:
return {
2018-02-14 16:21:52 +00:00
...state,
2018-02-08 18:50:19 +00:00
events: action.events
};
2018-02-14 16:21:52 +00:00
case NEW_KNOWN_DOMAIN:
return _maybeAddNewDomain(state, action);
2018-02-08 18:50:19 +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.
*
* @private
* @param {Object} state - The redux state.
* @param {Object} action - The redux action.
* @returns {Object}
*/
function _maybeAddNewDomain(state, action) {
let { domainName } = action;
const { knownDomains } = state;
if (domainName && domainName.length) {
domainName = domainName.toLowerCase();
if (knownDomains.indexOf(domainName) === -1) {
knownDomains.push(domainName);
// Ensure the list doesn't exceed a/the maximum size.
knownDomains.splice(0, knownDomains.length - MAX_DOMAIN_LIST_SIZE);
}
}
return {
...state,
knownDomains
};
}