Files
threshold-ui/ui/admin/app/routes/scopes/scope/users.js
T
Zhihe Li bcf004b8a4 Reformat codebase with trailing commas in functions (#2006)
* style: 💄 Update trailing commas in API

* style: 💄 Update trailing commas in core

* style: 💄 Update trailing commas in rose

* style: 💄 Update trailing commas in admin

* style: 💄 Update trailing commas in desktop
2023-11-22 14:07:41 -05:00

90 lines
2.2 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeUsersRoute extends Route {
// =services
@service store;
@service session;
@service can;
@service router;
// =methods
/**
* If arriving here unauthenticated, redirect to index for further processing.
*/
beforeModel() {
if (!this.session.isAuthenticated) this.router.transitionTo('index');
}
/**
* Load all users under current scope.
* @return {Promise{[UserModel]}}
*/
model() {
const scope = this.modelFor('scopes.scope');
const { id: scope_id } = scope;
if (this.can.can('list model', scope, { collection: 'users' })) {
return this.store.query('user', { scope_id });
}
}
// =actions
/**
* Rollback changes on an user.
* @param {UserModel} user
*/
@action
cancel(user) {
const { isNew } = user;
user.rollbackAttributes();
if (isNew) this.router.transitionTo('scopes.scope.users');
}
/**
* Save an user in current scope.
* @param {UserModel} user
* @param {Event} e
*/
@action
@loading
@notifyError(({ message }) => message)
@notifySuccess(({ isNew }) =>
isNew ? 'notifications.create-success' : 'notifications.save-success',
)
async save(user) {
await user.save();
if (this.can.can('read model', user)) {
await this.router.transitionTo('scopes.scope.users.user', user);
} else {
await this.router.transitionTo('scopes.scope.users');
}
this.refresh();
}
/**
* Delete user in current scope and redirect to index.
* @param {UserModel} user
*/
@action
@loading
@confirm('questions.delete-confirm')
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.delete-success')
async delete(user) {
await user.destroyRecord();
await this.router.replaceWith('scopes.scope.users');
this.refresh();
}
}