From 030191f8cd7e2e86e4573aed246ffe65bd022630 Mon Sep 17 00:00:00 2001 From: Kianna <30884335+kiannaquach@users.noreply.github.com> Date: Wed, 7 Feb 2024 10:43:57 -0800 Subject: [PATCH] UI: Part three bug bash custom messages (#25229) * Address comments * Fix serailizer warning mesage * Reset pageFilter when exiting * Add start and end time validation and fix bugs * Fix failing tests * Add validation tests * Set end time in contorller * Address feedback * Remove new date * Put new Date back --- ui/app/models/config-ui/message.js | 26 +++++++++- ui/app/serializers/config-ui/message.js | 2 - .../messages/message-expiration-date-form.hbs | 10 ++-- .../messages/message-expiration-date-form.js | 24 +++++++++- .../page/create-and-edit-message-form.hbs | 16 +++---- .../addon/components/messages/page/list.hbs | 6 +-- .../addon/controllers/messages/create.js | 3 +- .../config-ui/addon/routes/messages/index.js | 6 +++ ui/lib/core/addon/components/form-field.hbs | 5 +- ui/lib/core/addon/components/form-field.js | 1 + .../config-ui/messages/messages-test.js | 2 +- .../helpers/config-ui/message-selectors.js | 2 +- .../page/create-and-edit-message-test.js | 47 +++++++++++++++---- .../integration/components/form-field-test.js | 2 + 14 files changed, 113 insertions(+), 39 deletions(-) diff --git a/ui/app/models/config-ui/message.js b/ui/app/models/config-ui/message.js index 56a95cf40eb7..0edabc4270b7 100644 --- a/ui/app/models/config-ui/message.js +++ b/ui/app/models/config-ui/message.js @@ -4,7 +4,7 @@ */ import Model, { attr } from '@ember-data/model'; import lazyCapabilities, { apiPath } from 'vault/macros/lazy-capabilities'; -import { isAfter, addDays, startOfDay, parseISO } from 'date-fns'; +import { isAfter, addDays, startOfDay, parseISO, isBefore } from 'date-fns'; import { withModelValidations } from 'vault/decorators/model-validations'; import { withFormFields } from 'vault/decorators/model-form-fields'; @@ -22,6 +22,28 @@ const validations = { message: 'Link title and url are required.', }, ], + startTime: [ + { + validator(model) { + if (!model.endTime) return true; + const start = new Date(model.startTime); + const end = new Date(model.endTime); + return isBefore(start, end); + }, + message: 'Start time is after end time.', + }, + ], + endTime: [ + { + validator(model) { + if (!model.endTime) return true; + const start = new Date(model.startTime); + const end = new Date(model.endTime); + return isAfter(end, start); + }, + message: 'End time is before start time.', + }, + ], }; @withModelValidations(validations) @@ -95,7 +117,7 @@ export default class MessageModel extends Model { @attr('object', { editType: 'kv', keyPlaceholder: 'Display text (e.g. Learn more)', - valuePlaceholder: 'Link URL (e.g. https://www.learnmore.com)', + valuePlaceholder: 'Link URL (e.g. https://www.hashicorp.com/)', label: 'Link (optional)', isSingleRow: true, allowWhiteSpace: true, diff --git a/ui/app/serializers/config-ui/message.js b/ui/app/serializers/config-ui/message.js index 2333eb247550..51479944e5fc 100644 --- a/ui/app/serializers/config-ui/message.js +++ b/ui/app/serializers/config-ui/message.js @@ -9,8 +9,6 @@ import ApplicationSerializer from '../application'; export default class MessageSerializer extends ApplicationSerializer { attrs = { active: { serialize: false }, - start_time: { serialize: false }, - end_time: { serialize: false }, }; normalizeResponse(store, primaryModelClass, payload, id, requestType) { diff --git a/ui/lib/config-ui/addon/components/messages/message-expiration-date-form.hbs b/ui/lib/config-ui/addon/components/messages/message-expiration-date-form.hbs index 60c81c555fcd..aabcd72e085d 100644 --- a/ui/lib/config-ui/addon/components/messages/message-expiration-date-form.hbs +++ b/ui/lib/config-ui/addon/components/messages/message-expiration-date-form.hbs @@ -30,7 +30,7 @@ id="specificDate" value="specificDate" @value="specificDate" - @onChange={{fn (mut @message.endTime) this.formDateTime}} + @onChange={{this.specificDateChange}} @groupValue={{this.groupValue}} /> diff --git a/ui/lib/config-ui/addon/components/messages/message-expiration-date-form.js b/ui/lib/config-ui/addon/components/messages/message-expiration-date-form.js index daaff91305cb..b85e7622a415 100644 --- a/ui/lib/config-ui/addon/components/messages/message-expiration-date-form.js +++ b/ui/lib/config-ui/addon/components/messages/message-expiration-date-form.js @@ -3,6 +3,7 @@ * SPDX-License-Identifier: BUSL-1.1 */ +import { action } from '@ember/object'; import Component from '@glimmer/component'; import { tracked } from '@glimmer/tracking'; import { datetimeLocalStringFormat } from 'core/utils/date-formatters'; @@ -20,14 +21,33 @@ import { datetimeLocalStringFormat } from 'core/utils/date-formatters'; export default class MessageExpirationDateForm extends Component { datetimeLocalStringFormat = datetimeLocalStringFormat; @tracked groupValue = 'never'; - @tracked formDateTime = ''; + @tracked messageEndTime = ''; constructor() { super(...arguments); if (this.args.message.endTime) { this.groupValue = 'specificDate'; - this.formDateTime = this.args.message.endTime; + this.messageEndTime = this.args.message.endTime; } } + + get validationError() { + const validations = this.args.modelValidations || {}; + const state = validations[this.args.attr.name]; + return state && !state.isValid ? state.errors.join(' ') : null; + } + + @action + specificDateChange() { + this.groupValue = 'specificDate'; + this.args.message.endTime = this.messageEndTime; + } + + @action + onFocusOut(e) { + this.messageEndTime = e.target.value; + this.args.message.endTime = this.messageEndTime; + this.groupValue = 'specificDate'; + } } diff --git a/ui/lib/config-ui/addon/components/messages/page/create-and-edit-message-form.hbs b/ui/lib/config-ui/addon/components/messages/page/create-and-edit-message-form.hbs index 6aeffedc4e22..4ea214ef6236 100644 --- a/ui/lib/config-ui/addon/components/messages/page/create-and-edit-message-form.hbs +++ b/ui/lib/config-ui/addon/components/messages/page/create-and-edit-message-form.hbs @@ -9,21 +9,21 @@ />