blob: 77ab430e5248d6719409306920cf2ace49ceccd3 [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>
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 Wolmersfcda2002020-07-29 14:18:44 -050027 <dt>{{ $t('pageDateTimeSettings.form.time.label') }}</dt>
Dixsie Wolmers739e4592020-06-05 07:00:06 -050028 <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 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"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +053044 data-test-id="dateTimeSettings-radio-configureManual"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050045 @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 Wolmersfcda2002020-07-29 14:18:44 -050055 <b-form-text id="date-format-help">YYYY-MM-DD</b-form-text>
Dixsie Wolmers739e4592020-06-05 07:00:06 -050056 <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 Pandeyf7c39372020-07-17 16:53:07 +053062 data-test-id="dateTimeSettings-input-manualDate"
Dixsie Wolmers5ea16782020-07-27 17:50:43 -050063 class="form-control-with-button"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050064 @blur="$v.form.manual.date.$touch()"
65 />
66 <b-form-invalid-feedback role="alert">
67 <div v-if="!$v.form.manual.date.pattern">
68 {{ $t('global.form.invalidFormat') }}
69 </div>
Dixsie Wolmers6aa9cf72020-07-20 07:46:32 -050070 <div v-if="!$v.form.manual.date.required">
Dixsie Wolmers739e4592020-06-05 07:00:06 -050071 {{ $t('global.form.fieldRequired') }}
72 </div>
73 </b-form-invalid-feedback>
74 <b-form-datepicker
75 v-model="form.manual.date"
76 button-only
77 right
78 size="sm"
79 :hide-header="true"
80 :locale="locale"
81 :label-help="
82 $t('global.calendar.useCursorKeysToNavigateCalendarDates')
83 "
84 :disabled="form.configurationSelected === 'ntp'"
85 button-variant="link"
86 aria-controls="input-manual-date"
87 >
88 <template v-slot:button-content>
Dixsie Wolmersd2ac76d2020-08-26 17:23:45 -050089 <icon-calendar
90 :title="$t('global.calendar.openDatePicker')"
91 aria-hidden="true"
92 />
Dixsie Wolmers739e4592020-06-05 07:00:06 -050093 <span class="sr-only">
94 {{ $t('global.calendar.openDatePicker') }}
95 </span>
96 </template>
97 </b-form-datepicker>
98 </b-input-group>
99 </b-form-group>
100 </b-col>
101 <b-col sm="6" lg="4" xl="3">
102 <b-form-group
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500103 :label="
104 $t('pageDateTimeSettings.form.time.timezone', { timezone })
105 "
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500106 label-for="input-manual-time"
107 >
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500108 <b-form-text id="time-format-help">HH:MM</b-form-text>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500109 <b-input-group>
110 <b-form-input
111 id="input-manual-time"
112 v-model="form.manual.time"
113 :state="getValidationState($v.form.manual.time)"
114 :disabled="form.configurationSelected === 'ntp'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530115 data-test-id="dateTimeSettings-input-manualTime"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500116 @blur="$v.form.manual.time.$touch()"
117 />
118 <b-form-invalid-feedback role="alert">
119 <div v-if="!$v.form.manual.time.pattern">
120 {{ $t('global.form.invalidFormat') }}
121 </div>
122 <div v-if="!$v.form.manual.time.required">
123 {{ $t('global.form.fieldRequired') }}
124 </div>
125 </b-form-invalid-feedback>
126 </b-input-group>
127 </b-form-group>
128 </b-col>
129 </b-row>
130 <b-form-radio
131 v-model="form.configurationSelected"
132 value="ntp"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530133 data-test-id="dateTimeSettings-radio-configureNTP"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500134 @change="onChangeConfigType"
135 >
136 NTP
137 </b-form-radio>
138 <b-row class="mt-3 ml-3">
139 <b-col sm="6" lg="4" xl="3">
140 <b-form-group
141 :label="$t('pageDateTimeSettings.form.ntpServers.server1')"
142 label-for="input-ntp-1"
143 >
144 <b-input-group>
145 <b-form-input
146 id="input-ntp-1"
147 v-model="form.ntp.firstAddress"
148 :state="getValidationState($v.form.ntp.firstAddress)"
149 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530150 data-test-id="dateTimeSettings-input-ntpServer1"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500151 @blur="$v.form.ntp.firstAddress.$touch()"
152 />
153 <b-form-invalid-feedback role="alert">
154 <div v-if="!$v.form.ntp.firstAddress.required">
155 {{ $t('global.form.fieldRequired') }}
156 </div>
157 </b-form-invalid-feedback>
158 </b-input-group>
159 </b-form-group>
160 </b-col>
161 <b-col sm="6" lg="4" xl="3">
162 <b-form-group
163 :label="$t('pageDateTimeSettings.form.ntpServers.server2')"
164 label-for="input-ntp-2"
165 >
166 <b-input-group>
167 <b-form-input
168 id="input-ntp-2"
169 v-model="form.ntp.secondAddress"
170 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530171 data-test-id="dateTimeSettings-input-ntpServer2"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500172 @blur="$v.form.ntp.secondAddress.$touch()"
173 />
174 </b-input-group>
175 </b-form-group>
176 </b-col>
177 <b-col sm="6" lg="4" xl="3">
178 <b-form-group
179 :label="$t('pageDateTimeSettings.form.ntpServers.server3')"
180 label-for="input-ntp-3"
181 >
182 <b-input-group>
183 <b-form-input
184 id="input-ntp-3"
185 v-model="form.ntp.thirdAddress"
186 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530187 data-test-id="dateTimeSettings-input-ntpServer3"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500188 @blur="$v.form.ntp.thirdAddress.$touch()"
189 />
190 </b-input-group>
191 </b-form-group>
192 </b-col>
193 </b-row>
Mateusz Gapski471f2e02020-07-27 14:43:26 +0200194 <b-button
195 variant="primary"
196 type="submit"
197 data-test-id="dateTimeSettings-button-saveSettings"
198 >
199 {{ $t('global.action.saveSettings') }}
200 </b-button>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500201 </b-form-group>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500202 </b-form>
203 </page-section>
204 </b-container>
205</template>
206
207<script>
208import Alert from '@/components/Global/Alert';
209import IconCalendar from '@carbon/icons-vue/es/calendar/20';
210import PageTitle from '@/components/Global/PageTitle';
211import PageSection from '@/components/Global/PageSection';
212
213import BVToastMixin from '@/components/Mixins/BVToastMixin';
214import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500215import LocalTimezoneLabelMixin from '@/components/Mixins/LocalTimezoneLabelMixin';
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500216import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
217
218import { mapState } from 'vuex';
219import { requiredIf, helpers } from 'vuelidate/lib/validators';
220
221const isoDateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
222const isoTimeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
223
224export default {
225 name: 'DateTimeSettings',
226 components: { Alert, IconCalendar, PageTitle, PageSection },
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500227 mixins: [
228 BVToastMixin,
229 LoadingBarMixin,
230 LocalTimezoneLabelMixin,
231 VuelidateMixin
232 ],
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500233 data() {
234 return {
235 locale: this.$store.getters['global/languagePreference'],
236 form: {
237 configurationSelected: '',
238 manual: {
239 date: '',
240 time: ''
241 },
242 ntp: { firstAddress: '', secondAddress: '', thirdAddress: '' }
243 }
244 };
245 },
246 validations() {
247 return {
248 form: {
249 manual: {
250 date: {
251 required: requiredIf(function() {
252 return this.form.configurationSelected === 'manual';
253 }),
254 pattern: helpers.regex('pattern', isoDateRegex)
255 },
256 time: {
257 required: requiredIf(function() {
258 return this.form.configurationSelected === 'manual';
259 }),
260 pattern: helpers.regex('pattern', isoTimeRegex)
261 }
262 },
263 ntp: {
264 firstAddress: {
265 required: requiredIf(function() {
266 return this.form.configurationSelected === 'ntp';
267 })
268 },
269 secondAddress: {},
270 thirdAddress: {}
271 }
272 }
273 };
274 },
275 computed: {
276 ...mapState('dateTime', ['ntpServers', 'isNtpProtocolEnabled']),
277 bmcTime() {
278 return this.$store.getters['global/bmcTime'];
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500279 },
280 isUtcDisplay() {
281 return this.$store.getters['global/isUtcDisplay'];
282 },
283 timezone() {
284 if (this.isUtcDisplay) {
285 return 'UTC';
286 }
287 return this.localOffset();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500288 }
289 },
290 watch: {
291 ntpServers() {
292 this.setNtpValues();
293 },
294 manualDate() {
295 this.emitChange();
Sukanya Pandeyce33f212020-08-28 13:13:05 +0530296 },
297 bmcTime() {
298 this.form.manual.date = this.$options.filters.formatDate(
299 this.$store.getters['global/bmcTime']
300 );
301 this.form.manual.time = this.$options.filters
302 .formatTime(this.$store.getters['global/bmcTime'])
303 .slice(0, 5);
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500304 }
305 },
306 created() {
307 this.startLoader();
308 Promise.all([
309 this.$store.dispatch('global/getBmcTime'),
310 this.$store.dispatch('dateTime/getNtpData')
311 ]).finally(() => this.endLoader());
312 },
313 beforeRouteLeave(to, from, next) {
314 this.hideLoader();
315 next();
316 },
317 methods: {
318 emitChange() {
319 if (this.$v.$invalid) return;
320 this.$v.$reset(); //reset to re-validate on blur
321 this.$emit('change', {
322 manualDate: this.manualDate ? new Date(this.manualDate) : null
323 });
324 },
325 setNtpValues() {
326 this.form.configurationSelected = this.isNtpProtocolEnabled
327 ? 'ntp'
328 : 'manual';
329 this.form.ntp.firstAddress = this.ntpServers[0] || '';
330 this.form.ntp.secondAddress = this.ntpServers[1] || '';
331 this.form.ntp.thirdAddress = this.ntpServers[2] || '';
332 },
333 onChangeConfigType() {
334 this.$v.form.$reset();
335 this.setNtpValues();
336 },
337 submitForm() {
338 this.$v.$touch();
339 if (this.$v.$invalid) return;
340 this.startLoader();
341
342 let dateTimeForm = {};
343 let ntpFirstAddress;
344 let ntpSecondAddress;
345 let ntpThirdAddress;
346 let isNTPEnabled = this.form.configurationSelected === 'ntp';
347
348 if (!isNTPEnabled) {
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530349 const isUtcDisplay = this.$store.getters['global/isUtcDisplay'];
350 let date;
351
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500352 dateTimeForm.ntpProtocolEnabled = false;
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530353
354 if (isUtcDisplay) {
355 // Create UTC Date
356 date = this.getUtcDate(this.form.manual.date, this.form.manual.time);
357 } else {
358 // Create local Date
359 date = new Date(`${this.form.manual.date} ${this.form.manual.time}`);
360 }
361
362 dateTimeForm.updatedDateTime = date.toISOString();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500363 } else {
364 ntpFirstAddress = this.form.ntp.firstAddress;
365 ntpSecondAddress = this.form.ntp.secondAddress;
366 ntpThirdAddress = this.form.ntp.thirdAddress;
367 dateTimeForm.ntpProtocolEnabled = true;
368 dateTimeForm.ntpServersArray = [
369 ntpFirstAddress,
370 ntpSecondAddress,
371 ntpThirdAddress
372 ];
373 }
374
375 this.$store
376 .dispatch('dateTime/updateDateTimeSettings', dateTimeForm)
377 .then(success => {
378 this.successToast(success);
379 if (!isNTPEnabled) return;
380 // Shift address up if second address is empty
381 // to avoid refreshing after delay when updating NTP
382 if (ntpSecondAddress === '' && ntpThirdAddress !== '') {
383 this.form.ntp.secondAddress = ntpThirdAddress;
384 this.form.ntp.thirdAddress = '';
385 }
386 })
Dixsie Wolmersecd45a82020-07-14 17:07:26 -0500387 .then(() => {
388 this.$store.dispatch('global/getBmcTime');
389 })
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500390 .catch(({ message }) => this.errorToast(message))
391 .finally(() => {
392 this.$v.form.$reset();
393 this.endLoader();
394 });
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530395 },
396 getUtcDate(date, time) {
397 // Split user input string values to create
398 // a UTC Date object
399 const datesArray = date.split('-');
400 const timeArray = time.split(':');
401 let utcDate = Date.UTC(
402 datesArray[0], // User input year
Sukanya Pandey5a705962020-08-05 00:52:28 +0530403 //UTC expects zero-index month value 0-11 (January-December)
404 //for reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC#Parameters
405 parseInt(datesArray[1]) - 1, // User input month
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530406 datesArray[2], // User input day
407 timeArray[0], // User input hour
408 timeArray[1] // User input minute
409 );
410 return new Date(utcDate);
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500411 }
412 }
413};
414</script>