Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import Component from '@glimmer/component';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import errorMessage from 'vault/utils/error-message';
import apiErrorMessage from 'vault/utils/api-error-message';

import type ApiService from 'vault/services/api';
import type FlashMessageService from 'vault/services/flash-messages';
import type { HTMLElementEvent } from 'vault/forms';

/**
* @module ToolsHash
Expand All @@ -17,32 +21,33 @@ import errorMessage from 'vault/utils/error-message';
* <Tools::Hash />
*/
export default class ToolsHash extends Component {
@service store;
@service flashMessages;
@service declare readonly api: ApiService;
@service declare readonly flashMessages: FlashMessageService;

@tracked algorithm = 'sha2-256';
@tracked format = 'base64';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my own information, I know these files are older, but wouldn't these defaults be set by the model? And now that we're moving away from a model, they'd be set by the apiSpec? For example algorithm's default would be set by the api as 'sha2-256'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case there are no models for tools so it was just using the adapter and directly making a request. You bring up a good question about defaults. I can see that the default values for this request are defined in the OpenAPI spec, but I'm not seeing anywhere in the client where it is applying the values. I suppose that it doesn't bother populating the values if they are not required properties since the server will default the values on that end.

@tracked hashData = '';
@tracked sum = null;
@tracked sum = '';
@tracked errorMessage = '';

@action
reset() {
this.algorithm = 'sha2-256';
this.format = 'base64';
this.hashData = '';
this.sum = null;
this.sum = '';
this.errorMessage = '';
}

@action
handleEvent(evt) {
handleEvent(evt: HTMLElementEvent<HTMLInputElement>) {
const { name, value } = evt.target;
this[name] = value;
const key = name as 'algorithm' | 'format' | 'hashData';
this[key] = value;
}

@action
async handleSubmit(evt) {
async handleSubmit(evt: HTMLElementEvent<HTMLFormElement>) {
evt.preventDefault();
const data = {
input: this.hashData,
Expand All @@ -51,11 +56,11 @@ export default class ToolsHash extends Component {
};

try {
const response = await this.store.adapterFor('tools').toolAction('hash', data);
this.sum = response.data.sum;
const { sum } = await this.api.sys.generateHash(data);
this.sum = sum || '';
this.flashMessages.success('Hash was successful.');
} catch (error) {
this.errorMessage = errorMessage(error);
this.errorMessage = await apiErrorMessage(error);
}
}
}
Loading