2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
import reduxStore from '../createStore';
|
|
|
|
// import { get } from './helpers/rest';
|
|
|
|
import database from '../realm';
|
|
|
|
import * as actions from '../../actions';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-06-18 13:30:36 +00:00
|
|
|
const getLastUpdate = () => {
|
2018-04-24 19:34:03 +00:00
|
|
|
const [setting] = database.objects('settings').sorted('_updatedAt', true);
|
|
|
|
return setting && setting._updatedAt;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default async function() {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2018-06-18 13:30:36 +00:00
|
|
|
const lastUpdate = getLastUpdate();
|
|
|
|
const result = await (!lastUpdate ? this.ddp.call('public-settings/get') : this.ddp.call('public-settings/get', new Date(lastUpdate)));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
const filteredSettings = this._prepareSettings(this._filterSettings(result.update || result));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
InteractionManager.runAfterInteractions(() =>
|
|
|
|
database.write(() =>
|
2018-06-18 13:30:36 +00:00
|
|
|
filteredSettings.forEach(setting =>
|
|
|
|
database.create('settings', { ...setting, _updatedAt: new Date() }, true))));
|
2018-05-18 18:32:15 +00:00
|
|
|
reduxStore.dispatch(actions.addSettings(this.parseSettings(filteredSettings)));
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('getSettings', e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|