Files
threshold-ui/ui/admin/app/routes/scopes/scope/users.js
T

77 lines
1.8 KiB
JavaScript

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import loading from 'ember-loading/decorator';
import { confirm } from 'core/decorators/confirm';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeUsersRoute extends Route {
// =services
@service intl;
@service notify;
@service session;
// =methods
/**
* If arriving here unauthenticated, redirect to index for further processing.
*/
beforeModel() {
if (!this.session.isAuthenticated) this.transitionTo('index');
}
/**
* Load all users under current scope.
* @return {Promise{[UserModel]}}
*/
async model() {
const { id: scope_id } = this.modelFor('scopes.scope');
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.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();
await this.transitionTo('scopes.scope.users.user', user);
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.replaceWith('scopes.scope.users');
this.refresh();
}
}