blob: 3d9dc3b6185f04b3b1d7d920e2f9e39fe215c1b6 [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 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"
75 button-only
76 right
77 size="sm"
78 :hide-header="true"
79 :locale="locale"
80 :label-help="
81 $t('global.calendar.useCursorKeysToNavigateCalendarDates')
82 "
83 :disabled="form.configurationSelected === 'ntp'"
84 button-variant="link"
85 aria-controls="input-manual-date"
86 >
87 <template v-slot:button-content>
88 <icon-calendar />
89 <span class="sr-only">
90 {{ $t('global.calendar.openDatePicker') }}
91 </span>
92 </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,
227 VuelidateMixin
228 ],
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500229 data() {
230 return {
231 locale: this.$store.getters['global/languagePreference'],
232 form: {
233 configurationSelected: '',
234 manual: {
235 date: '',
236 time: ''
237 },
238 ntp: { firstAddress: '', secondAddress: '', thirdAddress: '' }
239 }
240 };
241 },
242 validations() {
243 return {
244 form: {
245 manual: {
246 date: {
247 required: requiredIf(function() {
248 return this.form.configurationSelected === 'manual';
249 }),
250 pattern: helpers.regex('pattern', isoDateRegex)
251 },
252 time: {
253 required: requiredIf(function() {
254 return this.form.configurationSelected === 'manual';
255 }),
256 pattern: helpers.regex('pattern', isoTimeRegex)
257 }
258 },
259 ntp: {
260 firstAddress: {
261 required: requiredIf(function() {
262 return this.form.configurationSelected === 'ntp';
263 })
264 },
265 secondAddress: {},
266 thirdAddress: {}
267 }
268 }
269 };
270 },
271 computed: {
272 ...mapState('dateTime', ['ntpServers', 'isNtpProtocolEnabled']),
273 bmcTime() {
274 return this.$store.getters['global/bmcTime'];
Dixsie Wolmersfcda2002020-07-29 14:18:44 -0500275 },
276 isUtcDisplay() {
277 return this.$store.getters['global/isUtcDisplay'];
278 },
279 timezone() {
280 if (this.isUtcDisplay) {
281 return 'UTC';
282 }
283 return this.localOffset();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500284 }
285 },
286 watch: {
287 ntpServers() {
288 this.setNtpValues();
289 },
290 manualDate() {
291 this.emitChange();
292 }
293 },
294 created() {
295 this.startLoader();
296 Promise.all([
297 this.$store.dispatch('global/getBmcTime'),
298 this.$store.dispatch('dateTime/getNtpData')
299 ]).finally(() => this.endLoader());
300 },
301 beforeRouteLeave(to, from, next) {
302 this.hideLoader();
303 next();
304 },
305 methods: {
306 emitChange() {
307 if (this.$v.$invalid) return;
308 this.$v.$reset(); //reset to re-validate on blur
309 this.$emit('change', {
310 manualDate: this.manualDate ? new Date(this.manualDate) : null
311 });
312 },
313 setNtpValues() {
314 this.form.configurationSelected = this.isNtpProtocolEnabled
315 ? 'ntp'
316 : 'manual';
317 this.form.ntp.firstAddress = this.ntpServers[0] || '';
318 this.form.ntp.secondAddress = this.ntpServers[1] || '';
319 this.form.ntp.thirdAddress = this.ntpServers[2] || '';
320 },
321 onChangeConfigType() {
322 this.$v.form.$reset();
323 this.setNtpValues();
324 },
325 submitForm() {
326 this.$v.$touch();
327 if (this.$v.$invalid) return;
328 this.startLoader();
329
330 let dateTimeForm = {};
331 let ntpFirstAddress;
332 let ntpSecondAddress;
333 let ntpThirdAddress;
334 let isNTPEnabled = this.form.configurationSelected === 'ntp';
335
336 if (!isNTPEnabled) {
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530337 const isUtcDisplay = this.$store.getters['global/isUtcDisplay'];
338 let date;
339
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500340 dateTimeForm.ntpProtocolEnabled = false;
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530341
342 if (isUtcDisplay) {
343 // Create UTC Date
344 date = this.getUtcDate(this.form.manual.date, this.form.manual.time);
345 } else {
346 // Create local Date
347 date = new Date(`${this.form.manual.date} ${this.form.manual.time}`);
348 }
349
350 dateTimeForm.updatedDateTime = date.toISOString();
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500351 } else {
352 ntpFirstAddress = this.form.ntp.firstAddress;
353 ntpSecondAddress = this.form.ntp.secondAddress;
354 ntpThirdAddress = this.form.ntp.thirdAddress;
355 dateTimeForm.ntpProtocolEnabled = true;
356 dateTimeForm.ntpServersArray = [
357 ntpFirstAddress,
358 ntpSecondAddress,
359 ntpThirdAddress
360 ];
361 }
362
363 this.$store
364 .dispatch('dateTime/updateDateTimeSettings', dateTimeForm)
365 .then(success => {
366 this.successToast(success);
367 if (!isNTPEnabled) return;
368 // Shift address up if second address is empty
369 // to avoid refreshing after delay when updating NTP
370 if (ntpSecondAddress === '' && ntpThirdAddress !== '') {
371 this.form.ntp.secondAddress = ntpThirdAddress;
372 this.form.ntp.thirdAddress = '';
373 }
374 })
Dixsie Wolmersecd45a82020-07-14 17:07:26 -0500375 .then(() => {
376 this.$store.dispatch('global/getBmcTime');
377 })
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500378 .catch(({ message }) => this.errorToast(message))
379 .finally(() => {
380 this.$v.form.$reset();
381 this.endLoader();
382 });
Sukanya Pandeyb5c161b2020-07-30 21:56:03 +0530383 },
384 getUtcDate(date, time) {
385 // Split user input string values to create
386 // a UTC Date object
387 const datesArray = date.split('-');
388 const timeArray = time.split(':');
389 let utcDate = Date.UTC(
390 datesArray[0], // User input year
391 datesArray[1], // User input month
392 datesArray[2], // User input day
393 timeArray[0], // User input hour
394 timeArray[1] // User input minute
395 );
396 return new Date(utcDate);
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500397 }
398 }
399};
400</script>
401
402<style lang="scss" scoped>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500403.b-form-datepicker {
404 position: absolute;
405 right: 0;
406 top: 0;
407 z-index: $zindex-dropdown + 1;
408}
409</style>