open-vault/ui/lib/pki/addon/components/pki-role-form.js
claire bontempo a4270c7701
UI: Pki engine (redesign) routing changes (#17997)
* reroute pki config routes

* issuers routes

* refactor role routes

* add issuer/generate- routes

* update key routes

* refactor cert routes;

* template cleanup, update routes

* update component names with pki-

* update components in test files
2022-11-17 16:50:58 -08:00

57 lines
1.6 KiB
JavaScript

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';
/**
* @module PkiRoleForm
* PkiRoleForm components are used to create and update PKI roles.
*
* @example
* ```js
* <PkiRoleForm @model={{this.model}}/>
* ```
* @callback onCancel
* @callback onSave
* @param {Object} model - pki/role model.
* @param {onCancel} onCancel - Callback triggered when cancel button is clicked.
* @param {onSave} onSave - Callback triggered on save success.
*/
export default class PkiRoleForm extends Component {
@service store;
@service flashMessages;
@tracked errorBanner;
@tracked invalidFormAlert;
@tracked modelValidations;
@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();
}
}