Update default selection for roles if input is not chosen

This commit is contained in:
Michael Rogers 2023-06-21 10:01:14 -05:00
parent 76faabf471
commit 6c087a1d1e
3 changed files with 23 additions and 13 deletions

View File

@ -21,7 +21,7 @@
*****************************************************************************/ *****************************************************************************/
import ExampleUserProvider from './ExampleUserProvider'; import ExampleUserProvider from './ExampleUserProvider';
const AUTO_LOGIN_USER = 'guest'; const AUTO_LOGIN_USER = 'mct-user';
const STATUS_ROLES = ['flight', 'driver']; const STATUS_ROLES = ['flight', 'driver'];
export default function ExampleUserPlugin({autoLoginUser, statusRoles} = { export default function ExampleUserPlugin({autoLoginUser, statusRoles} = {

View File

@ -90,6 +90,12 @@ class UserAPI extends EventEmitter {
return this._provider.getCurrentUser(); return this._provider.getCurrentUser();
} }
} }
/**
* If a user provider is set, it will return an array of possible roles
* that can be selected by the current user
* @memberof module:openmct.UserAPI#
* @returns {Array}
*/
getPossibleRoles() { getPossibleRoles() {
if (!this.hasProvider()) { if (!this.hasProvider()) {
@ -100,22 +106,24 @@ class UserAPI extends EventEmitter {
} }
/** /**
* If a user provider is set, it will return the active role Id * If a user provider is set, it will return the active role or null
* @returns object * @memberof module:openmct.UserAPI#
* @returns {string|null}
*/ */
getActiveRole() { getActiveRole() {
if (!this.hasProvider()) { if (!this.hasProvider()) {
return Promise.resolve(undefined); return null;
} }
// get from session storage // get from session storage
const sessionStorageValue = SessionPersistance.getActiveRole(); const sessionStorageValue = SessionPersistance.getActiveRole();
if (sessionStorageValue === 'undefined' || sessionStorageValue === undefined) {
return undefined;
}
return sessionStorageValue; return sessionStorageValue;
} }/**
* Set the active role in session storage
* @memberof module:openmct.UserAPI#
* @returns {undefined}
*/
setActiveRole(role) { setActiveRole(role) {
SessionPersistance.setActiveRole(role); SessionPersistance.setActiveRole(role);
} }

View File

@ -71,10 +71,11 @@ export default {
}, },
promptForRoleSelection() { promptForRoleSelection() {
const allRoles = this.openmct.user.getPossibleRoles(); const allRoles = this.openmct.user.getPossibleRoles();
const selectionOptions = allRoles.map(role => ({ const selectionOptions = allRoles
key: role, .map(role => ({
name: role key: role,
})).filter(this.openmct.user.canProvideStatusForRole); name: role
})).filter(this.openmct.user.canProvideStatusForRole);
const dialog = this.openmct.overlays.selection({ const dialog = this.openmct.overlays.selection({
selectionOptions, selectionOptions,
@ -91,7 +92,8 @@ export default {
emphasis: true, emphasis: true,
callback: () => { callback: () => {
dialog.dismiss(); dialog.dismiss();
this.updateRole(this.inputRoleSelection); const inputValueOrDefault = this.inputRoleSelection || selectionOptions[0].key;
this.updateRole(inputValueOrDefault);
this.openmct.notifications.info(`Successfully set new role to ${this.role}`); this.openmct.notifications.info(`Successfully set new role to ${this.role}`);
} }
} }