2022-09-28 22:11:13 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
|
|
|
|
/**
|
2022-11-18 00:50:58 +00:00
|
|
|
* @module PkiRoleForm
|
|
|
|
* PkiRoleForm components are used to create and update PKI roles.
|
2022-09-28 22:11:13 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
2022-11-18 00:50:58 +00:00
|
|
|
* <PkiRoleForm @model={{this.model}}/>
|
2022-09-28 22:11:13 +00:00
|
|
|
* ```
|
|
|
|
* @callback onCancel
|
|
|
|
* @callback onSave
|
2022-11-10 21:27:19 +00:00
|
|
|
* @param {Object} model - pki/role model.
|
2022-09-28 22:11:13 +00:00
|
|
|
* @param {onCancel} onCancel - Callback triggered when cancel button is clicked.
|
|
|
|
* @param {onSave} onSave - Callback triggered on save success.
|
|
|
|
*/
|
|
|
|
|
2022-11-18 00:50:58 +00:00
|
|
|
export default class PkiRoleForm extends Component {
|
2022-09-28 22:11:13 +00:00
|
|
|
@service store;
|
|
|
|
@service flashMessages;
|
|
|
|
|
|
|
|
@tracked errorBanner;
|
|
|
|
@tracked invalidFormAlert;
|
|
|
|
@tracked modelValidations;
|
|
|
|
|
2022-12-02 16:42:14 +00:00
|
|
|
get breadcrumbs() {
|
|
|
|
const backend = this.args.model.backend || 'pki';
|
|
|
|
const crumbs = [
|
|
|
|
{ label: 'secrets', route: 'secrets', linkExternal: true },
|
|
|
|
{ label: backend, route: 'overview' },
|
|
|
|
{ label: 'roles', route: 'roles.index' },
|
|
|
|
];
|
|
|
|
if (!this.args.model.isNew) {
|
|
|
|
crumbs.push({ label: this.args.model.id, route: 'roles.role.details' }, { label: 'edit' });
|
|
|
|
}
|
|
|
|
return crumbs;
|
|
|
|
}
|
|
|
|
|
2022-09-28 22:11:13 +00:00
|
|
|
@task
|
|
|
|
*save(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
try {
|
|
|
|
const { isValid, state, invalidFormMessage } = this.args.model.validate();
|
|
|
|
this.modelValidations = isValid ? null : state;
|
|
|
|
this.invalidFormAlert = invalidFormMessage;
|
|
|
|
if (isValid) {
|
|
|
|
const { isNew, name } = this.args.model;
|
|
|
|
yield this.args.model.save();
|
|
|
|
this.flashMessages.success(`Successfully ${isNew ? 'created' : 'updated'} the role ${name}.`);
|
|
|
|
this.args.onSave();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
const message = error.errors ? error.errors.join('. ') : error.message;
|
|
|
|
this.errorBanner = message;
|
|
|
|
this.invalidFormAlert = 'There was an error submitting this form.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancel() {
|
|
|
|
const method = this.args.model.isNew ? 'unloadRecord' : 'rollbackAttributes';
|
|
|
|
this.args.model[method]();
|
|
|
|
this.args.onCancel();
|
|
|
|
}
|
|
|
|
}
|