ohif-viewer/Packages/ohif-user/client/lib/data.js

55 lines
1.3 KiB
JavaScript
Raw Normal View History

import { Meteor } from 'meteor/meteor';
import { OHIF } from 'meteor/ohif:core';
// Throw error if there is no user logged in
OHIF.user.validate = () => {
if (!Meteor.userId()) {
throw new Meteor.Error('not-authorized');
}
};
// Get the persistent data by key
OHIF.user.getData = key => {
// Check if there is an user logged in
OHIF.user.validate();
2017-05-02 18:06:45 +02:00
// Get the user object
const user = Meteor.user();
// Get user profile data
2017-05-02 18:06:45 +02:00
const profile = user && user.profile;
// Get the user persistent data
const data = profile && profile.persistent;
2017-05-01 19:04:26 +02:00
let result = data;
const keys = key.split('.');
keys.forEach(key => {
2017-09-19 15:30:22 +02:00
if (typeof result !== 'object' || !result) return;
2017-05-01 19:04:26 +02:00
result = result[key];
});
return result;
};
// Store the persistent data by giving a key and a value to store
OHIF.user.setData = (key, value) => {
2017-04-28 21:05:19 +02:00
return new Promise((resolve, reject) => {
try {
// Check if there is an user logged in
OHIF.user.validate();
} catch(error) {
reject(error);
}
// Call the update method on server-side
Meteor.call('ohif.user.data.set', key, value, error => {
if (error) {
reject(error);
}
2017-04-28 21:05:19 +02:00
resolve();
});
});
};