Skip to content

Commit

Permalink
[RN] Legacy support of calendar-sync's knownDomains
Browse files Browse the repository at this point in the history
Knowledge is power, man!

We moved "knownDomains" from calendar-sync to base/known-domains.
However, we do have an official release in the app stores and I'd like
us to not throw away the knowledge it has acquired.
  • Loading branch information
lyubomir committed May 14, 2018
1 parent d4dea22 commit 631f51d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 19 deletions.
62 changes: 47 additions & 15 deletions react/features/calendar-sync/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import RNCalendarEvents from 'react-native-calendar-events';

import { APP_WILL_MOUNT } from '../app';
import { ADD_KNOWN_DOMAINS } from '../base/known-domains';
import { ADD_KNOWN_DOMAINS, addKnownDomains } from '../base/known-domains';
import { MiddlewareRegistry } from '../base/redux';
import { APP_LINK_SCHEME, parseURIString } from '../base/util';
import { APP_STATE_CHANGED } from '../mobile/background';
Expand Down Expand Up @@ -31,24 +31,58 @@ const MAX_LIST_LENGTH = 10;

CALENDAR_ENABLED
&& MiddlewareRegistry.register(store => next => action => {
const result = next(action);

switch (action.type) {
case ADD_KNOWN_DOMAINS:
case APP_WILL_MOUNT:
_fetchCalendarEntries(store, false, false);
break;
case ADD_KNOWN_DOMAINS: {
// XXX Fetch new calendar entries only when an actual domain has
// become known.
const { getState } = store;
const oldValue = getState()['features/base/known-domains'];
const result = next(action);
const newValue = getState()['features/base/known-domains'];

oldValue === newValue || _fetchCalendarEntries(store, false, false);

return result;
}

case APP_STATE_CHANGED: {
const result = next(action);

case APP_STATE_CHANGED:
_maybeClearAccessStatus(store, action);
break;

case REFRESH_CALENDAR:
return result;
}

case APP_WILL_MOUNT: {
// For legacy purposes, we've allowed the deserialization of
// knownDomains and now we're to translate it to base/known-domains.
const state = store.getState()['features/calendar-sync'];

if (state) {
const { knownDomains } = state;

Array.isArray(knownDomains)
&& knownDomains.length
&& store.dispatch(addKnownDomains(knownDomains));
}

const result = next(action);

_fetchCalendarEntries(store, false, false);

return result;
}

case REFRESH_CALENDAR: {
const result = next(action);

_fetchCalendarEntries(store, true, action.forcePermission);
break;

return result;
}
}

return result;
return next(action);
});

/**
Expand Down Expand Up @@ -122,9 +156,7 @@ function _fetchCalendarEntries(
logger.warn('Calendar access not granted.');
}
})
.catch(reason => {
logger.error('Error accessing calendar.', reason);
});
.catch(reason => logger.error('Error accessing calendar.', reason));
}

/**
Expand Down
25 changes: 21 additions & 4 deletions react/features/calendar-sync/reducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @flow

import { ReducerRegistry } from '../base/redux';
import { APP_WILL_MOUNT } from '../app';
import { ReducerRegistry, set } from '../base/redux';
import { PersistenceRegistry } from '../base/storage';

import {
SET_CALENDAR_AUTHORIZATION,
Expand All @@ -20,9 +22,25 @@ const DEFAULT_STATE = {

const STORE_NAME = 'features/calendar-sync';

// XXX For legacy purposes, read any {@code knownDomains} persisted by the
// feature calendar-sync.
CALENDAR_ENABLED
&& PersistenceRegistry.register(STORE_NAME, {
knownDomains: true
});

CALENDAR_ENABLED
&& ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
switch (action.type) {
case APP_WILL_MOUNT:
// For legacy purposes, we've allowed the deserialization of
// knownDomains. At this point, it should have already been
// translated into the new state format (namely, base/known-domains)
// and the app no longer needs it.
if (typeof state.knownDomains !== 'undefined') {
return set(state, 'knownDomains', undefined);
}
break;

case SET_CALENDAR_AUTHORIZATION:
return {
Expand All @@ -35,8 +53,7 @@ CALENDAR_ENABLED
...state,
events: action.events
};

default:
return state;
}

return state;
});

0 comments on commit 631f51d

Please sign in to comment.