blob: 613953fe6429098bcf3e527baf074536f5f982f1 [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 >
46 {{ $t('pageDateTimeSettings.form.manual') }}
47 </b-form-radio>
48 <b-row class="mt-3 ml-3">
49 <b-col sm="6" lg="4" xl="3">
50 <b-form-group
51 :label="$t('pageDateTimeSettings.form.date')"
52 label-for="input-manual-date"
53 >
Dixsie Wolmersfcda2002020-07-29 14:18:44 -050054 <b-form-text id="date-format-help">YYYY-MM-DD</b-form-text>
Dixsie Wolmers739e4592020-06-05 07:00:06 -050055 <b-input-group>
56 <b-form-input
57 id="input-manual-date"
58 v-model="form.manual.date"
59 :state="getValidationState($v.form.manual.date)"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +053060 :disabled="ntpOptionSelected"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +053061 data-test-id="dateTimeSettings-input-manualDate"
Dixsie Wolmers5ea16782020-07-27 17:50:43 -050062 class="form-control-with-button"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050063 @blur="$v.form.manual.date.$touch()"
64 />
65 <b-form-invalid-feedback role="alert">
66 <div v-if="!$v.form.manual.date.pattern">
67 {{ $t('global.form.invalidFormat') }}
68 </div>
Dixsie Wolmers6aa9cf72020-07-20 07:46:32 -050069 <div v-if="!$v.form.manual.date.required">
Dixsie Wolmers739e4592020-06-05 07:00:06 -050070 {{ $t('global.form.fieldRequired') }}
71 </div>
72 </b-form-invalid-feedback>
73 <b-form-datepicker
74 v-model="form.manual.date"
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060075 class="btn-datepicker btn-icon-only"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050076 button-only
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060077 right
Dixsie Wolmers739e4592020-06-05 07:00:06 -050078 :hide-header="true"
79 :locale="locale"
80 :label-help="
81 $t('global.calendar.useCursorKeysToNavigateCalendarDates')
82 "
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060083 :title="$t('global.calendar.selectDate')"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +053084 :disabled="ntpOptionSelected"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050085 button-variant="link"
86 aria-controls="input-manual-date"
87 >
Derick Montague602e98a2020-10-21 16:20:00 -050088 <template #button-content>
Dixsie Wolmers30f11f82020-11-10 16:07:56 -060089 <icon-calendar />
Dixsie Wolmerse9116eb2020-11-18 19:38:18 -060090 <span class="sr-only">
91 {{ $t('global.calendar.selectDate') }}
92 </span>
Dixsie Wolmers739e4592020-06-05 07:00:06 -050093 </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)"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530111 :disabled="ntpOptionSelected"
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 >
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)"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530145 :disabled="manualOptionSelected"
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"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530166 :disabled="manualOptionSelected"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530167 data-test-id="dateTimeSettings-input-ntpServer2"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500168 />
169 </b-input-group>
170 </b-form-group>
171 </b-col>
172 <b-col sm="6" lg="4" xl="3">
173 <b-form-group
174 :label="$t('pageDateTimeSettings.form.ntpServers.server3')"
175 label-for="input-ntp-3"
176 >
177 <b-input-group>
178 <b-form-input
179 id="input-ntp-3"
180 v-model="form.ntp.thirdAddress"
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530181 :disabled="manualOptionSelected"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530182 data-test-id="dateTimeSettings-input-ntpServer3"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500183 />
184 </b-input-group>
185 </b-form-group>
186 </b-col>
187 </b-row>
Mateusz Gapski471f2e02020-07-27 14:43:26 +0200188 <b-button
189 variant="primary"
190 type="submit"
191 data-test-id="dateTimeSettings-button-saveSettings"
192 >
193 {{ $t('global.action.saveSettings') }}
194 </b-button>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500195 </b-form-group>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500196 </b-form>
197 </page-section>
198 </b-container>
199</template>
200
201<script>
202import Alert from '@/components/Global/Alert';
203import IconCalendar from '@carbon/icons-vue/es/calendar/20';
204import PageTitle from '@/components/Global/PageTitle';
205import PageSection from '@/components/Global/PageSection';
206
207import BVToastMixin from '@/components/Mixins/BVToastMixin';
Yoshie Muranaka645250b2020-12-09 12:04:05 -0800208import LoadingBarMixin, { loading } from '@/components/Mixins/LoadingBarMixin';
209import LocalTimezoneLabelMixin from '@/components/Mixins/LocalTimezoneLabelMixin';
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500210import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
211
212import { mapState } from 'vuex';
213import { requiredIf, helpers } from 'vuelidate/lib/validators';
214
215const isoDateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
216const isoTimeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
217
218export default {
219 name: 'DateTimeSettings',
220 components: { Alert, IconCalendar, PageTitle, PageSection },
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500221 mixins: [
222 BVToastMixin,
223 LoadingBarMixin,
224 LocalTimezoneLabelMixin,
Derick Montague602e98a2020-10-21 16:20:00 -0500225 VuelidateMixin,
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500226 ],
Derick Montague602e98a2020-10-21 16:20:00 -0500227 beforeRouteLeave(to, from, next) {
228 this.hideLoader();
229 next();
230 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500231 data() {
232 return {
233 locale: this.$store.getters['global/languagePreference'],
234 form: {
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530235 configurationSelected: 'manual',
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500236 manual: {
237 date: '',
Derick Montague602e98a2020-10-21 16:20:00 -0500238 time: '',
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500239 },
Derick Montague602e98a2020-10-21 16:20:00 -0500240 ntp: { firstAddress: '', secondAddress: '', thirdAddress: '' },
241 },
Yoshie Muranakad73f4962020-12-09 08:52:23 -0800242 loading,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500243 };
244 },
245 validations() {
246 return {
247 form: {
248 manual: {
249 date: {
Derick Montague602e98a2020-10-21 16:20:00 -0500250 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500251 return this.form.configurationSelected === 'manual';
252 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500253 pattern: helpers.regex('pattern', isoDateRegex),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500254 },
255 time: {
Derick Montague602e98a2020-10-21 16:20:00 -0500256 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500257 return this.form.configurationSelected === 'manual';
258 }),
Derick Montague602e98a2020-10-21 16:20:00 -0500259 pattern: helpers.regex('pattern', isoTimeRegex),
260 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500261 },
262 ntp: {
263 firstAddress: {
Derick Montague602e98a2020-10-21 16:20:00 -0500264 required: requiredIf(function () {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500265 return this.form.configurationSelected === 'ntp';
Derick Montague602e98a2020-10-21 16:20:00 -0500266 }),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500267 },
Derick Montague602e98a2020-10-21 16:20:00 -0500268 },
269 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500270 };
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 },
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530277 ntpOptionSelected() {
278 return this.form.configurationSelected === 'ntp';
279 },
280 manualOptionSelected() {
281 return this.form.configurationSelected === 'manual';
282 },
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500283 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();
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530311 this.setNtpValues();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500312 Promise.all([
313 this.$store.dispatch('global/getBmcTime'),
Derick Montague602e98a2020-10-21 16:20:00 -0500314 this.$store.dispatch('dateTime/getNtpData'),
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500315 ]).finally(() => this.endLoader());
316 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500317 methods: {
318 emitChange() {
319 if (this.$v.$invalid) return;
320 this.$v.$reset(); //reset to re-validate on blur
321 this.$emit('change', {
Derick Montague602e98a2020-10-21 16:20:00 -0500322 manualDate: this.manualDate ? new Date(this.manualDate) : null,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500323 });
324 },
325 setNtpValues() {
326 this.form.configurationSelected = this.isNtpProtocolEnabled
327 ? 'ntp'
328 : 'manual';
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530329 [
330 this.form.ntp.firstAddress = '',
331 this.form.ntp.secondAddress = '',
332 this.form.ntp.thirdAddress = '',
333 ] = [this.ntpServers[0], this.ntpServers[1], this.ntpServers[2]];
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500334 },
335 submitForm() {
336 this.$v.$touch();
337 if (this.$v.$invalid) return;
338 this.startLoader();
339
340 let dateTimeForm = {};
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500341 let isNTPEnabled = this.form.configurationSelected === 'ntp';
342
343 if (!isNTPEnabled) {
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530344 const isUtcDisplay = this.$store.getters['global/isUtcDisplay'];
345 let date;
346
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500347 dateTimeForm.ntpProtocolEnabled = false;
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530348
349 if (isUtcDisplay) {
350 // Create UTC Date
351 date = this.getUtcDate(this.form.manual.date, this.form.manual.time);
352 } else {
353 // Create local Date
354 date = new Date(`${this.form.manual.date} ${this.form.manual.time}`);
355 }
356
357 dateTimeForm.updatedDateTime = date.toISOString();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500358 } else {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500359 dateTimeForm.ntpProtocolEnabled = true;
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530360
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500361 dateTimeForm.ntpServersArray = [
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530362 this.form.ntp.firstAddress,
363 this.form.ntp.secondAddress,
364 this.form.ntp.thirdAddress,
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500365 ];
366 }
367
368 this.$store
369 .dispatch('dateTime/updateDateTimeSettings', dateTimeForm)
Derick Montague602e98a2020-10-21 16:20:00 -0500370 .then((success) => {
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500371 this.successToast(success);
372 if (!isNTPEnabled) return;
373 // Shift address up if second address is empty
374 // to avoid refreshing after delay when updating NTP
Sukanya Pandey6b5ff102021-05-10 14:07:15 +0530375 if (!this.form.ntp.secondAddress && this.form.ntp.thirdAddres) {
376 this.form.ntp.secondAddress = this.form.ntp.thirdAddres;
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500377 this.form.ntp.thirdAddress = '';
378 }
379 })
Dixsie Wolmersecd45a82020-07-14 17:07:26 -0500380 .then(() => {
381 this.$store.dispatch('global/getBmcTime');
382 })
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500383 .catch(({ message }) => this.errorToast(message))
384 .finally(() => {
385 this.$v.form.$reset();
386 this.endLoader();
387 });
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530388 },
389 getUtcDate(date, time) {
390 // Split user input string values to create
391 // a UTC Date object
392 const datesArray = date.split('-');
393 const timeArray = time.split(':');
394 let utcDate = Date.UTC(
395 datesArray[0], // User input year
Sukanya Pandey5a705962020-08-05 00:52:28 +0530396 //UTC expects zero-index month value 0-11 (January-December)
397 //for reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC#Parameters
398 parseInt(datesArray[1]) - 1, // User input month
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530399 datesArray[2], // User input day
400 timeArray[0], // User input hour
401 timeArray[1] // User input minute
402 );
403 return new Date(utcDate);
Derick Montague602e98a2020-10-21 16:20:00 -0500404 },
405 },
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500406};
407</script>