blob: 2e3fc1957e5cdc2a0930f885702a89a2697cf0c0 [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>
27 <dt>{{ $t('pageDateTimeSettings.form.time') }}</dt>
28 <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 >
55 <b-form-text id="date-format-help">(YYYY-MM-DD)</b-form-text>
56 <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
99 :label="$t('pageDateTimeSettings.form.time')"
100 label-for="input-manual-time"
101 >
102 <b-form-text id="time-format-help">(HH:MM)</b-form-text>
103 <b-input-group>
104 <b-form-input
105 id="input-manual-time"
106 v-model="form.manual.time"
107 :state="getValidationState($v.form.manual.time)"
108 :disabled="form.configurationSelected === 'ntp'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530109 data-test-id="dateTimeSettings-input-manualTime"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500110 @blur="$v.form.manual.time.$touch()"
111 />
112 <b-form-invalid-feedback role="alert">
113 <div v-if="!$v.form.manual.time.pattern">
114 {{ $t('global.form.invalidFormat') }}
115 </div>
116 <div v-if="!$v.form.manual.time.required">
117 {{ $t('global.form.fieldRequired') }}
118 </div>
119 </b-form-invalid-feedback>
120 </b-input-group>
121 </b-form-group>
122 </b-col>
123 </b-row>
124 <b-form-radio
125 v-model="form.configurationSelected"
126 value="ntp"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530127 data-test-id="dateTimeSettings-radio-configureNTP"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500128 @change="onChangeConfigType"
129 >
130 NTP
131 </b-form-radio>
132 <b-row class="mt-3 ml-3">
133 <b-col sm="6" lg="4" xl="3">
134 <b-form-group
135 :label="$t('pageDateTimeSettings.form.ntpServers.server1')"
136 label-for="input-ntp-1"
137 >
138 <b-input-group>
139 <b-form-input
140 id="input-ntp-1"
141 v-model="form.ntp.firstAddress"
142 :state="getValidationState($v.form.ntp.firstAddress)"
143 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530144 data-test-id="dateTimeSettings-input-ntpServer1"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500145 @blur="$v.form.ntp.firstAddress.$touch()"
146 />
147 <b-form-invalid-feedback role="alert">
148 <div v-if="!$v.form.ntp.firstAddress.required">
149 {{ $t('global.form.fieldRequired') }}
150 </div>
151 </b-form-invalid-feedback>
152 </b-input-group>
153 </b-form-group>
154 </b-col>
155 <b-col sm="6" lg="4" xl="3">
156 <b-form-group
157 :label="$t('pageDateTimeSettings.form.ntpServers.server2')"
158 label-for="input-ntp-2"
159 >
160 <b-input-group>
161 <b-form-input
162 id="input-ntp-2"
163 v-model="form.ntp.secondAddress"
164 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530165 data-test-id="dateTimeSettings-input-ntpServer2"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500166 @blur="$v.form.ntp.secondAddress.$touch()"
167 />
168 </b-input-group>
169 </b-form-group>
170 </b-col>
171 <b-col sm="6" lg="4" xl="3">
172 <b-form-group
173 :label="$t('pageDateTimeSettings.form.ntpServers.server3')"
174 label-for="input-ntp-3"
175 >
176 <b-input-group>
177 <b-form-input
178 id="input-ntp-3"
179 v-model="form.ntp.thirdAddress"
180 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530181 data-test-id="dateTimeSettings-input-ntpServer3"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500182 @blur="$v.form.ntp.thirdAddress.$touch()"
183 />
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';
208import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
209import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
210
211import { mapState } from 'vuex';
212import { requiredIf, helpers } from 'vuelidate/lib/validators';
213
214const isoDateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
215const isoTimeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
216
217export default {
218 name: 'DateTimeSettings',
219 components: { Alert, IconCalendar, PageTitle, PageSection },
220 mixins: [BVToastMixin, LoadingBarMixin, VuelidateMixin],
221 data() {
222 return {
223 locale: this.$store.getters['global/languagePreference'],
224 form: {
225 configurationSelected: '',
226 manual: {
227 date: '',
228 time: ''
229 },
230 ntp: { firstAddress: '', secondAddress: '', thirdAddress: '' }
231 }
232 };
233 },
234 validations() {
235 return {
236 form: {
237 manual: {
238 date: {
239 required: requiredIf(function() {
240 return this.form.configurationSelected === 'manual';
241 }),
242 pattern: helpers.regex('pattern', isoDateRegex)
243 },
244 time: {
245 required: requiredIf(function() {
246 return this.form.configurationSelected === 'manual';
247 }),
248 pattern: helpers.regex('pattern', isoTimeRegex)
249 }
250 },
251 ntp: {
252 firstAddress: {
253 required: requiredIf(function() {
254 return this.form.configurationSelected === 'ntp';
255 })
256 },
257 secondAddress: {},
258 thirdAddress: {}
259 }
260 }
261 };
262 },
263 computed: {
264 ...mapState('dateTime', ['ntpServers', 'isNtpProtocolEnabled']),
265 bmcTime() {
266 return this.$store.getters['global/bmcTime'];
267 }
268 },
269 watch: {
270 ntpServers() {
271 this.setNtpValues();
272 },
273 manualDate() {
274 this.emitChange();
275 }
276 },
277 created() {
278 this.startLoader();
279 Promise.all([
280 this.$store.dispatch('global/getBmcTime'),
281 this.$store.dispatch('dateTime/getNtpData')
282 ]).finally(() => this.endLoader());
283 },
284 beforeRouteLeave(to, from, next) {
285 this.hideLoader();
286 next();
287 },
288 methods: {
289 emitChange() {
290 if (this.$v.$invalid) return;
291 this.$v.$reset(); //reset to re-validate on blur
292 this.$emit('change', {
293 manualDate: this.manualDate ? new Date(this.manualDate) : null
294 });
295 },
296 setNtpValues() {
297 this.form.configurationSelected = this.isNtpProtocolEnabled
298 ? 'ntp'
299 : 'manual';
300 this.form.ntp.firstAddress = this.ntpServers[0] || '';
301 this.form.ntp.secondAddress = this.ntpServers[1] || '';
302 this.form.ntp.thirdAddress = this.ntpServers[2] || '';
303 },
304 onChangeConfigType() {
305 this.$v.form.$reset();
306 this.setNtpValues();
307 },
308 submitForm() {
309 this.$v.$touch();
310 if (this.$v.$invalid) return;
311 this.startLoader();
312
313 let dateTimeForm = {};
314 let ntpFirstAddress;
315 let ntpSecondAddress;
316 let ntpThirdAddress;
317 let isNTPEnabled = this.form.configurationSelected === 'ntp';
318
319 if (!isNTPEnabled) {
320 dateTimeForm.ntpProtocolEnabled = false;
321 dateTimeForm.updatedDateTime = new Date(
322 `${this.form.manual.date} ${this.form.manual.time}`
323 ).toISOString();
324 } else {
325 ntpFirstAddress = this.form.ntp.firstAddress;
326 ntpSecondAddress = this.form.ntp.secondAddress;
327 ntpThirdAddress = this.form.ntp.thirdAddress;
328 dateTimeForm.ntpProtocolEnabled = true;
329 dateTimeForm.ntpServersArray = [
330 ntpFirstAddress,
331 ntpSecondAddress,
332 ntpThirdAddress
333 ];
334 }
335
336 this.$store
337 .dispatch('dateTime/updateDateTimeSettings', dateTimeForm)
338 .then(success => {
339 this.successToast(success);
340 if (!isNTPEnabled) return;
341 // Shift address up if second address is empty
342 // to avoid refreshing after delay when updating NTP
343 if (ntpSecondAddress === '' && ntpThirdAddress !== '') {
344 this.form.ntp.secondAddress = ntpThirdAddress;
345 this.form.ntp.thirdAddress = '';
346 }
347 })
Dixsie Wolmersecd45a82020-07-14 17:07:26 -0500348 .then(() => {
349 this.$store.dispatch('global/getBmcTime');
350 })
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500351 .catch(({ message }) => this.errorToast(message))
352 .finally(() => {
353 this.$v.form.$reset();
354 this.endLoader();
355 });
356 }
357 }
358};
359</script>
360
361<style lang="scss" scoped>
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500362.b-form-datepicker {
363 position: absolute;
364 right: 0;
365 top: 0;
366 z-index: $zindex-dropdown + 1;
367}
368</style>