Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 1 | <template> |
| 2 | <b-container fluid="xl"> |
| 3 | <page-title /> |
| 4 | <b-row> |
| 5 | <b-col md="8" xl="6"> |
| 6 | <alert variant="info" class="mb-4"> |
| 7 | <span> |
| 8 | {{ $t('pageDateTimeSettings.alert.message') }} |
| 9 | <b-link to="/profile-settings"> |
| 10 | {{ $t('pageDateTimeSettings.alert.link') }}</b-link |
| 11 | > |
| 12 | </span> |
| 13 | </alert> |
| 14 | </b-col> |
| 15 | </b-row> |
| 16 | <page-section> |
| 17 | <b-row> |
| 18 | <b-col lg="3"> |
| 19 | <dl> |
| 20 | <dt>{{ $t('pageDateTimeSettings.form.date') }}</dt> |
| 21 | <dd v-if="bmcTime">{{ bmcTime | formatDate }}</dd> |
| 22 | <dd v-else>--</dd> |
| 23 | </dl> |
| 24 | </b-col> |
| 25 | <b-col lg="3"> |
| 26 | <dl> |
Dixsie Wolmers | fcda200 | 2020-07-29 14:18:44 -0500 | [diff] [blame] | 27 | <dt>{{ $t('pageDateTimeSettings.form.time.label') }}</dt> |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 28 | <dd v-if="bmcTime">{{ bmcTime | formatTime }}</dd> |
| 29 | <dd v-else>--</dd> |
| 30 | </dl> |
| 31 | </b-col> |
| 32 | </b-row> |
| 33 | </page-section> |
| 34 | <page-section :section-title="$t('pageDateTimeSettings.configureSettings')"> |
| 35 | <b-form novalidate @submit.prevent="submitForm"> |
Mateusz Gapski | 471f2e0 | 2020-07-27 14:43:26 +0200 | [diff] [blame] | 36 | <b-form-group |
| 37 | label="Configure date and time" |
| 38 | :disabled="loading" |
| 39 | label-sr-only |
| 40 | > |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 41 | <b-form-radio |
| 42 | v-model="form.configurationSelected" |
| 43 | value="manual" |
Sukanya Pandey | f7c3937 | 2020-07-17 16:53:07 +0530 | [diff] [blame] | 44 | data-test-id="dateTimeSettings-radio-configureManual" |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 45 | @change="onChangeConfigType" |
| 46 | > |
| 47 | {{ $t('pageDateTimeSettings.form.manual') }} |
| 48 | </b-form-radio> |
| 49 | <b-row class="mt-3 ml-3"> |
| 50 | <b-col sm="6" lg="4" xl="3"> |
| 51 | <b-form-group |
| 52 | :label="$t('pageDateTimeSettings.form.date')" |
| 53 | label-for="input-manual-date" |
| 54 | > |
Dixsie Wolmers | fcda200 | 2020-07-29 14:18:44 -0500 | [diff] [blame] | 55 | <b-form-text id="date-format-help">YYYY-MM-DD</b-form-text> |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 56 | <b-input-group> |
| 57 | <b-form-input |
| 58 | id="input-manual-date" |
| 59 | v-model="form.manual.date" |
| 60 | :state="getValidationState($v.form.manual.date)" |
| 61 | :disabled="form.configurationSelected === 'ntp'" |
Sukanya Pandey | f7c3937 | 2020-07-17 16:53:07 +0530 | [diff] [blame] | 62 | data-test-id="dateTimeSettings-input-manualDate" |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 63 | @blur="$v.form.manual.date.$touch()" |
| 64 | /> |
| 65 | <b-form-invalid-feedback role="alert"> |
| 66 | <div v-if="!$v.form.manual.date.pattern"> |
| 67 | {{ $t('global.form.invalidFormat') }} |
| 68 | </div> |
Dixsie Wolmers | 6aa9cf7 | 2020-07-20 07:46:32 -0500 | [diff] [blame] | 69 | <div v-if="!$v.form.manual.date.required"> |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 70 | {{ $t('global.form.fieldRequired') }} |
| 71 | </div> |
| 72 | </b-form-invalid-feedback> |
| 73 | <b-form-datepicker |
| 74 | v-model="form.manual.date" |
| 75 | button-only |
| 76 | right |
| 77 | size="sm" |
| 78 | :hide-header="true" |
| 79 | :locale="locale" |
| 80 | :label-help=" |
| 81 | $t('global.calendar.useCursorKeysToNavigateCalendarDates') |
| 82 | " |
| 83 | :disabled="form.configurationSelected === 'ntp'" |
| 84 | button-variant="link" |
| 85 | aria-controls="input-manual-date" |
| 86 | > |
| 87 | <template v-slot:button-content> |
| 88 | <icon-calendar /> |
| 89 | <span class="sr-only"> |
| 90 | {{ $t('global.calendar.openDatePicker') }} |
| 91 | </span> |
| 92 | </template> |
| 93 | </b-form-datepicker> |
| 94 | </b-input-group> |
| 95 | </b-form-group> |
| 96 | </b-col> |
| 97 | <b-col sm="6" lg="4" xl="3"> |
| 98 | <b-form-group |
Dixsie Wolmers | fcda200 | 2020-07-29 14:18:44 -0500 | [diff] [blame] | 99 | :label=" |
| 100 | $t('pageDateTimeSettings.form.time.timezone', { timezone }) |
| 101 | " |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 102 | label-for="input-manual-time" |
| 103 | > |
Dixsie Wolmers | fcda200 | 2020-07-29 14:18:44 -0500 | [diff] [blame] | 104 | <b-form-text id="time-format-help">HH:MM</b-form-text> |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 105 | <b-input-group> |
| 106 | <b-form-input |
| 107 | id="input-manual-time" |
| 108 | v-model="form.manual.time" |
| 109 | :state="getValidationState($v.form.manual.time)" |
| 110 | :disabled="form.configurationSelected === 'ntp'" |
Sukanya Pandey | f7c3937 | 2020-07-17 16:53:07 +0530 | [diff] [blame] | 111 | data-test-id="dateTimeSettings-input-manualTime" |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 112 | @blur="$v.form.manual.time.$touch()" |
| 113 | /> |
| 114 | <b-form-invalid-feedback role="alert"> |
| 115 | <div v-if="!$v.form.manual.time.pattern"> |
| 116 | {{ $t('global.form.invalidFormat') }} |
| 117 | </div> |
| 118 | <div v-if="!$v.form.manual.time.required"> |
| 119 | {{ $t('global.form.fieldRequired') }} |
| 120 | </div> |
| 121 | </b-form-invalid-feedback> |
| 122 | </b-input-group> |
| 123 | </b-form-group> |
| 124 | </b-col> |
| 125 | </b-row> |
| 126 | <b-form-radio |
| 127 | v-model="form.configurationSelected" |
| 128 | value="ntp" |
Sukanya Pandey | f7c3937 | 2020-07-17 16:53:07 +0530 | [diff] [blame] | 129 | data-test-id="dateTimeSettings-radio-configureNTP" |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 130 | @change="onChangeConfigType" |
| 131 | > |
| 132 | NTP |
| 133 | </b-form-radio> |
| 134 | <b-row class="mt-3 ml-3"> |
| 135 | <b-col sm="6" lg="4" xl="3"> |
| 136 | <b-form-group |
| 137 | :label="$t('pageDateTimeSettings.form.ntpServers.server1')" |
| 138 | label-for="input-ntp-1" |
| 139 | > |
| 140 | <b-input-group> |
| 141 | <b-form-input |
| 142 | id="input-ntp-1" |
| 143 | v-model="form.ntp.firstAddress" |
| 144 | :state="getValidationState($v.form.ntp.firstAddress)" |
| 145 | :disabled="form.configurationSelected === 'manual'" |
Sukanya Pandey | f7c3937 | 2020-07-17 16:53:07 +0530 | [diff] [blame] | 146 | data-test-id="dateTimeSettings-input-ntpServer1" |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 147 | @blur="$v.form.ntp.firstAddress.$touch()" |
| 148 | /> |
| 149 | <b-form-invalid-feedback role="alert"> |
| 150 | <div v-if="!$v.form.ntp.firstAddress.required"> |
| 151 | {{ $t('global.form.fieldRequired') }} |
| 152 | </div> |
| 153 | </b-form-invalid-feedback> |
| 154 | </b-input-group> |
| 155 | </b-form-group> |
| 156 | </b-col> |
| 157 | <b-col sm="6" lg="4" xl="3"> |
| 158 | <b-form-group |
| 159 | :label="$t('pageDateTimeSettings.form.ntpServers.server2')" |
| 160 | label-for="input-ntp-2" |
| 161 | > |
| 162 | <b-input-group> |
| 163 | <b-form-input |
| 164 | id="input-ntp-2" |
| 165 | v-model="form.ntp.secondAddress" |
| 166 | :disabled="form.configurationSelected === 'manual'" |
Sukanya Pandey | f7c3937 | 2020-07-17 16:53:07 +0530 | [diff] [blame] | 167 | data-test-id="dateTimeSettings-input-ntpServer2" |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 168 | @blur="$v.form.ntp.secondAddress.$touch()" |
| 169 | /> |
| 170 | </b-input-group> |
| 171 | </b-form-group> |
| 172 | </b-col> |
| 173 | <b-col sm="6" lg="4" xl="3"> |
| 174 | <b-form-group |
| 175 | :label="$t('pageDateTimeSettings.form.ntpServers.server3')" |
| 176 | label-for="input-ntp-3" |
| 177 | > |
| 178 | <b-input-group> |
| 179 | <b-form-input |
| 180 | id="input-ntp-3" |
| 181 | v-model="form.ntp.thirdAddress" |
| 182 | :disabled="form.configurationSelected === 'manual'" |
Sukanya Pandey | f7c3937 | 2020-07-17 16:53:07 +0530 | [diff] [blame] | 183 | data-test-id="dateTimeSettings-input-ntpServer3" |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 184 | @blur="$v.form.ntp.thirdAddress.$touch()" |
| 185 | /> |
| 186 | </b-input-group> |
| 187 | </b-form-group> |
| 188 | </b-col> |
| 189 | </b-row> |
Mateusz Gapski | 471f2e0 | 2020-07-27 14:43:26 +0200 | [diff] [blame] | 190 | <b-button |
| 191 | variant="primary" |
| 192 | type="submit" |
| 193 | data-test-id="dateTimeSettings-button-saveSettings" |
| 194 | > |
| 195 | {{ $t('global.action.saveSettings') }} |
| 196 | </b-button> |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 197 | </b-form-group> |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 198 | </b-form> |
| 199 | </page-section> |
| 200 | </b-container> |
| 201 | </template> |
| 202 | |
| 203 | <script> |
| 204 | import Alert from '@/components/Global/Alert'; |
| 205 | import IconCalendar from '@carbon/icons-vue/es/calendar/20'; |
| 206 | import PageTitle from '@/components/Global/PageTitle'; |
| 207 | import PageSection from '@/components/Global/PageSection'; |
| 208 | |
| 209 | import BVToastMixin from '@/components/Mixins/BVToastMixin'; |
| 210 | import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin'; |
Dixsie Wolmers | fcda200 | 2020-07-29 14:18:44 -0500 | [diff] [blame] | 211 | import LocalTimezoneLabelMixin from '@/components/Mixins/LocalTimezoneLabelMixin'; |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 212 | import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js'; |
| 213 | |
| 214 | import { mapState } from 'vuex'; |
| 215 | import { requiredIf, helpers } from 'vuelidate/lib/validators'; |
| 216 | |
| 217 | const isoDateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/; |
| 218 | const isoTimeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/; |
| 219 | |
| 220 | export default { |
| 221 | name: 'DateTimeSettings', |
| 222 | components: { Alert, IconCalendar, PageTitle, PageSection }, |
Dixsie Wolmers | fcda200 | 2020-07-29 14:18:44 -0500 | [diff] [blame] | 223 | mixins: [ |
| 224 | BVToastMixin, |
| 225 | LoadingBarMixin, |
| 226 | LocalTimezoneLabelMixin, |
| 227 | VuelidateMixin |
| 228 | ], |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 229 | data() { |
| 230 | return { |
| 231 | locale: this.$store.getters['global/languagePreference'], |
| 232 | form: { |
| 233 | configurationSelected: '', |
| 234 | manual: { |
| 235 | date: '', |
| 236 | time: '' |
| 237 | }, |
| 238 | ntp: { firstAddress: '', secondAddress: '', thirdAddress: '' } |
| 239 | } |
| 240 | }; |
| 241 | }, |
| 242 | validations() { |
| 243 | return { |
| 244 | form: { |
| 245 | manual: { |
| 246 | date: { |
| 247 | required: requiredIf(function() { |
| 248 | return this.form.configurationSelected === 'manual'; |
| 249 | }), |
| 250 | pattern: helpers.regex('pattern', isoDateRegex) |
| 251 | }, |
| 252 | time: { |
| 253 | required: requiredIf(function() { |
| 254 | return this.form.configurationSelected === 'manual'; |
| 255 | }), |
| 256 | pattern: helpers.regex('pattern', isoTimeRegex) |
| 257 | } |
| 258 | }, |
| 259 | ntp: { |
| 260 | firstAddress: { |
| 261 | required: requiredIf(function() { |
| 262 | return this.form.configurationSelected === 'ntp'; |
| 263 | }) |
| 264 | }, |
| 265 | secondAddress: {}, |
| 266 | thirdAddress: {} |
| 267 | } |
| 268 | } |
| 269 | }; |
| 270 | }, |
| 271 | computed: { |
| 272 | ...mapState('dateTime', ['ntpServers', 'isNtpProtocolEnabled']), |
| 273 | bmcTime() { |
| 274 | return this.$store.getters['global/bmcTime']; |
Dixsie Wolmers | fcda200 | 2020-07-29 14:18:44 -0500 | [diff] [blame] | 275 | }, |
| 276 | isUtcDisplay() { |
| 277 | return this.$store.getters['global/isUtcDisplay']; |
| 278 | }, |
| 279 | timezone() { |
| 280 | if (this.isUtcDisplay) { |
| 281 | return 'UTC'; |
| 282 | } |
| 283 | return this.localOffset(); |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 284 | } |
| 285 | }, |
| 286 | watch: { |
| 287 | ntpServers() { |
| 288 | this.setNtpValues(); |
| 289 | }, |
| 290 | manualDate() { |
| 291 | this.emitChange(); |
| 292 | } |
| 293 | }, |
| 294 | created() { |
| 295 | this.startLoader(); |
| 296 | Promise.all([ |
| 297 | this.$store.dispatch('global/getBmcTime'), |
| 298 | this.$store.dispatch('dateTime/getNtpData') |
| 299 | ]).finally(() => this.endLoader()); |
| 300 | }, |
| 301 | beforeRouteLeave(to, from, next) { |
| 302 | this.hideLoader(); |
| 303 | next(); |
| 304 | }, |
| 305 | methods: { |
| 306 | emitChange() { |
| 307 | if (this.$v.$invalid) return; |
| 308 | this.$v.$reset(); //reset to re-validate on blur |
| 309 | this.$emit('change', { |
| 310 | manualDate: this.manualDate ? new Date(this.manualDate) : null |
| 311 | }); |
| 312 | }, |
| 313 | setNtpValues() { |
| 314 | this.form.configurationSelected = this.isNtpProtocolEnabled |
| 315 | ? 'ntp' |
| 316 | : 'manual'; |
| 317 | this.form.ntp.firstAddress = this.ntpServers[0] || ''; |
| 318 | this.form.ntp.secondAddress = this.ntpServers[1] || ''; |
| 319 | this.form.ntp.thirdAddress = this.ntpServers[2] || ''; |
| 320 | }, |
| 321 | onChangeConfigType() { |
| 322 | this.$v.form.$reset(); |
| 323 | this.setNtpValues(); |
| 324 | }, |
| 325 | submitForm() { |
| 326 | this.$v.$touch(); |
| 327 | if (this.$v.$invalid) return; |
| 328 | this.startLoader(); |
| 329 | |
| 330 | let dateTimeForm = {}; |
| 331 | let ntpFirstAddress; |
| 332 | let ntpSecondAddress; |
| 333 | let ntpThirdAddress; |
| 334 | let isNTPEnabled = this.form.configurationSelected === 'ntp'; |
| 335 | |
| 336 | if (!isNTPEnabled) { |
Sukanya Pandey | b5c161b | 2020-07-30 21:56:03 +0530 | [diff] [blame] | 337 | const isUtcDisplay = this.$store.getters['global/isUtcDisplay']; |
| 338 | let date; |
| 339 | |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 340 | dateTimeForm.ntpProtocolEnabled = false; |
Sukanya Pandey | b5c161b | 2020-07-30 21:56:03 +0530 | [diff] [blame] | 341 | |
| 342 | if (isUtcDisplay) { |
| 343 | // Create UTC Date |
| 344 | date = this.getUtcDate(this.form.manual.date, this.form.manual.time); |
| 345 | } else { |
| 346 | // Create local Date |
| 347 | date = new Date(`${this.form.manual.date} ${this.form.manual.time}`); |
| 348 | } |
| 349 | |
| 350 | dateTimeForm.updatedDateTime = date.toISOString(); |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 351 | } else { |
| 352 | ntpFirstAddress = this.form.ntp.firstAddress; |
| 353 | ntpSecondAddress = this.form.ntp.secondAddress; |
| 354 | ntpThirdAddress = this.form.ntp.thirdAddress; |
| 355 | dateTimeForm.ntpProtocolEnabled = true; |
| 356 | dateTimeForm.ntpServersArray = [ |
| 357 | ntpFirstAddress, |
| 358 | ntpSecondAddress, |
| 359 | ntpThirdAddress |
| 360 | ]; |
| 361 | } |
| 362 | |
| 363 | this.$store |
| 364 | .dispatch('dateTime/updateDateTimeSettings', dateTimeForm) |
| 365 | .then(success => { |
| 366 | this.successToast(success); |
| 367 | if (!isNTPEnabled) return; |
| 368 | // Shift address up if second address is empty |
| 369 | // to avoid refreshing after delay when updating NTP |
| 370 | if (ntpSecondAddress === '' && ntpThirdAddress !== '') { |
| 371 | this.form.ntp.secondAddress = ntpThirdAddress; |
| 372 | this.form.ntp.thirdAddress = ''; |
| 373 | } |
| 374 | }) |
Dixsie Wolmers | ecd45a8 | 2020-07-14 17:07:26 -0500 | [diff] [blame] | 375 | .then(() => { |
| 376 | this.$store.dispatch('global/getBmcTime'); |
| 377 | }) |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 378 | .catch(({ message }) => this.errorToast(message)) |
| 379 | .finally(() => { |
| 380 | this.$v.form.$reset(); |
| 381 | this.endLoader(); |
| 382 | }); |
Sukanya Pandey | b5c161b | 2020-07-30 21:56:03 +0530 | [diff] [blame] | 383 | }, |
| 384 | getUtcDate(date, time) { |
| 385 | // Split user input string values to create |
| 386 | // a UTC Date object |
| 387 | const datesArray = date.split('-'); |
| 388 | const timeArray = time.split(':'); |
| 389 | let utcDate = Date.UTC( |
| 390 | datesArray[0], // User input year |
Sukanya Pandey | 5a70596 | 2020-08-05 00:52:28 +0530 | [diff] [blame^] | 391 | //UTC expects zero-index month value 0-11 (January-December) |
| 392 | //for reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC#Parameters |
| 393 | parseInt(datesArray[1]) - 1, // User input month |
Sukanya Pandey | b5c161b | 2020-07-30 21:56:03 +0530 | [diff] [blame] | 394 | datesArray[2], // User input day |
| 395 | timeArray[0], // User input hour |
| 396 | timeArray[1] // User input minute |
| 397 | ); |
| 398 | return new Date(utcDate); |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | }; |
| 402 | </script> |
| 403 | |
| 404 | <style lang="scss" scoped> |
Dixsie Wolmers | 739e459 | 2020-06-05 07:00:06 -0500 | [diff] [blame] | 405 | .b-form-datepicker { |
| 406 | position: absolute; |
| 407 | right: 0; |
| 408 | top: 0; |
| 409 | z-index: $zindex-dropdown + 1; |
| 410 | } |
| 411 | </style> |