blob: b6bc815109772931c33d48eb98d70aff8047ae3e [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>
89 <icon-calendar />
90 <span class="sr-only">
91 {{ $t('global.calendar.openDatePicker') }}
92 </span>
93 </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
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500100 :label="
101 $t('pageDateTimeSettings.form.time.timezone', { timezone })
102 "
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500103 label-for="input-manual-time"
104 >
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500105 <b-form-text id="time-format-help">HH:MM</b-form-text>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500106 <b-input-group>
107 <b-form-input
108 id="input-manual-time"
109 v-model="form.manual.time"
110 :state="getValidationState($v.form.manual.time)"
111 :disabled="form.configurationSelected === 'ntp'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530112 data-test-id="dateTimeSettings-input-manualTime"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500113 @blur="$v.form.manual.time.$touch()"
114 />
115 <b-form-invalid-feedback role="alert">
116 <div v-if="!$v.form.manual.time.pattern">
117 {{ $t('global.form.invalidFormat') }}
118 </div>
119 <div v-if="!$v.form.manual.time.required">
120 {{ $t('global.form.fieldRequired') }}
121 </div>
122 </b-form-invalid-feedback>
123 </b-input-group>
124 </b-form-group>
125 </b-col>
126 </b-row>
127 <b-form-radio
128 v-model="form.configurationSelected"
129 value="ntp"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530130 data-test-id="dateTimeSettings-radio-configureNTP"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500131 @change="onChangeConfigType"
132 >
133 NTP
134 </b-form-radio>
135 <b-row class="mt-3 ml-3">
136 <b-col sm="6" lg="4" xl="3">
137 <b-form-group
138 :label="$t('pageDateTimeSettings.form.ntpServers.server1')"
139 label-for="input-ntp-1"
140 >
141 <b-input-group>
142 <b-form-input
143 id="input-ntp-1"
144 v-model="form.ntp.firstAddress"
145 :state="getValidationState($v.form.ntp.firstAddress)"
146 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530147 data-test-id="dateTimeSettings-input-ntpServer1"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500148 @blur="$v.form.ntp.firstAddress.$touch()"
149 />
150 <b-form-invalid-feedback role="alert">
151 <div v-if="!$v.form.ntp.firstAddress.required">
152 {{ $t('global.form.fieldRequired') }}
153 </div>
154 </b-form-invalid-feedback>
155 </b-input-group>
156 </b-form-group>
157 </b-col>
158 <b-col sm="6" lg="4" xl="3">
159 <b-form-group
160 :label="$t('pageDateTimeSettings.form.ntpServers.server2')"
161 label-for="input-ntp-2"
162 >
163 <b-input-group>
164 <b-form-input
165 id="input-ntp-2"
166 v-model="form.ntp.secondAddress"
167 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530168 data-test-id="dateTimeSettings-input-ntpServer2"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500169 @blur="$v.form.ntp.secondAddress.$touch()"
170 />
171 </b-input-group>
172 </b-form-group>
173 </b-col>
174 <b-col sm="6" lg="4" xl="3">
175 <b-form-group
176 :label="$t('pageDateTimeSettings.form.ntpServers.server3')"
177 label-for="input-ntp-3"
178 >
179 <b-input-group>
180 <b-form-input
181 id="input-ntp-3"
182 v-model="form.ntp.thirdAddress"
183 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530184 data-test-id="dateTimeSettings-input-ntpServer3"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500185 @blur="$v.form.ntp.thirdAddress.$touch()"
186 />
187 </b-input-group>
188 </b-form-group>
189 </b-col>
190 </b-row>
Mateusz Gapski471f2e02020-07-27 14:43:26 +0200191 <b-button
192 variant="primary"
193 type="submit"
194 data-test-id="dateTimeSettings-button-saveSettings"
195 >
196 {{ $t('global.action.saveSettings') }}
197 </b-button>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500198 </b-form-group>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500199 </b-form>
200 </page-section>
201 </b-container>
202</template>
203
204<script>
205import Alert from '@/components/Global/Alert';
206import IconCalendar from '@carbon/icons-vue/es/calendar/20';
207import PageTitle from '@/components/Global/PageTitle';
208import PageSection from '@/components/Global/PageSection';
209
210import BVToastMixin from '@/components/Mixins/BVToastMixin';
211import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500212import LocalTimezoneLabelMixin from '@/components/Mixins/LocalTimezoneLabelMixin';
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500213import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
214
215import { mapState } from 'vuex';
216import { requiredIf, helpers } from 'vuelidate/lib/validators';
217
218const isoDateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
219const isoTimeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
220
221export default {
222 name: 'DateTimeSettings',
223 components: { Alert, IconCalendar, PageTitle, PageSection },
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500224 mixins: [
225 BVToastMixin,
226 LoadingBarMixin,
227 LocalTimezoneLabelMixin,
228 VuelidateMixin
229 ],
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500230 data() {
231 return {
232 locale: this.$store.getters['global/languagePreference'],
233 form: {
234 configurationSelected: '',
235 manual: {
236 date: '',
237 time: ''
238 },
239 ntp: { firstAddress: '', secondAddress: '', thirdAddress: '' }
240 }
241 };
242 },
243 validations() {
244 return {
245 form: {
246 manual: {
247 date: {
248 required: requiredIf(function() {
249 return this.form.configurationSelected === 'manual';
250 }),
251 pattern: helpers.regex('pattern', isoDateRegex)
252 },
253 time: {
254 required: requiredIf(function() {
255 return this.form.configurationSelected === 'manual';
256 }),
257 pattern: helpers.regex('pattern', isoTimeRegex)
258 }
259 },
260 ntp: {
261 firstAddress: {
262 required: requiredIf(function() {
263 return this.form.configurationSelected === 'ntp';
264 })
265 },
266 secondAddress: {},
267 thirdAddress: {}
268 }
269 }
270 };
271 },
272 computed: {
273 ...mapState('dateTime', ['ntpServers', 'isNtpProtocolEnabled']),
274 bmcTime() {
275 return this.$store.getters['global/bmcTime'];
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500276 },
277 isUtcDisplay() {
278 return this.$store.getters['global/isUtcDisplay'];
279 },
280 timezone() {
281 if (this.isUtcDisplay) {
282 return 'UTC';
283 }
284 return this.localOffset();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500285 }
286 },
287 watch: {
288 ntpServers() {
289 this.setNtpValues();
290 },
291 manualDate() {
292 this.emitChange();
293 }
294 },
295 created() {
296 this.startLoader();
297 Promise.all([
298 this.$store.dispatch('global/getBmcTime'),
299 this.$store.dispatch('dateTime/getNtpData')
300 ]).finally(() => this.endLoader());
301 },
302 beforeRouteLeave(to, from, next) {
303 this.hideLoader();
304 next();
305 },
306 methods: {
307 emitChange() {
308 if (this.$v.$invalid) return;
309 this.$v.$reset(); //reset to re-validate on blur
310 this.$emit('change', {
311 manualDate: this.manualDate ? new Date(this.manualDate) : null
312 });
313 },
314 setNtpValues() {
315 this.form.configurationSelected = this.isNtpProtocolEnabled
316 ? 'ntp'
317 : 'manual';
318 this.form.ntp.firstAddress = this.ntpServers[0] || '';
319 this.form.ntp.secondAddress = this.ntpServers[1] || '';
320 this.form.ntp.thirdAddress = this.ntpServers[2] || '';
321 },
322 onChangeConfigType() {
323 this.$v.form.$reset();
324 this.setNtpValues();
325 },
326 submitForm() {
327 this.$v.$touch();
328 if (this.$v.$invalid) return;
329 this.startLoader();
330
331 let dateTimeForm = {};
332 let ntpFirstAddress;
333 let ntpSecondAddress;
334 let ntpThirdAddress;
335 let isNTPEnabled = this.form.configurationSelected === 'ntp';
336
337 if (!isNTPEnabled) {
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530338 const isUtcDisplay = this.$store.getters['global/isUtcDisplay'];
339 let date;
340
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500341 dateTimeForm.ntpProtocolEnabled = false;
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530342
343 if (isUtcDisplay) {
344 // Create UTC Date
345 date = this.getUtcDate(this.form.manual.date, this.form.manual.time);
346 } else {
347 // Create local Date
348 date = new Date(`${this.form.manual.date} ${this.form.manual.time}`);
349 }
350
351 dateTimeForm.updatedDateTime = date.toISOString();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500352 } else {
353 ntpFirstAddress = this.form.ntp.firstAddress;
354 ntpSecondAddress = this.form.ntp.secondAddress;
355 ntpThirdAddress = this.form.ntp.thirdAddress;
356 dateTimeForm.ntpProtocolEnabled = true;
357 dateTimeForm.ntpServersArray = [
358 ntpFirstAddress,
359 ntpSecondAddress,
360 ntpThirdAddress
361 ];
362 }
363
364 this.$store
365 .dispatch('dateTime/updateDateTimeSettings', dateTimeForm)
366 .then(success => {
367 this.successToast(success);
368 if (!isNTPEnabled) return;
369 // Shift address up if second address is empty
370 // to avoid refreshing after delay when updating NTP
371 if (ntpSecondAddress === '' && ntpThirdAddress !== '') {
372 this.form.ntp.secondAddress = ntpThirdAddress;
373 this.form.ntp.thirdAddress = '';
374 }
375 })
Dixsie Wolmersecd45a82020-07-14 17:07:26 -0500376 .then(() => {
377 this.$store.dispatch('global/getBmcTime');
378 })
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500379 .catch(({ message }) => this.errorToast(message))
380 .finally(() => {
381 this.$v.form.$reset();
382 this.endLoader();
383 });
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530384 },
385 getUtcDate(date, time) {
386 // Split user input string values to create
387 // a UTC Date object
388 const datesArray = date.split('-');
389 const timeArray = time.split(':');
390 let utcDate = Date.UTC(
391 datesArray[0], // User input year
Sukanya Pandey5a705962020-08-05 00:52:28 +0530392 //UTC expects zero-index month value 0-11 (January-December)
393 //for reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC#Parameters
394 parseInt(datesArray[1]) - 1, // User input month
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530395 datesArray[2], // User input day
396 timeArray[0], // User input hour
397 timeArray[1] // User input minute
398 );
399 return new Date(utcDate);
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500400 }
401 }
402};
403</script>