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
|
|
|
|
|
|
|
const getLastMessage = () => {
|
|
|
|
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 {
|
|
|
|
const lastMessage = getLastMessage();
|
|
|
|
const result = await (!lastMessage ? this.ddp.call('public-settings/get') : this.ddp.call('public-settings/get', new Date(lastMessage)));
|
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(() =>
|
|
|
|
filteredSettings.forEach(setting => database.create('settings', setting, 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
|
|
|
}
|