blob: 33e097c4df6c9275efd7465e2f8f99caf1b06548 [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"
SurenNewaredc618a82020-08-17 18:42:20 +053076 class="input-action"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050077 button-only
Dixsie Wolmers739e4592020-06-05 07:00:06 -050078 :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>
Dixsie Wolmersd2ac76d2020-08-26 17:23:45 -050088 <icon-calendar
89 :title="$t('global.calendar.openDatePicker')"
90 aria-hidden="true"
91 />
Dixsie Wolmers739e4592020-06-05 07:00:06 -050092 <span class="sr-only">
93 {{ $t('global.calendar.openDatePicker') }}
94 </span>
95 </template>
96 </b-form-datepicker>
97 </b-input-group>
98 </b-form-group>
99 </b-col>
100 <b-col sm="6" lg="4" xl="3">
101 <b-form-group
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500102 :label="
103 $t('pageDateTimeSettings.form.time.timezone', { timezone })
104 "
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500105 label-for="input-manual-time"
106 >
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500107 <b-form-text id="time-format-help">HH:MM</b-form-text>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500108 <b-input-group>
109 <b-form-input
110 id="input-manual-time"
111 v-model="form.manual.time"
112 :state="getValidationState($v.form.manual.time)"
113 :disabled="form.configurationSelected === 'ntp'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530114 data-test-id="dateTimeSettings-input-manualTime"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500115 @blur="$v.form.manual.time.$touch()"
116 />
117 <b-form-invalid-feedback role="alert">
118 <div v-if="!$v.form.manual.time.pattern">
119 {{ $t('global.form.invalidFormat') }}
120 </div>
121 <div v-if="!$v.form.manual.time.required">
122 {{ $t('global.form.fieldRequired') }}
123 </div>
124 </b-form-invalid-feedback>
125 </b-input-group>
126 </b-form-group>
127 </b-col>
128 </b-row>
129 <b-form-radio
130 v-model="form.configurationSelected"
131 value="ntp"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530132 data-test-id="dateTimeSettings-radio-configureNTP"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500133 @change="onChangeConfigType"
134 >
135 NTP
136 </b-form-radio>
137 <b-row class="mt-3 ml-3">
138 <b-col sm="6" lg="4" xl="3">
139 <b-form-group
140 :label="$t('pageDateTimeSettings.form.ntpServers.server1')"
141 label-for="input-ntp-1"
142 >
143 <b-input-group>
144 <b-form-input
145 id="input-ntp-1"
146 v-model="form.ntp.firstAddress"
147 :state="getValidationState($v.form.ntp.firstAddress)"
148 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530149 data-test-id="dateTimeSettings-input-ntpServer1"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500150 @blur="$v.form.ntp.firstAddress.$touch()"
151 />
152 <b-form-invalid-feedback role="alert">
153 <div v-if="!$v.form.ntp.firstAddress.required">
154 {{ $t('global.form.fieldRequired') }}
155 </div>
156 </b-form-invalid-feedback>
157 </b-input-group>
158 </b-form-group>
159 </b-col>
160 <b-col sm="6" lg="4" xl="3">
161 <b-form-group
162 :label="$t('pageDateTimeSettings.form.ntpServers.server2')"
163 label-for="input-ntp-2"
164 >
165 <b-input-group>
166 <b-form-input
167 id="input-ntp-2"
168 v-model="form.ntp.secondAddress"
169 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530170 data-test-id="dateTimeSettings-input-ntpServer2"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500171 @blur="$v.form.ntp.secondAddress.$touch()"
172 />
173 </b-input-group>
174 </b-form-group>
175 </b-col>
176 <b-col sm="6" lg="4" xl="3">
177 <b-form-group
178 :label="$t('pageDateTimeSettings.form.ntpServers.server3')"
179 label-for="input-ntp-3"
180 >
181 <b-input-group>
182 <b-form-input
183 id="input-ntp-3"
184 v-model="form.ntp.thirdAddress"
185 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530186 data-test-id="dateTimeSettings-input-ntpServer3"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500187 @blur="$v.form.ntp.thirdAddress.$touch()"
188 />
189 </b-input-group>
190 </b-form-group>
191 </b-col>
192 </b-row>
Mateusz Gapski471f2e02020-07-27 14:43:26 +0200193 <b-button
194 variant="primary"
195 type="submit"
196 data-test-id="dateTimeSettings-button-saveSettings"
197 >
198 {{ $t('global.action.saveSettings') }}
199 </b-button>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500200 </b-form-group>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500201 </b-form>
202 </page-section>
203 </b-container>
204</template>
205
206<script>
207import Alert from '@/components/Global/Alert';
208import IconCalendar from '@carbon/icons-vue/es/calendar/20';
209import PageTitle from '@/components/Global/PageTitle';
210import PageSection from '@/components/Global/PageSection';
211
212import BVToastMixin from '@/components/Mixins/BVToastMixin';
213import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500214import LocalTimezoneLabelMixin from '@/components/Mixins/LocalTimezoneLabelMixin';
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500215import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
216
217import { mapState } from 'vuex';
218import { requiredIf, helpers } from 'vuelidate/lib/validators';
219
220const isoDateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
221const isoTimeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
222
223export default {
224 name: 'DateTimeSettings',
225 components: { Alert, IconCalendar, PageTitle, PageSection },
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500226 mixins: [
227 BVToastMixin,
228 LoadingBarMixin,
229 LocalTimezoneLabelMixin,
230 VuelidateMixin
231 ],
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500232 data() {
233 return {
234 locale: this.$store.getters['global/languagePreference'],
235 form: {
236 configurationSelected: '',
237 manual: {
238 date: '',
239 time: ''
240 },
241 ntp: { firstAddress: '', secondAddress: '', thirdAddress: '' }
242 }
243 };
244 },
245 validations() {
246 return {
247 form: {
248 manual: {
249 date: {
250 required: requiredIf(function() {
251 return this.form.configurationSelected === 'manual';
252 }),
253 pattern: helpers.regex('pattern', isoDateRegex)
254 },
255 time: {
256 required: requiredIf(function() {
257 return this.form.configurationSelected === 'manual';
258 }),
259 pattern: helpers.regex('pattern', isoTimeRegex)
260 }
261 },
262 ntp: {
263 firstAddress: {
264 required: requiredIf(function() {
265 return this.form.configurationSelected === 'ntp';
266 })
267 },
268 secondAddress: {},
269 thirdAddress: {}
270 }
271 }
272 };
273 },
274 computed: {
275 ...mapState('dateTime', ['ntpServers', 'isNtpProtocolEnabled']),
276 bmcTime() {
277 return this.$store.getters['global/bmcTime'];
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500278 },
279 isUtcDisplay() {
280 return this.$store.getters['global/isUtcDisplay'];
281 },
282 timezone() {
283 if (this.isUtcDisplay) {
284 return 'UTC';
285 }
286 return this.localOffset();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500287 }
288 },
289 watch: {
290 ntpServers() {
291 this.setNtpValues();
292 },
293 manualDate() {
294 this.emitChange();
Sukanya Pandeyce33f212020-08-28 13:13:05 +0530295 },
296 bmcTime() {
297 this.form.manual.date = this.$options.filters.formatDate(
298 this.$store.getters['global/bmcTime']
299 );
300 this.form.manual.time = this.$options.filters
301 .formatTime(this.$store.getters['global/bmcTime'])
302 .slice(0, 5);
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500303 }
304 },
305 created() {
306 this.startLoader();
307 Promise.all([
308 this.$store.dispatch('global/getBmcTime'),
309 this.$store.dispatch('dateTime/getNtpData')
310 ]).finally(() => this.endLoader());
311 },
312 beforeRouteLeave(to, from, next) {
313 this.hideLoader();
314 next();
315 },
316 methods: {
317 emitChange() {
318 if (this.$v.$invalid) return;
319 this.$v.$reset(); //reset to re-validate on blur
320 this.$emit('change', {
321 manualDate: this.manualDate ? new Date(this.manualDate) : null
322 });
323 },
324 setNtpValues() {
325 this.form.configurationSelected = this.isNtpProtocolEnabled
326 ? 'ntp'
327 : 'manual';
328 this.form.ntp.firstAddress = this.ntpServers[0] || '';
329 this.form.ntp.secondAddress = this.ntpServers[1] || '';
330 this.form.ntp.thirdAddress = this.ntpServers[2] || '';
331 },
332 onChangeConfigType() {
333 this.$v.form.$reset();
334 this.setNtpValues();
335 },
336 submitForm() {
337 this.$v.$touch();
338 if (this.$v.$invalid) return;
339 this.startLoader();
340
341 let dateTimeForm = {};
342 let ntpFirstAddress;
343 let ntpSecondAddress;
344 let ntpThirdAddress;
345 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 {
363 ntpFirstAddress = this.form.ntp.firstAddress;
364 ntpSecondAddress = this.form.ntp.secondAddress;
365 ntpThirdAddress = this.form.ntp.thirdAddress;
366 dateTimeForm.ntpProtocolEnabled = true;
367 dateTimeForm.ntpServersArray = [
368 ntpFirstAddress,
369 ntpSecondAddress,
370 ntpThirdAddress
371 ];
372 }
373
374 this.$store
375 .dispatch('dateTime/updateDateTimeSettings', dateTimeForm)
376 .then(success => {
377 this.successToast(success);
378 if (!isNTPEnabled) return;
379 // Shift address up if second address is empty
380 // to avoid refreshing after delay when updating NTP
381 if (ntpSecondAddress === '' && ntpThirdAddress !== '') {
382 this.form.ntp.secondAddress = ntpThirdAddress;
383 this.form.ntp.thirdAddress = '';
384 }
385 })
Dixsie Wolmersecd45a82020-07-14 17:07:26 -0500386 .then(() => {
387 this.$store.dispatch('global/getBmcTime');
388 })
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500389 .catch(({ message }) => this.errorToast(message))
390 .finally(() => {
391 this.$v.form.$reset();
392 this.endLoader();
393 });
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530394 },
395 getUtcDate(date, time) {
396 // Split user input string values to create
397 // a UTC Date object
398 const datesArray = date.split('-');
399 const timeArray = time.split(':');
400 let utcDate = Date.UTC(
401 datesArray[0], // User input year
Sukanya Pandey5a705962020-08-05 00:52:28 +0530402 //UTC expects zero-index month value 0-11 (January-December)
403 //for reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC#Parameters
404 parseInt(datesArray[1]) - 1, // User input month
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530405 datesArray[2], // User input day
406 timeArray[0], // User input hour
407 timeArray[1] // User input minute
408 );
409 return new Date(utcDate);
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500410 }
411 }
412};
413</script>