2022-06-08 14:31:22 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2022-05-30 17:10:44 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
import { inject as service } from '@ember/service';
|
2022-06-08 14:31:22 +00:00
|
|
|
import { trimPath } from '../helpers/trim-path';
|
2022-06-10 18:59:47 +00:00
|
|
|
import { copy } from 'ember-copy';
|
|
|
|
import EmberObject from '@ember/object';
|
2022-06-08 14:31:22 +00:00
|
|
|
|
2022-05-30 17:10:44 +00:00
|
|
|
export default class SecureVariableFormComponent extends Component {
|
|
|
|
@service router;
|
2022-06-17 18:05:30 +00:00
|
|
|
@service flashMessages;
|
2022-05-30 17:10:44 +00:00
|
|
|
|
2022-06-08 14:31:22 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} DuplicatePathWarning
|
|
|
|
* @property {string} path
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {DuplicatePathWarning}
|
|
|
|
*/
|
|
|
|
@tracked duplicatePathWarning = null;
|
|
|
|
|
2022-05-30 17:10:44 +00:00
|
|
|
get shouldDisableSave() {
|
|
|
|
return !this.args.model?.path;
|
|
|
|
}
|
|
|
|
|
2022-06-20 20:39:16 +00:00
|
|
|
@tracked keyValues = copy(this.args.model?.keyValues || [])?.map((kv) => {
|
2022-06-10 18:59:47 +00:00
|
|
|
return {
|
|
|
|
key: kv.key,
|
|
|
|
value: kv.value,
|
|
|
|
warnings: EmberObject.create(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-06-08 14:31:22 +00:00
|
|
|
@action
|
|
|
|
validatePath(e) {
|
|
|
|
const value = trimPath([e.target.value]);
|
2022-06-10 18:59:47 +00:00
|
|
|
const existingVariables = this.args.existingVariables || [];
|
|
|
|
let existingVariable = existingVariables
|
2022-06-08 14:31:22 +00:00
|
|
|
.without(this.args.model)
|
|
|
|
.find((v) => v.path === value);
|
|
|
|
if (existingVariable) {
|
|
|
|
this.duplicatePathWarning = {
|
|
|
|
path: existingVariable.path,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.duplicatePathWarning = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:59:47 +00:00
|
|
|
@action
|
|
|
|
validateKey(entry, e) {
|
|
|
|
const value = e.target.value;
|
|
|
|
if (value.includes('.')) {
|
|
|
|
entry.warnings.set('dottedKeyError', 'Key should not contain a period.');
|
|
|
|
} else {
|
|
|
|
delete entry.warnings.dottedKeyError;
|
|
|
|
entry.warnings.notifyPropertyChange('dottedKeyError');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 17:10:44 +00:00
|
|
|
@action appendRow() {
|
2022-06-10 18:59:47 +00:00
|
|
|
this.keyValues.pushObject({
|
2022-05-30 17:10:44 +00:00
|
|
|
key: '',
|
|
|
|
value: '',
|
2022-06-10 18:59:47 +00:00
|
|
|
warnings: EmberObject.create(),
|
2022-05-30 17:10:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action deleteRow(row) {
|
2022-06-10 18:59:47 +00:00
|
|
|
this.keyValues.removeObject(row);
|
2022-05-30 17:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
async save(e) {
|
|
|
|
e.preventDefault();
|
2022-06-17 18:05:30 +00:00
|
|
|
try {
|
2022-06-20 20:29:16 +00:00
|
|
|
const nonEmptyItems = this.keyValues.filter(
|
|
|
|
(item) => item.key.trim() && item.value
|
|
|
|
);
|
|
|
|
if (!nonEmptyItems.length) {
|
|
|
|
throw new Error('Please provide at least one key/value pair.');
|
|
|
|
} else {
|
|
|
|
this.keyValues = nonEmptyItems;
|
|
|
|
}
|
|
|
|
|
2022-06-17 18:05:30 +00:00
|
|
|
this.args.model.set('keyValues', this.keyValues);
|
|
|
|
this.args.model.setAndTrimPath();
|
|
|
|
await this.args.model.save();
|
2022-06-20 20:29:16 +00:00
|
|
|
|
2022-06-17 18:05:30 +00:00
|
|
|
this.flashMessages.add({
|
|
|
|
title: 'Secure Variable saved',
|
|
|
|
message: `${this.args.model.path} successfully saved`,
|
|
|
|
type: 'success',
|
|
|
|
destroyOnClick: false,
|
|
|
|
timeout: 5000,
|
|
|
|
showProgress: true,
|
|
|
|
});
|
2022-06-20 20:29:16 +00:00
|
|
|
this.router.transitionTo('variables.variable', this.args.model.path);
|
2022-06-17 18:05:30 +00:00
|
|
|
} catch (error) {
|
|
|
|
this.flashMessages.add({
|
|
|
|
title: `Error saving ${this.args.model.path}`,
|
|
|
|
message: error,
|
|
|
|
type: 'error',
|
|
|
|
destroyOnClick: false,
|
|
|
|
sticky: true,
|
|
|
|
});
|
|
|
|
}
|
2022-05-30 17:10:44 +00:00
|
|
|
}
|
2022-06-20 20:39:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends a row to the end of the Items list if you're editing an existing variable.
|
|
|
|
* This will allow it to auto-focus and make all other rows deletable
|
|
|
|
*/
|
|
|
|
@action appendItemIfEditing() {
|
|
|
|
if (!this.args.model?.isNew) {
|
|
|
|
this.appendRow();
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 17:10:44 +00:00
|
|
|
}
|