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