Site settings and fires fromNow Reactive

This commit is contained in:
vjrj 2018-01-29 11:30:17 +01:00
parent e4b0135953
commit 379e72a9e7
15 changed files with 482 additions and 64 deletions

View file

@ -0,0 +1,49 @@
/* eslint-disable consistent-return */
/* eslint-disable import/no-absolute-path */
import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
import { defaultCreatedAt, defaultUpdateAt } from '/imports/api/Utility/Utils.js';
import SiteSettingsTypes from './SiteSettingsTypes';
const SiteSettings = new Mongo.Collection('siteSettings', { idGeneration: 'MONGO' });
SiteSettings.allow({
insert: () => false,
update: () => false,
remove: () => false
});
SiteSettings.deny({
insert: () => true,
update: () => true,
remove: () => true
});
SiteSettings.get = (name) => {
const setting = SiteSettings.findOne({
name
});
return typeof setting === 'object' ? setting.value : undefined;
};
SiteSettings.observe = (name, callback) => {
SiteSettings.find({ name }).observe({
added: (document) => {
callback(document.value);
}
});
};
SiteSettings.getSchema = type => new SimpleSchema({
name: String,
type: String,
description: String,
value: SiteSettingsTypes[type].value,
createdAt: defaultCreatedAt,
updatedAt: defaultUpdateAt
});
// SiteSettings.attachSchema(SiteSettings.schema);
export default SiteSettings;

View file

@ -0,0 +1,66 @@
// Adapted from: https://github.com/yogiben/meteor-admin-settings
const SiteSettingsTypes = {
string: {
value: {
type: String
}
},
textarea: {
value: {
type: String
/* autoform: {
* afFieldInput: {
* rows: 5
* }
* } */
}
},
number: {
value: {
type: Number
}
},
boolean: {
value: {
type: Boolean
/* autoform: {
* afFieldInput: {
* type: 'boolean-select'
* }
* } */
}
},
date: {
value: {
type: Date
/* autoform: {
* afFieldInput: {
* type: 'date'
* }
* } */
}
},
color: {
value: {
type: String
/* autoform: {
* afFieldInput: {
* type: 'color'
* }
* } */
}
},
password: {
value: {
type: String
/* autoform: {
* afFieldInput: {
* type: 'password'
* }
* } */
}
}
};
export default SiteSettingsTypes;

View file

@ -0,0 +1,60 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import SiteSettings from './SiteSettings';
import SiteSettingsTypes from './SiteSettingsTypes';
import rateLimit from '../../modules/rate-limit';
Meteor.methods({
'siteSettings.insert': function siteSettingsInsert(setting) {
check(setting, {
name: String,
type: String,
description: String,
value: SiteSettingsTypes[self.type].value
});
SiteSettings.getSchema.validate(setting);
try {
return SiteSettings.insert({ owner: this.userId, ...setting });
} catch (exception) {
throw new Meteor.Error('500', exception);
}
},
'siteSettings.update': function siteSettingsUpdate(setting) {
check(setting, {
_id: String,
name: String,
type: String,
description: String,
value: SiteSettingsTypes[self.type].value
});
try {
const siteSettingId = setting._id;
SiteSettings.update(siteSettingId, { $set: setting });
return siteSettingId; // Return _id so we can redirect to siteSetting after update.
} catch (exception) {
throw new Meteor.Error('500', exception);
}
},
'siteSettings.remove': function siteSettingsRemove(siteSettingId) {
check(siteSettingId, String);
try {
return SiteSettings.remove(siteSettingId);
} catch (exception) {
throw new Meteor.Error('500', exception);
}
}
});
rateLimit({
methods: [
'siteSettings.insert',
'siteSettings.update',
'siteSettings.remove'
],
limit: 5,
timeRange: 1000
});

View file

@ -0,0 +1,6 @@
/* eslint-disable prefer-arrow-callback */
import { Meteor } from 'meteor/meteor';
import SiteSettings from '../SiteSettings';
Meteor.publish('settings', () => SiteSettings.find());