Skip to content

Commit

Permalink
Added UserSettings class
Browse files Browse the repository at this point in the history
  • Loading branch information
adhbh committed Apr 8, 2016
1 parent 1db6dba commit 747dfc8
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/current-user/CurrentUser.js
@@ -1,4 +1,5 @@
import UserAuthorities from './UserAuthorities';
import UserSettings from './UserSettings';

const models = Symbol('models');
const propertiesToIgnore = new Set([
Expand Down Expand Up @@ -50,11 +51,25 @@ function getUserPropertiesToCopy(currentUserObject) {
}

export default class CurrentUser {
constructor(userData, userAuthorities, modelDefinitions) {
constructor(userData, userAuthorities, modelDefinitions, settings) {
Object.assign(this, getUserPropertiesToCopy(userData));

this.authorities = userAuthorities;
this[models] = modelDefinitions;

/**
* @property {UserSettings} settings Contains a reference to a `UserSettings` instance that can be used
* to retrieve and save system settings.
*
* @description
* ```js
* d2.currentUser.userSettings.get('keyUiLocale')
* .then(userSettingsValue => {
* console.log('UI Locale: ' + userSettingsValue);
* });
* ```
*/
this.userSettings = settings;
}

getUserGroups() {
Expand Down Expand Up @@ -132,6 +147,6 @@ export default class CurrentUser {
}

static create(userData, authorities, modelDefinitions) {
return new CurrentUser(userData, UserAuthorities.create(authorities), modelDefinitions);
return new CurrentUser(userData, UserAuthorities.create(authorities), modelDefinitions, new UserSettings());
}
}
76 changes: 76 additions & 0 deletions src/current-user/UserSettings.js
@@ -0,0 +1,76 @@
import Api from '../api/Api';
import { isString } from '../lib/check';


/**
* @class UserSettings
*
* @description
* Handles communication with the userSettings endpoint. Can be used to get or save userSettings.
*/

class UserSettings {
constructor(api = Api.getApi()) {
this.api = api;
}

/**
* @method all
*
* @returns {Promise} Promise that resolves with the usersettings object from the api.
*
* @description
* Loads all the user settings of current user and returns them as an object from the promise.
* ```js
* d2.currentUser.userSettings.all()
* .then(userSettings => {
* console.log('UI Locale: ' + userSettings.keyUiLocale);
* });
* ```
*/
all() {
return this.api.get('userSettings');
}

/**
* @method get
*
* @param {String} userSettingsKey The identifier of the user setting that should be retrieved.
* @returns {Promise} A promise that resolves with the value or will fail if the value is not available.
*
* @description
* ```js
* d2.currentUser.userSettings.get('keyUiLocale')
* .then(userSettingValue => {
* console.log('UI Locale: ' + userSettingValue);
* });
* ```
*/
get(userSettingsKey) {
function processValue(value) {
// Attempt to parse the response as JSON. If this fails we return the value as is.
try {
return JSON.parse(value);
} catch (e) {
return value;
}
}

return new Promise((resolve, reject) => {
if (!isString(userSettingsKey)) {
throw new TypeError('A "key" parameter should be specified when calling get() on systemSettings');
}

this.api.get(['userSettings', userSettingsKey].join('/'), undefined, { dataType: 'text' })
.then(response => {
const userSettingValue = processValue(response);
if (userSettingValue) {
resolve(processValue(response));
}
reject(new Error('The requested userSetting has no value or does not exist.'));
});
});
}
}

export default UserSettings;
3 changes: 1 addition & 2 deletions src/d2.js
Expand Up @@ -154,8 +154,7 @@ export function init(initConfig) {
});

d2.currentUser = CurrentUser.create(responses.currentUser, responses.authorities, d2.models);
d2.currentUser.userSettings = responses.userSettings;

d2.currentUser.userSettings = Object.assign(d2.currentUser.userSettings, responses.userSettings);
d2.system.setSystemInfo(responses.systemInfo);
d2.system.setInstalledApps(responses.apps);

Expand Down

0 comments on commit 747dfc8

Please sign in to comment.