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-09-11 16:32:52 +00:00
|
|
|
import { settingsUpdatedAt } from '../../constants/settings';
|
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;
|
|
|
|
};
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
function updateServer(param) {
|
|
|
|
database.databases.serversDB.write(() => {
|
|
|
|
database.databases.serversDB.create('servers', { id: this.ddp.url, ...param }, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
export default async function() {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2018-09-28 18:57:29 +00:00
|
|
|
if (!this.ddp) {
|
|
|
|
// TODO: should implement loop or get from rest?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-18 13:30:36 +00:00
|
|
|
const lastUpdate = getLastUpdate();
|
2018-09-11 16:32:52 +00:00
|
|
|
const fetchNewSettings = lastUpdate < settingsUpdatedAt;
|
|
|
|
const result = await ((!lastUpdate || fetchNewSettings) ? this.ddp.call('public-settings/get') : this.ddp.call('public-settings/get', new Date(lastUpdate)));
|
2018-08-31 16:46:33 +00:00
|
|
|
const data = result.update || result || [];
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
const filteredSettings = this._prepareSettings(this._filterSettings(data));
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
InteractionManager.runAfterInteractions(
|
|
|
|
() => database.write(
|
|
|
|
() => filteredSettings.forEach((setting) => {
|
2018-08-31 16:46:33 +00:00
|
|
|
database.create('settings', { ...setting, _updatedAt: new Date() }, true);
|
|
|
|
|
|
|
|
if (setting._id === 'Site_Name') {
|
|
|
|
updateServer.call(this, { name: setting.valueAsString });
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2018-05-18 18:32:15 +00:00
|
|
|
reduxStore.dispatch(actions.addSettings(this.parseSettings(filteredSettings)));
|
2018-08-31 16:46:33 +00:00
|
|
|
|
|
|
|
const iconSetting = data.find(item => item._id === 'Assets_favicon_512');
|
|
|
|
if (iconSetting) {
|
|
|
|
const iconURL = `${ this.ddp.url }/${ iconSetting.value.url || iconSetting.value.defaultUrl }`;
|
|
|
|
updateServer.call(this, { iconURL });
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('getSettings', e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|