blob: d3c19d571c0641620f6d5021f1a3746cfa5f4b56 [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 :title="$t('global.calendar.selectDate')"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050085 :disabled="form.configurationSelected === 'ntp'"
86 button-variant="link"
87 aria-controls="input-manual-date"
88 >
Derick Montague602e98a2020-10-21 16:20:00 -050089 <template #button-content>
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060090 <icon-calendar />
Dixsie Wolmerse9116eb2020-11-18 19:38:18 -060091 <span class="sr-only">
92 {{ $t('global.calendar.selectDate') }}
93 </span>
Dixsie Wolmers739e4592020-06-05 07:00:06 -050094 </template>
95 </b-form-datepicker>
96 </b-input-group>
97 </b-form-group>
98 </b-col>
99 <b-col sm="6" lg="4" xl="3">
100 <b-form-group
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500101 :label="
102 $t('pageDateTimeSettings.form.time.timezone', { timezone })
103 "
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500104 label-for="input-manual-time"
105 >
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500106 <b-form-text id="time-format-help">HH:MM</b-form-text>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500107 <b-input-group>
108 <b-form-input
109 id="input-manual-time"
110 v-model="form.manual.time"
111 :state="getValidationState($v.form.manual.time)"
112 :disabled="form.configurationSelected === 'ntp'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530113 data-test-id="dateTimeSettings-input-manualTime"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500114 @blur="$v.form.manual.time.$touch()"
115 />
116 <b-form-invalid-feedback role="alert">
117 <div v-if="!$v.form.manual.time.pattern">
118 {{ $t('global.form.invalidFormat') }}
119 </div>
120 <div v-if="!$v.form.manual.time.required">
121 {{ $t('global.form.fieldRequired') }}
122 </div>
123 </b-form-invalid-feedback>
124 </b-input-group>
125 </b-form-group>
126 </b-col>
127 </b-row>
128 <b-form-radio
129 v-model="form.configurationSelected"
130 value="ntp"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530131 data-test-id="dateTimeSettings-radio-configureNTP"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500132 @change="onChangeConfigType"
133 >
134 NTP
135 </b-form-radio>
136 <b-row class="mt-3 ml-3">
137 <b-col sm="6" lg="4" xl="3">
138 <b-form-group
139 :label="$t('pageDateTimeSettings.form.ntpServers.server1')"
140 label-for="input-ntp-1"
141 >
142 <b-input-group>
143 <b-form-input
144 id="input-ntp-1"
145 v-model="form.ntp.firstAddress"
146 :state="getValidationState($v.form.ntp.firstAddress)"
147 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530148 data-test-id="dateTimeSettings-input-ntpServer1"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500149 @blur="$v.form.ntp.firstAddress.$touch()"
150 />
151 <b-form-invalid-feedback role="alert">
152 <div v-if="!$v.form.ntp.firstAddress.required">
153 {{ $t('global.form.fieldRequired') }}
154 </div>
155 </b-form-invalid-feedback>
156 </b-input-group>
157 </b-form-group>
158 </b-col>
159 <b-col sm="6" lg="4" xl="3">
160 <b-form-group
161 :label="$t('pageDateTimeSettings.form.ntpServers.server2')"
162 label-for="input-ntp-2"
163 >
164 <b-input-group>
165 <b-form-input
166 id="input-ntp-2"
167 v-model="form.ntp.secondAddress"
168 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530169 data-test-id="dateTimeSettings-input-ntpServer2"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500170 @blur="$v.form.ntp.secondAddress.$touch()"
171 />
172 </b-input-group>
173 </b-form-group>
174 </b-col>
175 <b-col sm="6" lg="4" xl="3">
176 <b-form-group
177 :label="$t('pageDateTimeSettings.form.ntpServers.server3')"
178 label-for="input-ntp-3"
179 >
180 <b-input-group>
181 <b-form-input
182 id="input-ntp-3"
183 v-model="form.ntp.thirdAddress"
184 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530185 data-test-id="dateTimeSettings-input-ntpServer3"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500186 @blur="$v.form.ntp.thirdAddress.$touch()"
187 />
188 </b-input-group>
189 </b-form-group>
190 </b-col>
191 </b-row>
Mateusz Gapski471f2e02020-07-27 14:43:26 +0200192 <b-button
193 variant="primary"
194 type="submit"
195 data-test-id="dateTimeSettings-button-saveSettings"
196 >
197 {{ $t('global.action.saveSettings') }}
198 </b-button>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500199 </b-form-group>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500200 </b-form>
201 </page-section>
202 </b-container>
203</template>
204
205<script>
206import Alert from '@/components/Global/Alert';
207import IconCalendar from '@carbon/icons-vue/es/calendar/20';
208import PageTitle from '@/components/Global/PageTitle';
209import PageSection from '@/components/Global/PageSection';
210
211import BVToastMixin from '@/components/Mixins/BVToastMixin';
212import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500213import LocalTimezoneLabelMixin from '@/components/Mixins/LocalTimezoneLabelMixin';
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500214import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
215
216import { mapState } from 'vuex';
217import { requiredIf, helpers } from 'vuelidate/lib/validators';
218
219const isoDateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
220const isoTimeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
221
222export default {
223 name: 'DateTimeSettings',
224 components: { Alert, IconCalendar, PageTitle, PageSection },
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500225 mixins: [
226 BVToastMixin,
227 LoadingBarMixin,
228 LocalTimezoneLabelMixin,
Derick Montague602e98a2020-10-21 16:20:00 -0500229 VuelidateMixin,
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500230 ],
Derick Montague602e98a2020-10-21 16:20:00 -0500231 beforeRouteLeave(to, from, next) {
232 this.hideLoader();
233 next();
234 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500235 data() {
236 return {
237 locale: this.$store.getters['global/languagePreference'],
238 form: {
239 configurationSelected: '',
240 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 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500246 };
247 },
248 validations() {
249 return {
250 form: {
251 manual: {
252 date: {
Derick Montague602e98a2020-10-21 16:20:00 -0500253 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500254 return this.form.configurationSelected === 'manual';
255 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500256 pattern: helpers.regex('pattern', isoDateRegex),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500257 },
258 time: {
Derick Montague602e98a2020-10-21 16:20:00 -0500259 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500260 return this.form.configurationSelected === 'manual';
261 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500262 pattern: helpers.regex('pattern', isoTimeRegex),
263 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500264 },
265 ntp: {
266 firstAddress: {
Derick Montague602e98a2020-10-21 16:20:00 -0500267 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500268 return this.form.configurationSelected === 'ntp';
Derick Montague602e98a2020-10-21 16:20:00 -0500269 }),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500270 },
271 secondAddress: {},
Derick Montague602e98a2020-10-21 16:20:00 -0500272 thirdAddress: {},
273 },
274 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500275 };
276 },
277 computed: {
278 ...mapState('dateTime', ['ntpServers', 'isNtpProtocolEnabled']),
279 bmcTime() {
280 return this.$store.getters['global/bmcTime'];
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500281 },
282 isUtcDisplay() {
283 return this.$store.getters['global/isUtcDisplay'];
284 },
285 timezone() {
286 if (this.isUtcDisplay) {
287 return 'UTC';
288 }
289 return this.localOffset();
Derick Montague602e98a2020-10-21 16:20:00 -0500290 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500291 },
292 watch: {
293 ntpServers() {
294 this.setNtpValues();
295 },
296 manualDate() {
297 this.emitChange();
Sukanya Pandeyce33f212020-08-28 13:13:05 +0530298 },
299 bmcTime() {
300 this.form.manual.date = this.$options.filters.formatDate(
301 this.$store.getters['global/bmcTime']
302 );
303 this.form.manual.time = this.$options.filters
304 .formatTime(this.$store.getters['global/bmcTime'])
305 .slice(0, 5);
Derick Montague602e98a2020-10-21 16:20:00 -0500306 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500307 },
308 created() {
309 this.startLoader();
310 Promise.all([
311 this.$store.dispatch('global/getBmcTime'),
Derick Montague602e98a2020-10-21 16:20:00 -0500312 this.$store.dispatch('dateTime/getNtpData'),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500313 ]).finally(() => this.endLoader());
314 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500315 methods: {
316 emitChange() {
317 if (this.$v.$invalid) return;
318 this.$v.$reset(); //reset to re-validate on blur
319 this.$emit('change', {
Derick Montague602e98a2020-10-21 16:20:00 -0500320 manualDate: this.manualDate ? new Date(this.manualDate) : null,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500321 });
322 },
323 setNtpValues() {
324 this.form.configurationSelected = this.isNtpProtocolEnabled
325 ? 'ntp'
326 : 'manual';
327 this.form.ntp.firstAddress = this.ntpServers[0] || '';
328 this.form.ntp.secondAddress = this.ntpServers[1] || '';
329 this.form.ntp.thirdAddress = this.ntpServers[2] || '';
330 },
331 onChangeConfigType() {
332 this.$v.form.$reset();
333 this.setNtpValues();
334 },
335 submitForm() {
336 this.$v.$touch();
337 if (this.$v.$invalid) return;
338 this.startLoader();
339
340 let dateTimeForm = {};
341 let ntpFirstAddress;
342 let ntpSecondAddress;
343 let ntpThirdAddress;
344 let isNTPEnabled = this.form.configurationSelected === 'ntp';
345
346 if (!isNTPEnabled) {
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530347 const isUtcDisplay = this.$store.getters['global/isUtcDisplay'];
348 let date;
349
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500350 dateTimeForm.ntpProtocolEnabled = false;
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530351
352 if (isUtcDisplay) {
353 // Create UTC Date
354 date = this.getUtcDate(this.form.manual.date, this.form.manual.time);
355 } else {
356 // Create local Date
357 date = new Date(`${this.form.manual.date} ${this.form.manual.time}`);
358 }
359
360 dateTimeForm.updatedDateTime = date.toISOString();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500361 } else {
362 ntpFirstAddress = this.form.ntp.firstAddress;
363 ntpSecondAddress = this.form.ntp.secondAddress;
364 ntpThirdAddress = this.form.ntp.thirdAddress;
365 dateTimeForm.ntpProtocolEnabled = true;
366 dateTimeForm.ntpServersArray = [
367 ntpFirstAddress,
368 ntpSecondAddress,
Derick Montague602e98a2020-10-21 16:20:00 -0500369 ntpThirdAddress,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500370 ];
371 }
372
373 this.$store
374 .dispatch('dateTime/updateDateTimeSettings', dateTimeForm)
Derick Montague602e98a2020-10-21 16:20:00 -0500375 .then((success) => {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500376 this.successToast(success);
377 if (!isNTPEnabled) return;
378 // Shift address up if second address is empty
379 // to avoid refreshing after delay when updating NTP
380 if (ntpSecondAddress === '' && ntpThirdAddress !== '') {
381 this.form.ntp.secondAddress = ntpThirdAddress;
382 this.form.ntp.thirdAddress = '';
383 }
384 })
Dixsie Wolmersecd45a82020-07-14 17:07:26 -0500385 .then(() => {
386 this.$store.dispatch('global/getBmcTime');
387 })
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500388 .catch(({ message }) => this.errorToast(message))
389 .finally(() => {
390 this.$v.form.$reset();
391 this.endLoader();
392 });
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530393 },
394 getUtcDate(date, time) {
395 // Split user input string values to create
396 // a UTC Date object
397 const datesArray = date.split('-');
398 const timeArray = time.split(':');
399 let utcDate = Date.UTC(
400 datesArray[0], // User input year
Sukanya Pandey5a705962020-08-05 00:52:28 +0530401 //UTC expects zero-index month value 0-11 (January-December)
402 //for reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC#Parameters
403 parseInt(datesArray[1]) - 1, // User input month
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530404 datesArray[2], // User input day
405 timeArray[0], // User input hour
406 timeArray[1] // User input minute
407 );
408 return new Date(utcDate);
Derick Montague602e98a2020-10-21 16:20:00 -0500409 },
410 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500411};
412</script>