open-vault/ui/lib/core/addon/components/ttl-picker.js
Jordan Reimer 5c2a08de6d
Ember Upgrade to 3.24 (#13443)
* Update browserslist

* Add browserslistrc

* ember-cli-update --to 3.26, fix conflicts

* Run codemodes that start with ember-*

* More codemods - before cp*

* More codemods (curly data-test-*)

* WIP ember-basic-dropdown template errors

* updates ember-basic-dropdown and related deps to fix build issues

* updates basic dropdown instances to new version API

* updates more deps -- ember-template-lint is working again

* runs no-implicit-this codemod

* creates and runs no-quoteless-attributes codemod

* runs angle brackets codemod

* updates lint:hbs globs to only touch hbs files

* removes yield only templates

* creates and runs deprecated args transform

* supresses lint error for invokeAction on LinkTo component

* resolves remaining ambiguous path lint errors

* resolves simple-unless lint errors

* adds warnings for deprecated tagName arg on LinkTo components

* adds warnings for remaining curly component invocation

* updates global template lint rules

* resolves remaining template lint errors

* disables some ember specfic lint rules that target pre octane patterns

* js lint fix run

* resolves remaining js lint errors

* fixes test run

* adds npm-run-all dep

* fixes test attribute issues

* fixes console acceptance tests

* fixes tests

* adds yield only wizard/tutorial-active template

* fixes more tests

* attempts to fix more flaky tests

* removes commented out settled in transit test

* updates deprecations workflow and adds initializer to filter by version

* updates flaky policies acl old test

* updates to flaky transit test

* bumps ember deps down to LTS version

* runs linters after main merge

* fixes client count tests after bad merge conflict fixes

* fixes client count history test

* more updates to lint config

* another round of hbs lint fixes after extending stylistic rule

* updates lint-staged commands

* removes indent eslint rule since it seems to break things

* fixes bad attribute in transform-edit-form template

* test fixes

* fixes enterprise tests

* adds changelog

* removes deprecated ember-concurrency-test-waiters dep and adds @ember/test-waiters

* flaky test fix

Co-authored-by: hashishaw <cshaw@hashicorp.com>
2021-12-16 20:44:29 -07:00

124 lines
3.5 KiB
JavaScript

import { typeOf } from '@ember/utils';
import EmberError from '@ember/error';
import Component from '@ember/component';
import { set, computed } from '@ember/object';
import Duration from '@icholy/duration';
import layout from '../templates/components/ttl-picker';
const ERROR_MESSAGE = 'TTLs must be specified in whole number increments, please enter a whole number.';
/**
* @module TtlPicker
* `TtlPicker` components are used to set the 'time to live'.
* This version is being deprecated and replaced by `TtlPicker2` which is an automatic-width version that
* automatically recalculates the time value when unit is updated unless time has been changed recently.
* Once all instances of TtlPicker are replaced with TtlPicker2, this component will be removed and
* TtlPicker2 will be renamed to TtlPicker.
*
* @example
* ```js
<TtlPicker @labelText="Lease" @initialValue={{lease}} @onChange={{action (mut lease)}} />
* ```
*
* @param labelClass="" {String} - A CSS class to add to the label.
* @param labelText="TTL" {String} - The text content of the label associated with the widget.
* @param initialValue=null {Number} - The starting value of the TTL;
* @param setDefaultValue=true {Boolean} - If true, the component will trigger onChange on the initial
* render, causing a value to be set.
* @param onChange=Function.prototype{Function} - The function to call when the value of the ttl changes.
* @param outputSeconds=false{Boolean} - If true, the component will trigger onChange with a value
* converted to seconds instead of a Golang duration string.
*/
export default Component.extend({
layout,
'data-test-component': 'ttl-picker',
classNames: 'field',
onChange: () => {},
setDefaultValue: true,
labelText: 'TTL',
labelClass: '',
ouputSeconds: false,
time: 30,
unit: 'm',
initialValue: null,
errorMessage: null,
unitOptions: computed(function () {
return [
{ label: 'seconds', value: 's' },
{ label: 'minutes', value: 'm' },
{ label: 'hours', value: 'h' },
{ label: 'days', value: 'd' },
];
}),
convertToSeconds(time, unit) {
const toSeconds = {
s: 1,
m: 60,
h: 3600,
};
return time * toSeconds[unit];
},
TTL: computed('outputSeconds', 'time', 'unit', function () {
let { time, unit, outputSeconds } = this;
//convert to hours
if (unit === 'd') {
time = time * 24;
unit = 'h';
}
const timeString = time + unit;
return outputSeconds ? this.convertToSeconds(time, unit) : timeString;
}),
didInsertElement() {
this._super(...arguments);
if (this.setDefaultValue === false) {
return;
}
this.onChange(this.TTL);
},
init() {
this._super(...arguments);
if (!this.onChange) {
throw new EmberError('`onChange` handler is a required attr in `' + this.toString() + '`.');
}
if (this.initialValue != undefined) {
this.parseAndSetTime();
}
},
parseAndSetTime() {
let value = this.initialValue;
let seconds = typeOf(value) === 'number' ? value : 30;
try {
seconds = Duration.parse(value).seconds();
} catch (e) {
// if parsing fails leave as default 30
}
this.set('time', seconds);
this.set('unit', 's');
},
actions: {
changedValue(key, value) {
if (value && key === 'time') {
value = parseInt(value, 10);
if (Number.isNaN(value)) {
this.set('errorMessage', ERROR_MESSAGE);
return;
}
}
this.set('errorMessage', null);
set(this, key, value);
this.onChange(this.TTL);
},
},
});