blob: 00d7b45a6bfc2c09b37f005c12163c1b2b7c0bbb [file] [log] [blame]
Dixsie Wolmers739e4592020-06-05 07:00:06 -05001<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>
Sandeepa Singhf67f7692021-07-19 18:04:18 +05308 {{ $t('pageDateTime.alert.message') }}
Dixsie Wolmers739e4592020-06-05 07:00:06 -05009 <b-link to="/profile-settings">
Sandeepa Singhf67f7692021-07-19 18:04:18 +053010 {{ $t('pageDateTime.alert.link') }}</b-link
Dixsie Wolmers739e4592020-06-05 07:00:06 -050011 >
12 </span>
13 </alert>
14 </b-col>
15 </b-row>
16 <page-section>
17 <b-row>
18 <b-col lg="3">
19 <dl>
Sandeepa Singhf67f7692021-07-19 18:04:18 +053020 <dt>{{ $t('pageDateTime.form.date') }}</dt>
Ed Tanous7d6b44c2024-03-23 14:56:34 -070021 <dd v-if="bmcTime">{{ bmcTime }}</dd>
Dixsie Wolmers739e4592020-06-05 07:00:06 -050022 <dd v-else>--</dd>
23 </dl>
24 </b-col>
25 <b-col lg="3">
26 <dl>
Sandeepa Singhf67f7692021-07-19 18:04:18 +053027 <dt>{{ $t('pageDateTime.form.time.label') }}</dt>
Ed Tanous7d6b44c2024-03-23 14:56:34 -070028 <dd v-if="bmcTime">{{ bmcTime }}</dd>
Dixsie Wolmers739e4592020-06-05 07:00:06 -050029 <dd v-else>--</dd>
30 </dl>
31 </b-col>
32 </b-row>
33 </page-section>
Sandeepa Singhf67f7692021-07-19 18:04:18 +053034 <page-section :section-title="$t('pageDateTime.configureSettings')">
Dixsie Wolmers739e4592020-06-05 07:00:06 -050035 <b-form novalidate @submit.prevent="submitForm">
Mateusz Gapski471f2e02020-07-27 14:43:26 +020036 <b-form-group
37 label="Configure date and time"
38 :disabled="loading"
39 label-sr-only
40 >
Dixsie Wolmers739e4592020-06-05 07:00:06 -050041 <b-form-radio
42 v-model="form.configurationSelected"
43 value="manual"
Sandeepa Singhf67f7692021-07-19 18:04:18 +053044 data-test-id="dateTime-radio-configureManual"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050045 >
Sandeepa Singhf67f7692021-07-19 18:04:18 +053046 {{ $t('pageDateTime.form.manual') }}
Dixsie Wolmers739e4592020-06-05 07:00:06 -050047 </b-form-radio>
48 <b-row class="mt-3 ml-3">
49 <b-col sm="6" lg="4" xl="3">
50 <b-form-group
Sandeepa Singhf67f7692021-07-19 18:04:18 +053051 :label="$t('pageDateTime.form.date')"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050052 label-for="input-manual-date"
53 >
Dixsie Wolmersfcda2002020-07-29 14:18:44 -050054 <b-form-text id="date-format-help">YYYY-MM-DD</b-form-text>
Dixsie Wolmers739e4592020-06-05 07:00:06 -050055 <b-input-group>
56 <b-form-input
57 id="input-manual-date"
58 v-model="form.manual.date"
59 :state="getValidationState($v.form.manual.date)"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +053060 :disabled="ntpOptionSelected"
Sandeepa Singhf67f7692021-07-19 18:04:18 +053061 data-test-id="dateTime-input-manualDate"
Dixsie Wolmers5ea16782020-07-27 17:50:43 -050062 class="form-control-with-button"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050063 @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 Wolmers6aa9cf72020-07-20 07:46:32 -050069 <div v-if="!$v.form.manual.date.required">
Dixsie Wolmers739e4592020-06-05 07:00:06 -050070 {{ $t('global.form.fieldRequired') }}
71 </div>
72 </b-form-invalid-feedback>
73 <b-form-datepicker
74 v-model="form.manual.date"
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060075 class="btn-datepicker btn-icon-only"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050076 button-only
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060077 right
Dixsie Wolmers739e4592020-06-05 07:00:06 -050078 :hide-header="true"
79 :locale="locale"
80 :label-help="
81 $t('global.calendar.useCursorKeysToNavigateCalendarDates')
82 "
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060083 :title="$t('global.calendar.selectDate')"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +053084 :disabled="ntpOptionSelected"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050085 button-variant="link"
86 aria-controls="input-manual-date"
87 >
Derick Montague602e98a2020-10-21 16:20:00 -050088 <template #button-content>
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060089 <icon-calendar />
Dixsie Wolmerse9116eb2020-11-18 19:38:18 -060090 <span class="sr-only">
91 {{ $t('global.calendar.selectDate') }}
92 </span>
Dixsie Wolmers739e4592020-06-05 07:00:06 -050093 </template>
94 </b-form-datepicker>
95 </b-input-group>
96 </b-form-group>
97 </b-col>
98 <b-col sm="6" lg="4" xl="3">
99 <b-form-group
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530100 :label="$t('pageDateTime.form.time.timezone', { timezone })"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500101 label-for="input-manual-time"
102 >
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500103 <b-form-text id="time-format-help">HH:MM</b-form-text>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500104 <b-input-group>
105 <b-form-input
106 id="input-manual-time"
107 v-model="form.manual.time"
108 :state="getValidationState($v.form.manual.time)"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530109 :disabled="ntpOptionSelected"
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530110 data-test-id="dateTime-input-manualTime"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500111 @blur="$v.form.manual.time.$touch()"
112 />
113 <b-form-invalid-feedback role="alert">
114 <div v-if="!$v.form.manual.time.pattern">
115 {{ $t('global.form.invalidFormat') }}
116 </div>
117 <div v-if="!$v.form.manual.time.required">
118 {{ $t('global.form.fieldRequired') }}
119 </div>
120 </b-form-invalid-feedback>
121 </b-input-group>
122 </b-form-group>
123 </b-col>
124 </b-row>
125 <b-form-radio
126 v-model="form.configurationSelected"
127 value="ntp"
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530128 data-test-id="dateTime-radio-configureNTP"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500129 >
130 NTP
131 </b-form-radio>
132 <b-row class="mt-3 ml-3">
133 <b-col sm="6" lg="4" xl="3">
134 <b-form-group
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530135 :label="$t('pageDateTime.form.ntpServers.server1')"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500136 label-for="input-ntp-1"
137 >
138 <b-input-group>
139 <b-form-input
140 id="input-ntp-1"
141 v-model="form.ntp.firstAddress"
142 :state="getValidationState($v.form.ntp.firstAddress)"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530143 :disabled="manualOptionSelected"
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530144 data-test-id="dateTime-input-ntpServer1"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500145 @blur="$v.form.ntp.firstAddress.$touch()"
146 />
147 <b-form-invalid-feedback role="alert">
148 <div v-if="!$v.form.ntp.firstAddress.required">
149 {{ $t('global.form.fieldRequired') }}
150 </div>
151 </b-form-invalid-feedback>
152 </b-input-group>
153 </b-form-group>
154 </b-col>
155 <b-col sm="6" lg="4" xl="3">
156 <b-form-group
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530157 :label="$t('pageDateTime.form.ntpServers.server2')"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500158 label-for="input-ntp-2"
159 >
160 <b-input-group>
161 <b-form-input
162 id="input-ntp-2"
163 v-model="form.ntp.secondAddress"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530164 :disabled="manualOptionSelected"
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530165 data-test-id="dateTime-input-ntpServer2"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500166 />
167 </b-input-group>
168 </b-form-group>
169 </b-col>
170 <b-col sm="6" lg="4" xl="3">
171 <b-form-group
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530172 :label="$t('pageDateTime.form.ntpServers.server3')"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500173 label-for="input-ntp-3"
174 >
175 <b-input-group>
176 <b-form-input
177 id="input-ntp-3"
178 v-model="form.ntp.thirdAddress"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530179 :disabled="manualOptionSelected"
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530180 data-test-id="dateTime-input-ntpServer3"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500181 />
182 </b-input-group>
183 </b-form-group>
184 </b-col>
185 </b-row>
Mateusz Gapski471f2e02020-07-27 14:43:26 +0200186 <b-button
187 variant="primary"
188 type="submit"
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530189 data-test-id="dateTime-button-saveSettings"
Mateusz Gapski471f2e02020-07-27 14:43:26 +0200190 >
191 {{ $t('global.action.saveSettings') }}
192 </b-button>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500193 </b-form-group>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500194 </b-form>
195 </page-section>
196 </b-container>
197</template>
198
199<script>
200import Alert from '@/components/Global/Alert';
201import IconCalendar from '@carbon/icons-vue/es/calendar/20';
202import PageTitle from '@/components/Global/PageTitle';
203import PageSection from '@/components/Global/PageSection';
204
205import BVToastMixin from '@/components/Mixins/BVToastMixin';
Yoshie Muranaka645250b2020-12-09 12:04:05 -0800206import LoadingBarMixin, { loading } from '@/components/Mixins/LoadingBarMixin';
207import LocalTimezoneLabelMixin from '@/components/Mixins/LocalTimezoneLabelMixin';
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500208import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
Ed Tanous7d6b44c2024-03-23 14:56:34 -0700209import { useVuelidate } from '@vuelidate/core';
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500210
211import { mapState } from 'vuex';
Ed Tanous7d6b44c2024-03-23 14:56:34 -0700212import { requiredIf, helpers } from '@vuelidate/validators';
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500213
214const isoDateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
215const isoTimeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
216
217export default {
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530218 name: 'DateTime',
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500219 components: { Alert, IconCalendar, PageTitle, PageSection },
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500220 mixins: [
221 BVToastMixin,
222 LoadingBarMixin,
223 LocalTimezoneLabelMixin,
Derick Montague602e98a2020-10-21 16:20:00 -0500224 VuelidateMixin,
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500225 ],
Derick Montague602e98a2020-10-21 16:20:00 -0500226 beforeRouteLeave(to, from, next) {
227 this.hideLoader();
228 next();
229 },
Ed Tanous7d6b44c2024-03-23 14:56:34 -0700230 setup() {
231 return {
232 v$: useVuelidate(),
233 };
234 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500235 data() {
236 return {
237 locale: this.$store.getters['global/languagePreference'],
238 form: {
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530239 configurationSelected: 'manual',
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500240 manual: {
241 date: '',
Derick Montague602e98a2020-10-21 16:20:00 -0500242 time: '',
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500243 },
Derick Montague602e98a2020-10-21 16:20:00 -0500244 ntp: { firstAddress: '', secondAddress: '', thirdAddress: '' },
245 },
Yoshie Muranakad73f4962020-12-09 08:52:23 -0800246 loading,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500247 };
248 },
249 validations() {
250 return {
251 form: {
252 manual: {
253 date: {
Derick Montague602e98a2020-10-21 16:20:00 -0500254 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500255 return this.form.configurationSelected === 'manual';
256 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500257 pattern: helpers.regex('pattern', isoDateRegex),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500258 },
259 time: {
Derick Montague602e98a2020-10-21 16:20:00 -0500260 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500261 return this.form.configurationSelected === 'manual';
262 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500263 pattern: helpers.regex('pattern', isoTimeRegex),
264 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500265 },
266 ntp: {
267 firstAddress: {
Derick Montague602e98a2020-10-21 16:20:00 -0500268 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500269 return this.form.configurationSelected === 'ntp';
Derick Montague602e98a2020-10-21 16:20:00 -0500270 }),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500271 },
Derick Montague602e98a2020-10-21 16:20:00 -0500272 },
273 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500274 };
275 },
276 computed: {
277 ...mapState('dateTime', ['ntpServers', 'isNtpProtocolEnabled']),
278 bmcTime() {
279 return this.$store.getters['global/bmcTime'];
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500280 },
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530281 ntpOptionSelected() {
282 return this.form.configurationSelected === 'ntp';
283 },
284 manualOptionSelected() {
285 return this.form.configurationSelected === 'manual';
286 },
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500287 isUtcDisplay() {
288 return this.$store.getters['global/isUtcDisplay'];
289 },
290 timezone() {
291 if (this.isUtcDisplay) {
292 return 'UTC';
293 }
294 return this.localOffset();
Derick Montague602e98a2020-10-21 16:20:00 -0500295 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500296 },
297 watch: {
298 ntpServers() {
299 this.setNtpValues();
300 },
301 manualDate() {
302 this.emitChange();
Sukanya Pandeyce33f212020-08-28 13:13:05 +0530303 },
304 bmcTime() {
305 this.form.manual.date = this.$options.filters.formatDate(
Ed Tanous81323992024-02-27 11:26:24 -0800306 this.$store.getters['global/bmcTime'],
Sukanya Pandeyce33f212020-08-28 13:13:05 +0530307 );
308 this.form.manual.time = this.$options.filters
309 .formatTime(this.$store.getters['global/bmcTime'])
310 .slice(0, 5);
Derick Montague602e98a2020-10-21 16:20:00 -0500311 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500312 },
313 created() {
314 this.startLoader();
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530315 this.setNtpValues();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500316 Promise.all([
317 this.$store.dispatch('global/getBmcTime'),
Derick Montague602e98a2020-10-21 16:20:00 -0500318 this.$store.dispatch('dateTime/getNtpData'),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500319 ]).finally(() => this.endLoader());
320 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500321 methods: {
322 emitChange() {
323 if (this.$v.$invalid) return;
324 this.$v.$reset(); //reset to re-validate on blur
325 this.$emit('change', {
Derick Montague602e98a2020-10-21 16:20:00 -0500326 manualDate: this.manualDate ? new Date(this.manualDate) : null,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500327 });
328 },
329 setNtpValues() {
330 this.form.configurationSelected = this.isNtpProtocolEnabled
331 ? 'ntp'
332 : 'manual';
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530333 [
334 this.form.ntp.firstAddress = '',
335 this.form.ntp.secondAddress = '',
336 this.form.ntp.thirdAddress = '',
337 ] = [this.ntpServers[0], this.ntpServers[1], this.ntpServers[2]];
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500338 },
339 submitForm() {
340 this.$v.$touch();
341 if (this.$v.$invalid) return;
342 this.startLoader();
343
344 let dateTimeForm = {};
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500345 let isNTPEnabled = this.form.configurationSelected === 'ntp';
346
347 if (!isNTPEnabled) {
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530348 const isUtcDisplay = this.$store.getters['global/isUtcDisplay'];
349 let date;
350
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500351 dateTimeForm.ntpProtocolEnabled = false;
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530352
353 if (isUtcDisplay) {
354 // Create UTC Date
355 date = this.getUtcDate(this.form.manual.date, this.form.manual.time);
356 } else {
357 // Create local Date
358 date = new Date(`${this.form.manual.date} ${this.form.manual.time}`);
359 }
360
361 dateTimeForm.updatedDateTime = date.toISOString();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500362 } else {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500363 dateTimeForm.ntpProtocolEnabled = true;
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530364
Kenneth84ec88f2021-11-11 14:41:04 -0600365 const ntpArray = [
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530366 this.form.ntp.firstAddress,
367 this.form.ntp.secondAddress,
368 this.form.ntp.thirdAddress,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500369 ];
Kenneth84ec88f2021-11-11 14:41:04 -0600370
371 // Filter the ntpArray to remove empty strings,
372 // per Redfish spec there should be no empty strings or null on the ntp array.
373 const ntpArrayFiltered = ntpArray.filter((x) => x);
374
375 dateTimeForm.ntpServersArray = [...ntpArrayFiltered];
376
377 [this.ntpServers[0], this.ntpServers[1], this.ntpServers[2]] = [
378 ...dateTimeForm.ntpServersArray,
379 ];
380
381 this.setNtpValues();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500382 }
383
384 this.$store
Sandeepa Singhf67f7692021-07-19 18:04:18 +0530385 .dispatch('dateTime/updateDateTime', dateTimeForm)
Derick Montague602e98a2020-10-21 16:20:00 -0500386 .then((success) => {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500387 this.successToast(success);
388 if (!isNTPEnabled) return;
389 // Shift address up if second address is empty
390 // to avoid refreshing after delay when updating NTP
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530391 if (!this.form.ntp.secondAddress && this.form.ntp.thirdAddres) {
392 this.form.ntp.secondAddress = this.form.ntp.thirdAddres;
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500393 this.form.ntp.thirdAddress = '';
394 }
395 })
Dixsie Wolmersecd45a82020-07-14 17:07:26 -0500396 .then(() => {
397 this.$store.dispatch('global/getBmcTime');
398 })
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500399 .catch(({ message }) => this.errorToast(message))
400 .finally(() => {
401 this.$v.form.$reset();
402 this.endLoader();
403 });
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530404 },
405 getUtcDate(date, time) {
406 // Split user input string values to create
407 // a UTC Date object
408 const datesArray = date.split('-');
409 const timeArray = time.split(':');
410 let utcDate = Date.UTC(
411 datesArray[0], // User input year
Sukanya Pandey5a705962020-08-05 00:52:28 +0530412 //UTC expects zero-index month value 0-11 (January-December)
413 //for reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC#Parameters
414 parseInt(datesArray[1]) - 1, // User input month
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530415 datesArray[2], // User input day
416 timeArray[0], // User input hour
Ed Tanous81323992024-02-27 11:26:24 -0800417 timeArray[1], // User input minute
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530418 );
419 return new Date(utcDate);
Derick Montague602e98a2020-10-21 16:20:00 -0500420 },
421 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500422};
423</script>