Store status roles in an array instead of a singular value

This commit is contained in:
Michael Rogers 2023-06-14 11:45:40 -05:00
parent 550fa7e7e7
commit c0e7804ccd
2 changed files with 17 additions and 11 deletions

View File

@ -68,7 +68,10 @@ export default class ExampleUserProvider extends EventEmitter {
this.user = undefined; this.user = undefined;
this.loggedIn = false; this.loggedIn = false;
this.autoLoginUser = undefined; this.autoLoginUser = undefined;
this.status = STATUSES[0]; this.statusRoleValues = statusRoles.map(x => ({
role: x,
status: STATUSES[0]
}));
this.pollQuestion = undefined; this.pollQuestion = undefined;
// this.defaultStatusRole = defaultStatusRole; // this.defaultStatusRole = defaultStatusRole;
this.statusRoles = statusRoles; this.statusRoles = statusRoles;
@ -109,13 +112,13 @@ export default class ExampleUserProvider extends EventEmitter {
return Promise.resolve(this.user.getRoles().includes(roleId)); return Promise.resolve(this.user.getRoles().includes(roleId));
} }
getActiveRole() { // getActiveRole() {
if (!this.loggedIn) { // if (!this.loggedIn) {
Promise.resolve(undefined); // Promise.resolve(undefined);
} // }
return Promise.resolve(this.selectedStatusRole); // return Promise.resolve(this.selectedStatusRole);
} // }
getPossibleRoles() { getPossibleRoles() {
return this.user.getRoles(); return this.user.getRoles();
} }
@ -130,7 +133,9 @@ export default class ExampleUserProvider extends EventEmitter {
} }
getStatusForRole(role) { getStatusForRole(role) {
return Promise.resolve(this.status); const statusForRole = this.statusRoleValues.find(x => x.role === role);
return Promise.resolve(statusForRole?.status);
} }
async getDefaultStatusForRole(role) { async getDefaultStatusForRole(role) {
@ -141,7 +146,8 @@ export default class ExampleUserProvider extends EventEmitter {
setStatusForRole(role, status) { setStatusForRole(role, status) {
status.timestamp = Date.now(); status.timestamp = Date.now();
this.status = status; const matchingIndex = this.statusRoleValues.findIndex(x => x.role === role);
this.statusRoleValues[matchingIndex].status = status;
this.emit('statusChange', { this.emit('statusChange', {
role, role,
status status

View File

@ -151,11 +151,11 @@ export default class StatusAPI extends EventEmitter {
* @param {Status} status The status to set for the provided role * @param {Status} status The status to set for the provided role
* @returns {Promise<Boolean>} true if operation was successful, otherwise false. * @returns {Promise<Boolean>} true if operation was successful, otherwise false.
*/ */
async setStatusForRole(role, status) { setStatusForRole(_role, status) {
const provider = this.#userAPI.getProvider(); const provider = this.#userAPI.getProvider();
if (provider.setStatusForRole) { if (provider.setStatusForRole) {
const activeRole = await provider.getActiveRole(); const activeRole = this.#userAPI.getActiveRole();
if (!provider.canProvideStatusForRole(activeRole)) { if (!provider.canProvideStatusForRole(activeRole)) {
return false; return false;
} }