blob: 74879f3265c1705297bd792a8d99613d27bcf8e6 [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">
36 <b-form-group label="Configure date and time" label-sr-only>
37 <b-form-radio
38 v-model="form.configurationSelected"
39 value="manual"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +053040 data-test-id="dateTimeSettings-radio-configureManual"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050041 @change="onChangeConfigType"
42 >
43 {{ $t('pageDateTimeSettings.form.manual') }}
44 </b-form-radio>
45 <b-row class="mt-3 ml-3">
46 <b-col sm="6" lg="4" xl="3">
47 <b-form-group
48 :label="$t('pageDateTimeSettings.form.date')"
49 label-for="input-manual-date"
50 >
51 <b-form-text id="date-format-help">(YYYY-MM-DD)</b-form-text>
52 <b-input-group>
53 <b-form-input
54 id="input-manual-date"
55 v-model="form.manual.date"
56 :state="getValidationState($v.form.manual.date)"
57 :disabled="form.configurationSelected === 'ntp'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +053058 data-test-id="dateTimeSettings-input-manualDate"
Dixsie Wolmers739e4592020-06-05 07:00:06 -050059 @blur="$v.form.manual.date.$touch()"
60 />
61 <b-form-invalid-feedback role="alert">
62 <div v-if="!$v.form.manual.date.pattern">
63 {{ $t('global.form.invalidFormat') }}
64 </div>
Dixsie Wolmers6aa9cf72020-07-20 07:46:32 -050065 <div v-if="!$v.form.manual.date.required">
Dixsie Wolmers739e4592020-06-05 07:00:06 -050066 {{ $t('global.form.fieldRequired') }}
67 </div>
68 </b-form-invalid-feedback>
69 <b-form-datepicker
70 v-model="form.manual.date"
71 button-only
72 right
73 size="sm"
74 :hide-header="true"
75 :locale="locale"
76 :label-help="
77 $t('global.calendar.useCursorKeysToNavigateCalendarDates')
78 "
79 :disabled="form.configurationSelected === 'ntp'"
80 button-variant="link"
81 aria-controls="input-manual-date"
82 >
83 <template v-slot:button-content>
84 <icon-calendar />
85 <span class="sr-only">
86 {{ $t('global.calendar.openDatePicker') }}
87 </span>
88 </template>
89 </b-form-datepicker>
90 </b-input-group>
91 </b-form-group>
92 </b-col>
93 <b-col sm="6" lg="4" xl="3">
94 <b-form-group
95 :label="$t('pageDateTimeSettings.form.time')"
96 label-for="input-manual-time"
97 >
98 <b-form-text id="time-format-help">(HH:MM)</b-form-text>
99 <b-input-group>
100 <b-form-input
101 id="input-manual-time"
102 v-model="form.manual.time"
103 :state="getValidationState($v.form.manual.time)"
104 :disabled="form.configurationSelected === 'ntp'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530105 data-test-id="dateTimeSettings-input-manualTime"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500106 @blur="$v.form.manual.time.$touch()"
107 />
108 <b-form-invalid-feedback role="alert">
109 <div v-if="!$v.form.manual.time.pattern">
110 {{ $t('global.form.invalidFormat') }}
111 </div>
112 <div v-if="!$v.form.manual.time.required">
113 {{ $t('global.form.fieldRequired') }}
114 </div>
115 </b-form-invalid-feedback>
116 </b-input-group>
117 </b-form-group>
118 </b-col>
119 </b-row>
120 <b-form-radio
121 v-model="form.configurationSelected"
122 value="ntp"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530123 data-test-id="dateTimeSettings-radio-configureNTP"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500124 @change="onChangeConfigType"
125 >
126 NTP
127 </b-form-radio>
128 <b-row class="mt-3 ml-3">
129 <b-col sm="6" lg="4" xl="3">
130 <b-form-group
131 :label="$t('pageDateTimeSettings.form.ntpServers.server1')"
132 label-for="input-ntp-1"
133 >
134 <b-input-group>
135 <b-form-input
136 id="input-ntp-1"
137 v-model="form.ntp.firstAddress"
138 :state="getValidationState($v.form.ntp.firstAddress)"
139 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530140 data-test-id="dateTimeSettings-input-ntpServer1"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500141 @blur="$v.form.ntp.firstAddress.$touch()"
142 />
143 <b-form-invalid-feedback role="alert">
144 <div v-if="!$v.form.ntp.firstAddress.required">
145 {{ $t('global.form.fieldRequired') }}
146 </div>
147 </b-form-invalid-feedback>
148 </b-input-group>
149 </b-form-group>
150 </b-col>
151 <b-col sm="6" lg="4" xl="3">
152 <b-form-group
153 :label="$t('pageDateTimeSettings.form.ntpServers.server2')"
154 label-for="input-ntp-2"
155 >
156 <b-input-group>
157 <b-form-input
158 id="input-ntp-2"
159 v-model="form.ntp.secondAddress"
160 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530161 data-test-id="dateTimeSettings-input-ntpServer2"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500162 @blur="$v.form.ntp.secondAddress.$touch()"
163 />
164 </b-input-group>
165 </b-form-group>
166 </b-col>
167 <b-col sm="6" lg="4" xl="3">
168 <b-form-group
169 :label="$t('pageDateTimeSettings.form.ntpServers.server3')"
170 label-for="input-ntp-3"
171 >
172 <b-input-group>
173 <b-form-input
174 id="input-ntp-3"
175 v-model="form.ntp.thirdAddress"
176 :disabled="form.configurationSelected === 'manual'"
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530177 data-test-id="dateTimeSettings-input-ntpServer3"
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500178 @blur="$v.form.ntp.thirdAddress.$touch()"
179 />
180 </b-input-group>
181 </b-form-group>
182 </b-col>
183 </b-row>
184 </b-form-group>
Sukanya Pandeyf7c39372020-07-17 16:53:07 +0530185 <b-button
186 variant="primary"
187 type="submit"
188 data-test-id="dateTimeSettings-button-saveSettings"
189 >
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500190 {{ $t('global.action.saveSettings') }}
191 </b-button>
192 </b-form>
193 </page-section>
194 </b-container>
195</template>
196
197<script>
198import Alert from '@/components/Global/Alert';
199import IconCalendar from '@carbon/icons-vue/es/calendar/20';
200import PageTitle from '@/components/Global/PageTitle';
201import PageSection from '@/components/Global/PageSection';
202
203import BVToastMixin from '@/components/Mixins/BVToastMixin';
204import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
205import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
206
207import { mapState } from 'vuex';
208import { requiredIf, helpers } from 'vuelidate/lib/validators';
209
210const isoDateRegex = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/;
211const isoTimeRegex = /^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
212
213export default {
214 name: 'DateTimeSettings',
215 components: { Alert, IconCalendar, PageTitle, PageSection },
216 mixins: [BVToastMixin, LoadingBarMixin, VuelidateMixin],
217 data() {
218 return {
219 locale: this.$store.getters['global/languagePreference'],
220 form: {
221 configurationSelected: '',
222 manual: {
223 date: '',
224 time: ''
225 },
226 ntp: { firstAddress: '', secondAddress: '', thirdAddress: '' }
227 }
228 };
229 },
230 validations() {
231 return {
232 form: {
233 manual: {
234 date: {
235 required: requiredIf(function() {
236 return this.form.configurationSelected === 'manual';
237 }),
238 pattern: helpers.regex('pattern', isoDateRegex)
239 },
240 time: {
241 required: requiredIf(function() {
242 return this.form.configurationSelected === 'manual';
243 }),
244 pattern: helpers.regex('pattern', isoTimeRegex)
245 }
246 },
247 ntp: {
248 firstAddress: {
249 required: requiredIf(function() {
250 return this.form.configurationSelected === 'ntp';
251 })
252 },
253 secondAddress: {},
254 thirdAddress: {}
255 }
256 }
257 };
258 },
259 computed: {
260 ...mapState('dateTime', ['ntpServers', 'isNtpProtocolEnabled']),
261 bmcTime() {
262 return this.$store.getters['global/bmcTime'];
263 }
264 },
265 watch: {
266 ntpServers() {
267 this.setNtpValues();
268 },
269 manualDate() {
270 this.emitChange();
271 }
272 },
273 created() {
274 this.startLoader();
275 Promise.all([
276 this.$store.dispatch('global/getBmcTime'),
277 this.$store.dispatch('dateTime/getNtpData')
278 ]).finally(() => this.endLoader());
279 },
280 beforeRouteLeave(to, from, next) {
281 this.hideLoader();
282 next();
283 },
284 methods: {
285 emitChange() {
286 if (this.$v.$invalid) return;
287 this.$v.$reset(); //reset to re-validate on blur
288 this.$emit('change', {
289 manualDate: this.manualDate ? new Date(this.manualDate) : null
290 });
291 },
292 setNtpValues() {
293 this.form.configurationSelected = this.isNtpProtocolEnabled
294 ? 'ntp'
295 : 'manual';
296 this.form.ntp.firstAddress = this.ntpServers[0] || '';
297 this.form.ntp.secondAddress = this.ntpServers[1] || '';
298 this.form.ntp.thirdAddress = this.ntpServers[2] || '';
299 },
300 onChangeConfigType() {
301 this.$v.form.$reset();
302 this.setNtpValues();
303 },
304 submitForm() {
305 this.$v.$touch();
306 if (this.$v.$invalid) return;
307 this.startLoader();
308
309 let dateTimeForm = {};
310 let ntpFirstAddress;
311 let ntpSecondAddress;
312 let ntpThirdAddress;
313 let isNTPEnabled = this.form.configurationSelected === 'ntp';
314
315 if (!isNTPEnabled) {
316 dateTimeForm.ntpProtocolEnabled = false;
317 dateTimeForm.updatedDateTime = new Date(
318 `${this.form.manual.date} ${this.form.manual.time}`
319 ).toISOString();
320 } else {
321 ntpFirstAddress = this.form.ntp.firstAddress;
322 ntpSecondAddress = this.form.ntp.secondAddress;
323 ntpThirdAddress = this.form.ntp.thirdAddress;
324 dateTimeForm.ntpProtocolEnabled = true;
325 dateTimeForm.ntpServersArray = [
326 ntpFirstAddress,
327 ntpSecondAddress,
328 ntpThirdAddress
329 ];
330 }
331
332 this.$store
333 .dispatch('dateTime/updateDateTimeSettings', dateTimeForm)
334 .then(success => {
335 this.successToast(success);
336 if (!isNTPEnabled) return;
337 // Shift address up if second address is empty
338 // to avoid refreshing after delay when updating NTP
339 if (ntpSecondAddress === '' && ntpThirdAddress !== '') {
340 this.form.ntp.secondAddress = ntpThirdAddress;
341 this.form.ntp.thirdAddress = '';
342 }
343 })
Dixsie Wolmersecd45a82020-07-14 17:07:26 -0500344 .then(() => {
345 this.$store.dispatch('global/getBmcTime');
346 })
Dixsie Wolmers739e4592020-06-05 07:00:06 -0500347 .catch(({ message }) => this.errorToast(message))
348 .finally(() => {
349 this.$v.form.$reset();
350 this.endLoader();
351 });
352 }
353 }
354};
355</script>
356
357<style lang="scss" scoped>
358@import 'src/assets/styles/helpers';
359
360.b-form-datepicker {
361 position: absolute;
362 right: 0;
363 top: 0;
364 z-index: $zindex-dropdown + 1;
365}
366</style>