blob: d1d0453900a0208b5633a967fe58d283bb8795d9 [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"
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060076 class="btn-datepicker btn-icon-only"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050077 button-only
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060078 right
Dixsie Wolmers739e4592020-06-05 07:00:06 -050079 :hide-header="true"
80 :locale="locale"
81 :label-help="
82 $t('global.calendar.useCursorKeysToNavigateCalendarDates')
83 "
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060084 :aria-label="$t('global.calendar.selectDate')"
85 :title="$t('global.calendar.selectDate')"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050086 :disabled="form.configurationSelected === 'ntp'"
87 button-variant="link"
88 aria-controls="input-manual-date"
89 >
Derick Montague602e98a2020-10-21 16:20:00 -050090 <template #button-content>
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060091 <icon-calendar />
Dixsie Wolmers739e4592020-06-05 07:00:06 -050092 </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 Wolmersfcda2002020-07-29 14:18:44 -050099 :label="
100 $t('pageDateTimeSettings.form.time.timezone', { timezone })
101 "
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500102 label-for="input-manual-time"
103 >
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500104 <b-form-text id="time-format-help">HH:MM</b-form-text>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500105 <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 Pandeyf7c39372020-07-17 16:53:07 +0530111 data-test-id="dateTimeSettings-input-manualTime"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500112 @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 Pandeyf7c39372020-07-17 16:53:07 +0530129 data-test-id="dateTimeSettings-radio-configureNTP"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500130 @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 Pandeyf7c39372020-07-17 16:53:07 +0530146 data-test-id="dateTimeSettings-input-ntpServer1"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500147 @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 Pandeyf7c39372020-07-17 16:53:07 +0530167 data-test-id="dateTimeSettings-input-ntpServer2"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500168 @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 Pandeyf7c39372020-07-17 16:53:07 +0530183 data-test-id="dateTimeSettings-input-ntpServer3"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500184 @blur="$v.form.ntp.thirdAddress.$touch()"
185 />
186 </b-input-group>
187 </b-form-group>
188 </b-col>
189 </b-row>
Mateusz Gapski471f2e02020-07-27 14:43:26 +0200190 <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 Wolmers739e4592020-06-05 07:00:06 -0500197 </b-form-group>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500198 </b-form>
199 </page-section>
200 </b-container>
201</template>
202
203<script>
204import Alert from '@/components/Global/Alert';
205import IconCalendar from '@carbon/icons-vue/es/calendar/20';
206import PageTitle from '@/components/Global/PageTitle';
207import PageSection from '@/components/Global/PageSection';
208
209import BVToastMixin from '@/components/Mixins/BVToastMixin';
210import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500211import LocalTimezoneLabelMixin from '@/components/Mixins/LocalTimezoneLabelMixin';
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500212import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
213
214import { mapState } from 'vuex';
215import { requiredIf, helpers } from 'vuelidate/lib/validators';
216
217const isoDateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
218const isoTimeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
219
220export default {
221 name: 'DateTimeSettings',
222 components: { Alert, IconCalendar, PageTitle, PageSection },
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500223 mixins: [
224 BVToastMixin,
225 LoadingBarMixin,
226 LocalTimezoneLabelMixin,
Derick Montague602e98a2020-10-21 16:20:00 -0500227 VuelidateMixin,
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500228 ],
Derick Montague602e98a2020-10-21 16:20:00 -0500229 beforeRouteLeave(to, from, next) {
230 this.hideLoader();
231 next();
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: '',
Derick Montague602e98a2020-10-21 16:20:00 -0500240 time: '',
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500241 },
Derick Montague602e98a2020-10-21 16:20:00 -0500242 ntp: { firstAddress: '', secondAddress: '', thirdAddress: '' },
243 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500244 };
245 },
246 validations() {
247 return {
248 form: {
249 manual: {
250 date: {
Derick Montague602e98a2020-10-21 16:20:00 -0500251 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500252 return this.form.configurationSelected === 'manual';
253 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500254 pattern: helpers.regex('pattern', isoDateRegex),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500255 },
256 time: {
Derick Montague602e98a2020-10-21 16:20:00 -0500257 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500258 return this.form.configurationSelected === 'manual';
259 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500260 pattern: helpers.regex('pattern', isoTimeRegex),
261 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500262 },
263 ntp: {
264 firstAddress: {
Derick Montague602e98a2020-10-21 16:20:00 -0500265 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500266 return this.form.configurationSelected === 'ntp';
Derick Montague602e98a2020-10-21 16:20:00 -0500267 }),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500268 },
269 secondAddress: {},
Derick Montague602e98a2020-10-21 16:20:00 -0500270 thirdAddress: {},
271 },
272 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500273 };
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();
Derick Montague602e98a2020-10-21 16:20:00 -0500288 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500289 },
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);
Derick Montague602e98a2020-10-21 16:20:00 -0500304 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500305 },
306 created() {
307 this.startLoader();
308 Promise.all([
309 this.$store.dispatch('global/getBmcTime'),
Derick Montague602e98a2020-10-21 16:20:00 -0500310 this.$store.dispatch('dateTime/getNtpData'),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500311 ]).finally(() => this.endLoader());
312 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500313 methods: {
314 emitChange() {
315 if (this.$v.$invalid) return;
316 this.$v.$reset(); //reset to re-validate on blur
317 this.$emit('change', {
Derick Montague602e98a2020-10-21 16:20:00 -0500318 manualDate: this.manualDate ? new Date(this.manualDate) : null,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500319 });
320 },
321 setNtpValues() {
322 this.form.configurationSelected = this.isNtpProtocolEnabled
323 ? 'ntp'
324 : 'manual';
325 this.form.ntp.firstAddress = this.ntpServers[0] || '';
326 this.form.ntp.secondAddress = this.ntpServers[1] || '';
327 this.form.ntp.thirdAddress = this.ntpServers[2] || '';
328 },
329 onChangeConfigType() {
330 this.$v.form.$reset();
331 this.setNtpValues();
332 },
333 submitForm() {
334 this.$v.$touch();
335 if (this.$v.$invalid) return;
336 this.startLoader();
337
338 let dateTimeForm = {};
339 let ntpFirstAddress;
340 let ntpSecondAddress;
341 let ntpThirdAddress;
342 let isNTPEnabled = this.form.configurationSelected === 'ntp';
343
344 if (!isNTPEnabled) {
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530345 const isUtcDisplay = this.$store.getters['global/isUtcDisplay'];
346 let date;
347
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500348 dateTimeForm.ntpProtocolEnabled = false;
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530349
350 if (isUtcDisplay) {
351 // Create UTC Date
352 date = this.getUtcDate(this.form.manual.date, this.form.manual.time);
353 } else {
354 // Create local Date
355 date = new Date(`${this.form.manual.date} ${this.form.manual.time}`);
356 }
357
358 dateTimeForm.updatedDateTime = date.toISOString();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500359 } else {
360 ntpFirstAddress = this.form.ntp.firstAddress;
361 ntpSecondAddress = this.form.ntp.secondAddress;
362 ntpThirdAddress = this.form.ntp.thirdAddress;
363 dateTimeForm.ntpProtocolEnabled = true;
364 dateTimeForm.ntpServersArray = [
365 ntpFirstAddress,
366 ntpSecondAddress,
Derick Montague602e98a2020-10-21 16:20:00 -0500367 ntpThirdAddress,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500368 ];
369 }
370
371 this.$store
372 .dispatch('dateTime/updateDateTimeSettings', dateTimeForm)
Derick Montague602e98a2020-10-21 16:20:00 -0500373 .then((success) => {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500374 this.successToast(success);
375 if (!isNTPEnabled) return;
376 // Shift address up if second address is empty
377 // to avoid refreshing after delay when updating NTP
378 if (ntpSecondAddress === '' && ntpThirdAddress !== '') {
379 this.form.ntp.secondAddress = ntpThirdAddress;
380 this.form.ntp.thirdAddress = '';
381 }
382 })
Dixsie Wolmersecd45a82020-07-14 17:07:26 -0500383 .then(() => {
384 this.$store.dispatch('global/getBmcTime');
385 })
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500386 .catch(({ message }) => this.errorToast(message))
387 .finally(() => {
388 this.$v.form.$reset();
389 this.endLoader();
390 });
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530391 },
392 getUtcDate(date, time) {
393 // Split user input string values to create
394 // a UTC Date object
395 const datesArray = date.split('-');
396 const timeArray = time.split(':');
397 let utcDate = Date.UTC(
398 datesArray[0], // User input year
Sukanya Pandey5a705962020-08-05 00:52:28 +0530399 //UTC expects zero-index month value 0-11 (January-December)
400 //for reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC#Parameters
401 parseInt(datesArray[1]) - 1, // User input month
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530402 datesArray[2], // User input day
403 timeArray[0], // User input hour
404 timeArray[1] // User input minute
405 );
406 return new Date(utcDate);
Derick Montague602e98a2020-10-21 16:20:00 -0500407 },
408 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500409};
410</script>