blob: 9be70b8fc1c08d83fda705ebbb652e19248e4533 [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';
Yoshie Muranaka645250b2020-12-09 12:04:05 -0800212import LoadingBarMixin, { loading } from '@/components/Mixins/LoadingBarMixin';
213import 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 },
Yoshie Muranakad73f4962020-12-09 08:52:23 -0800246 loading,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500247 };
248 },
249 validations() {
250 return {
251 form: {
252 manual: {
253 date: {
Derick Montague602e98a2020-10-21 16:20:00 -0500254 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500255 return this.form.configurationSelected === 'manual';
256 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500257 pattern: helpers.regex('pattern', isoDateRegex),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500258 },
259 time: {
Derick Montague602e98a2020-10-21 16:20:00 -0500260 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500261 return this.form.configurationSelected === 'manual';
262 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500263 pattern: helpers.regex('pattern', isoTimeRegex),
264 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500265 },
266 ntp: {
267 firstAddress: {
Derick Montague602e98a2020-10-21 16:20:00 -0500268 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500269 return this.form.configurationSelected === 'ntp';
Derick Montague602e98a2020-10-21 16:20:00 -0500270 }),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500271 },
272 secondAddress: {},
Derick Montague602e98a2020-10-21 16:20:00 -0500273 thirdAddress: {},
274 },
275 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500276 };
277 },
278 computed: {
279 ...mapState('dateTime', ['ntpServers', 'isNtpProtocolEnabled']),
280 bmcTime() {
281 return this.$store.getters['global/bmcTime'];
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500282 },
283 isUtcDisplay() {
284 return this.$store.getters['global/isUtcDisplay'];
285 },
286 timezone() {
287 if (this.isUtcDisplay) {
288 return 'UTC';
289 }
290 return this.localOffset();
Derick Montague602e98a2020-10-21 16:20:00 -0500291 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500292 },
293 watch: {
294 ntpServers() {
295 this.setNtpValues();
296 },
297 manualDate() {
298 this.emitChange();
Sukanya Pandeyce33f212020-08-28 13:13:05 +0530299 },
300 bmcTime() {
301 this.form.manual.date = this.$options.filters.formatDate(
302 this.$store.getters['global/bmcTime']
303 );
304 this.form.manual.time = this.$options.filters
305 .formatTime(this.$store.getters['global/bmcTime'])
306 .slice(0, 5);
Derick Montague602e98a2020-10-21 16:20:00 -0500307 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500308 },
309 created() {
310 this.startLoader();
311 Promise.all([
312 this.$store.dispatch('global/getBmcTime'),
Derick Montague602e98a2020-10-21 16:20:00 -0500313 this.$store.dispatch('dateTime/getNtpData'),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500314 ]).finally(() => this.endLoader());
315 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500316 methods: {
317 emitChange() {
318 if (this.$v.$invalid) return;
319 this.$v.$reset(); //reset to re-validate on blur
320 this.$emit('change', {
Derick Montague602e98a2020-10-21 16:20:00 -0500321 manualDate: this.manualDate ? new Date(this.manualDate) : null,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500322 });
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,
Derick Montague602e98a2020-10-21 16:20:00 -0500370 ntpThirdAddress,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500371 ];
372 }
373
374 this.$store
375 .dispatch('dateTime/updateDateTimeSettings', dateTimeForm)
Derick Montague602e98a2020-10-21 16:20:00 -0500376 .then((success) => {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500377 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);
Derick Montague602e98a2020-10-21 16:20:00 -0500410 },
411 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500412};
413</script>