-
Notifications
You must be signed in to change notification settings - Fork 33
Icu 18005 UI create component ttl tts inputs #3091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cameronperera
wants to merge
4
commits into
llb/app-tokens
Choose a base branch
from
ICU-18005-ui-create-component-ttl-tts-inputs
base: llb/app-tokens
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
df4b25d
feat: 🎸 create variableTimeField component
cameronperera fd99cf5
test: 💍 add tests for variableTimeField component
cameronperera e5fa945
refactor: 💡 remove code in user form and controller
cameronperera 9755225
Merge branch 'llb/app-tokens' into ICU-18005-ui-create-component-ttl-…
cameronperera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| {{! | ||
| Copyright (c) HashiCorp, Inc. | ||
| SPDX-License-Identifier: BUSL-1.1 | ||
| }} | ||
| <Hds::Form::KeyValueInputs @isRequired={{@isRequired}} @data={{this.data}}> | ||
| <:header as |H|> | ||
| <H.Legend>{{@legend}}</H.Legend> | ||
| </:header> | ||
| <:row as |R|> | ||
| <R.Field as |F|> | ||
| <F.Label> | ||
| {{t 'form.variable-time-field.days'}} | ||
| </F.Label> | ||
| <F.TextInput | ||
| type='number' | ||
| name='days' | ||
| @value={{R.rowData.days}} | ||
| {{on 'input' (fn this.updateTime 'days')}} | ||
| /> | ||
| </R.Field> | ||
| <R.Field as |F|> | ||
| <F.Label> | ||
| {{t 'form.variable-time-field.hours'}} | ||
| </F.Label> | ||
| <F.TextInput | ||
| type='number' | ||
| name='hours' | ||
| @value={{R.rowData.hours}} | ||
| {{on 'input' (fn this.updateTime 'hours')}} | ||
| /> | ||
| </R.Field> | ||
| <R.Field as |F|> | ||
| <F.Label> | ||
| {{t 'form.variable-time-field.minutes'}} | ||
| </F.Label> | ||
| <F.TextInput | ||
| type='number' | ||
| name='minutes' | ||
| @value={{R.rowData.minutes}} | ||
| {{on 'input' (fn this.updateTime 'minutes')}} | ||
| /> | ||
| </R.Field> | ||
| {{#if @max}} | ||
| <R.Generic> | ||
| <Hds::Button | ||
| @text={{t 'form.variable-time-field.set-to-max'}} | ||
| variant='secondary' | ||
| {{on 'click' this.setMax}} | ||
| /> | ||
| </R.Generic> | ||
| {{/if}} | ||
| </:row> | ||
| </Hds::Form::KeyValueInputs> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * Copyright (c) HashiCorp, Inc. | ||
| * SPDX-License-Identifier: BUSL-1.1 | ||
| */ | ||
|
|
||
| import Component from '@glimmer/component'; | ||
| import { action } from '@ember/object'; | ||
| import { tracked } from '@glimmer/tracking'; | ||
|
|
||
| export default class VariableTimeFieldIndex extends Component { | ||
| @tracked days; | ||
| @tracked hours; | ||
| @tracked minutes; | ||
| @tracked data = this.createDataRow(); | ||
|
|
||
| constructor() { | ||
| super(...arguments); | ||
| this.initializeTimeFields(); | ||
| } | ||
|
|
||
| initializeTimeFields() { | ||
| let totalSeconds = this.args.time || 0; | ||
| this.days = Math.floor(totalSeconds / 86400); | ||
| totalSeconds %= 86400; | ||
| this.hours = Math.floor(totalSeconds / 3600); | ||
| totalSeconds %= 3600; | ||
| this.minutes = Math.floor(totalSeconds / 60); | ||
| this.data = this.createDataRow(); | ||
| } | ||
|
|
||
| createDataRow() { | ||
| return [{ days: this.days, hours: this.hours, minutes: this.minutes }]; | ||
| } | ||
|
|
||
| @action | ||
| updateTime(key, { target: { value } }) { | ||
| this.data[0][key] = Number(value); | ||
| const days = this.data[0].days; | ||
| const hours = this.data[0].hours; | ||
| const minutes = this.data[0].minutes; | ||
|
|
||
| let totalSeconds = | ||
| (days || 0) * 86400 + (hours || 0) * 3600 + (minutes || 0) * 60; | ||
| this.args.updateTime(totalSeconds); | ||
| } | ||
|
|
||
| @action | ||
| setMax() { | ||
| if (this.args.max == null) { | ||
| return; | ||
| } | ||
| let maxSeconds = this.args.max; | ||
| this.days = Math.floor(maxSeconds / 86400); | ||
| maxSeconds %= 86400; | ||
| this.hours = Math.floor(maxSeconds / 3600); | ||
| maxSeconds %= 3600; | ||
| this.minutes = Math.floor(maxSeconds / 60); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thoughts on converting all the values used to calculate the different units of time (e.g. 86400, 3600, 60) into |
||
| this.data = this.createDataRow(); | ||
|
|
||
| this.args.updateTime(this.args.max); | ||
| } | ||
| } | ||
142 changes: 142 additions & 0 deletions
142
ui/admin/tests/integration/components/variable-time-field/index-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /** | ||
| * Copyright (c) HashiCorp, Inc. | ||
| * SPDX-License-Identifier: BUSL-1.1 | ||
| */ | ||
|
|
||
| import { module, test } from 'qunit'; | ||
| import { setupRenderingTest } from 'admin/tests/helpers'; | ||
| import { click, fillIn, render } from '@ember/test-helpers'; | ||
| import { hbs } from 'ember-cli-htmlbars'; | ||
| import { setupIntl } from 'ember-intl/test-support'; | ||
|
|
||
| module('Integration | Component | variable-time-field/index', function (hooks) { | ||
| setupRenderingTest(hooks); | ||
| setupIntl(hooks, 'en-us'); | ||
|
|
||
| const DAYS_INPUT = 'input[name="days"]'; | ||
| const HOURS_INPUT = 'input[name="hours"]'; | ||
| const MINUTES_INPUT = 'input[name="minutes"]'; | ||
| const SET_TO_MAX_BUTTON = 'button'; | ||
| const LEGEND = 'Test Legend'; | ||
| const ONE_HOUR = 3600; // in seconds | ||
| const ONE_DAY = 86400; // in seconds | ||
|
|
||
| hooks.beforeEach(function () { | ||
| this.set('legend', LEGEND); | ||
| this.set('time', ONE_HOUR); | ||
| this.set('max', ONE_DAY); | ||
| this.set('updateTime', () => {}); | ||
| }); | ||
|
|
||
| test('it renders', async function (assert) { | ||
| await render(hbs`<VariableTimeField | ||
| @legend={{this.legend}} | ||
| @isRequired={{true}} | ||
| @time={{this.time}} | ||
| @updateTime={{this.updateTime}} | ||
| />`); | ||
|
|
||
| assert.dom('legend').containsText(LEGEND); | ||
| assert.dom(DAYS_INPUT).hasValue('0'); | ||
| assert.dom(HOURS_INPUT).hasValue('1'); | ||
| assert.dom(MINUTES_INPUT).hasValue('0'); | ||
|
|
||
| await render(hbs` | ||
| <VariableTimeField | ||
| @legend={{this.legend}} | ||
| @max={{this.max}} | ||
| @time={{this.time}} | ||
| @updateTime={{this.updateTime}} | ||
| /> | ||
| `); | ||
|
|
||
| assert.dom(SET_TO_MAX_BUTTON).hasText('Set to Max'); | ||
| assert.dom(DAYS_INPUT).hasValue('0'); | ||
| assert.dom(HOURS_INPUT).hasValue('1'); | ||
| assert.dom(MINUTES_INPUT).hasValue('0'); | ||
| }); | ||
|
|
||
| const testCases = [ | ||
| { | ||
| input: DAYS_INPUT, | ||
| value: '2', | ||
| expectedSeconds: 176400, // 2 days + 1 hour | ||
| expectedValues: { days: '2', hours: '1', minutes: '0' }, | ||
| }, | ||
| { | ||
| input: HOURS_INPUT, | ||
| value: '2', | ||
| expectedSeconds: 7200, // 2 hours | ||
| expectedValues: { days: '0', hours: '2', minutes: '0' }, | ||
| }, | ||
| { | ||
| input: MINUTES_INPUT, | ||
| value: '15', | ||
| expectedSeconds: 4500, // 2 days + 1 minute | ||
| expectedValues: { days: '0', hours: '1', minutes: '15' }, | ||
| }, | ||
| ]; | ||
|
|
||
| test.each( | ||
| 'it updates time on change', | ||
| testCases, | ||
| async function (assert, { input, value, expectedSeconds, expectedValues }) { | ||
| this.set('updateTime', (seconds) => { | ||
| if (seconds === expectedSeconds) { | ||
| assert.ok( | ||
| true, | ||
| `updateTime called with correct calculated seconds value: ${seconds}`, | ||
| ); | ||
| } else { | ||
| assert.ok( | ||
| false, | ||
| `Unexpected seconds value: ${seconds}, expected: ${expectedSeconds}`, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| await render(hbs`<VariableTimeField | ||
| @legend={{this.legend}} | ||
| @time={{this.time}} | ||
| @updateTime={{this.updateTime}} | ||
| />`); | ||
|
|
||
| assert.dom(DAYS_INPUT).hasValue('0'); | ||
| assert.dom(HOURS_INPUT).hasValue('1'); | ||
| assert.dom(MINUTES_INPUT).hasValue('0'); | ||
|
|
||
| await fillIn(input, value); | ||
|
|
||
| assert.dom(DAYS_INPUT).hasValue(expectedValues.days); | ||
| assert.dom(HOURS_INPUT).hasValue(expectedValues.hours); | ||
| assert.dom(MINUTES_INPUT).hasValue(expectedValues.minutes); | ||
| }, | ||
| ); | ||
|
|
||
| test('it sets to max time on button click', async function (assert) { | ||
| assert.expect(4); | ||
| this.set('legend', 'Test Legend'); | ||
| this.set('time', 0); | ||
| this.set('max', 90061); // 1 day, 1 hour, 1 minute, and 1 second in seconds | ||
| this.set('updateTime', (totalSeconds) => { | ||
| assert.strictEqual( | ||
| totalSeconds, | ||
| 90061, | ||
| 'updateTime called with max total seconds', | ||
| ); | ||
| }); | ||
|
|
||
| await render(hbs`<VariableTimeField | ||
| @legend={{this.legend}} | ||
| @time={{this.time}} | ||
| @max={{this.max}} | ||
| @updateTime={{this.updateTime}} | ||
| />`); | ||
|
|
||
| await click('button'); | ||
|
|
||
| assert.dom('input[name="days"]').hasValue('1'); | ||
| assert.dom('input[name="hours"]').hasValue('1'); | ||
| assert.dom('input[name="minutes"]').hasValue('1'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does
dataneed to be initialized if we already did so ininitializeTimeFieldsin the constructor