Ghost/ghost/admin/app/routes/settings/staff/user.js
Kevin Ansfield 524b23c182
Migrated staff user screen to Ember Octane patterns (#15532)
refs https://github.com/TryGhost/Ghost/issues/14101

- migrated staff user controller to native class syntax
- removed use of `{{action}}` helper
- moved from custom components to native `<input>` and `<textarea>` for form fields
  - added `{{select-on-click}}` modifier to cover the `<GhTextingInput @selectOnClick>` option behaviour for any input element
- added `submitForm()` test helper that finds closest `form` element and trigger's a `submit` event on it simulating <kbd>Enter</kbd> being pressed whilst a field has focus
2022-10-05 12:05:31 +01:00

102 lines
2.8 KiB
JavaScript

import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
import ConfirmUnsavedChangesModal from '../../../components/modals/confirm-unsaved-changes';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
export default class UserRoute extends AuthenticatedRoute {
@service modals;
model(params) {
return this.store.queryRecord('user', {slug: params.user_slug, include: 'count.posts'});
}
afterModel(user) {
super.afterModel(...arguments);
const currentUser = this.session.user;
let isOwnProfile = user.id === currentUser.id;
let isAuthorOrContributor = currentUser.isAuthorOrContributor;
let isEditor = currentUser.isEditor;
if (isAuthorOrContributor && !isOwnProfile) {
this.transitionTo('settings.staff.user', currentUser);
} else if (isEditor && !isOwnProfile && !user.isAuthorOrContributor) {
this.transitionTo('settings.staff');
}
if (isOwnProfile) {
this.store.queryRecord('api-key', {id: 'me'}).then((apiKey) => {
this.controller.personalToken = apiKey.id + ':' + apiKey.secret;
this.controller.personalTokenRegenerated = false;
});
}
}
serialize(model) {
return {user_slug: model.get('slug')};
}
setupController(controller, model) {
controller.model = model;
controller.reset();
}
@action
async willTransition(transition) {
if (this.hasConfirmed) {
return true;
}
transition.abort();
// wait for any existing confirm modal to be closed before allowing transition
if (this.confirmModal) {
return;
}
if (this.controller.saveTask?.isRunning) {
await this.controller.saveTask.last;
}
const shouldLeave = await this.confirmUnsavedChanges();
if (shouldLeave) {
this.controller.reset();
this.hasConfirmed = true;
return transition.retry();
}
}
async confirmUnsavedChanges() {
if (this.controller.model.hasDirtyAttributes || this.controller.dirtyAttributes) {
this.confirmModal = this.modals
.open(ConfirmUnsavedChangesModal)
.finally(() => {
this.confirmModal = null;
});
return this.confirmModal;
}
return true;
}
deactivate() {
this.confirmModal = null;
this.hasConfirmed = false;
this.controller.reset();
}
@action
didTransition() {
this.modelFor('settings.staff.user').get('errors').clear();
}
buildRouteInfoMetadata() {
return {
titleToken: 'Staff - User'
};
}
}