claire bontempo 4617af328b
UI: Refactor tool actions form (#27406)
* rename wrap test

* refactor tool hash component

* whoops fix component syntax

* random refactor

* rewrap component

* unwrap component

* lookup refactor

* wrap refactor

* update selectors

* delete tool action form component

* co-locate templates

* Revert "co-locate templates"

This reverts commit c52bb9875284a4ee78c773c794f4fe572ae7a7f4.

* fix component jsdoc syntax

* rename tracked property

* rename rewrap token input selector

* remove parseint now that input is typed as a number

* nvm convert to number

* co-locate templates

* move to tools/ folder

* add flash message to test
2024-06-11 01:47:36 +00:00

67 lines
1.6 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import Component from '@glimmer/component';
import { service } from '@ember/service';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import errorMessage from 'vault/utils/error-message';
/**
* @module ToolsWrap
* ToolsWrap components are components that sys/wrapping/wrap functionality.
*
* @example
* <Tools::Wrap />
*/
export default class ToolsWrap extends Component {
@service store;
@service flashMessages;
@tracked buttonDisabled = false;
@tracked token = '';
@tracked wrapTTL = null;
@tracked wrapData = '{\n}';
@tracked errorMessage = '';
@action
reset(clearData = true) {
this.token = '';
this.errorMessage = '';
this.wrapTTL = null;
if (clearData) this.wrapData = '{\n}';
}
@action
updateTtl(evt) {
if (!evt) return;
this.wrapTTL = evt.enabled ? `${evt.seconds}s` : '30m';
}
@action
codemirrorUpdated(val, codemirror) {
codemirror.performLint();
const hasErrors = codemirror?.state.lint.marked?.length > 0;
this.buttonDisabled = hasErrors;
if (!hasErrors) this.wrapData = val;
}
@action
async handleSubmit(evt) {
evt.preventDefault();
const data = JSON.parse(this.wrapData);
const wrapTTL = this.wrapTTL || null;
try {
const response = await this.store.adapterFor('tools').toolAction('wrap', data, { wrapTTL });
this.token = response.wrap_info.token;
this.flashMessages.success('Wrap was successful.');
} catch (error) {
this.errorMessage = errorMessage(error);
}
}
}