1<template>
2  <div class="form-background p-3">
3    <b-form novalidate @submit.prevent="handleSubmit">
4      <b-form-group
5        :label="
6          $t('pageServerPowerOperations.bootSettings.bootSettingsOverride')
7        "
8        label-for="boot-option"
9        class="mb-3"
10      >
11        <b-form-select
12          id="boot-option"
13          v-model="form.bootOption"
14          :disabled="bootSourceOptions.length === 0"
15          :options="bootSourceOptions"
16          @change="onChangeSelect"
17        >
18        </b-form-select>
19      </b-form-group>
20      <b-form-checkbox
21        v-model="form.oneTimeBoot"
22        class="mb-4"
23        :disabled="form.bootOption === 'None'"
24        @change="v$.form.oneTimeBoot.$touch()"
25      >
26        {{ $t('pageServerPowerOperations.bootSettings.enableOneTimeBoot') }}
27      </b-form-checkbox>
28      <b-form-group
29        :label="$t('pageServerPowerOperations.bootSettings.tpmRequiredPolicy')"
30      >
31        <b-form-text id="tpm-required-policy-help-block">
32          {{
33            $t('pageServerPowerOperations.bootSettings.tpmRequiredPolicyHelper')
34          }}
35        </b-form-text>
36        <b-form-checkbox
37          id="tpm-required-policy"
38          v-model="form.tpmPolicyOn"
39          aria-describedby="tpm-required-policy-help-block"
40          @change="v$.form.tpmPolicyOn.$touch()"
41        >
42          {{ $t('global.status.enabled') }}
43        </b-form-checkbox>
44      </b-form-group>
45      <b-button variant="primary" type="submit" class="mb-3">
46        {{ $t('global.action.save') }}
47      </b-button>
48    </b-form>
49  </div>
50</template>
51
52<script>
53import { mapState } from 'vuex';
54import BVToastMixin from '@/components/Mixins/BVToastMixin';
55import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
56import { useI18n } from 'vue-i18n';
57import { useVuelidate } from '@vuelidate/core';
58
59export default {
60  name: 'BootSettings',
61  mixins: [BVToastMixin, LoadingBarMixin],
62  setup() {
63    return {
64      v$: useVuelidate(),
65    };
66  },
67  data() {
68    return {
69      $t: useI18n().t,
70      form: {
71        bootOption: this.$store.getters['serverBootSettings/bootSource'],
72        oneTimeBoot: this.$store.getters['serverBootSettings/overrideEnabled'],
73        tpmPolicyOn: this.$store.getters['serverBootSettings/tpmEnabled'],
74      },
75    };
76  },
77  computed: {
78    ...mapState('serverBootSettings', [
79      'bootSourceOptions',
80      'bootSource',
81      'overrideEnabled',
82      'tpmEnabled',
83    ]),
84  },
85  watch: {
86    bootSource: function (value) {
87      this.form.bootOption = value;
88    },
89    overrideEnabled: function (value) {
90      this.form.oneTimeBoot = value;
91    },
92    tpmEnabled: function (value) {
93      this.form.tpmPolicyOn = value;
94    },
95  },
96  validations: {
97    // Empty validations to leverage vuelidate form states
98    // to check for changed values
99    form: {
100      bootOption: {},
101      oneTimeBoot: {},
102      tpmPolicyOn: {},
103    },
104  },
105  created() {
106    this.$store
107      .dispatch('serverBootSettings/getTpmPolicy')
108      .finally(() =>
109        this.$root.$emit('server-power-operations-boot-settings-complete'),
110      );
111  },
112  methods: {
113    handleSubmit() {
114      this.startLoader();
115      const tpmPolicyChanged = this.v$.form.tpmPolicyOn.$dirty;
116      let settings;
117      let bootSource = this.form.bootOption;
118      let overrideEnabled = this.form.oneTimeBoot;
119      let tpmEnabled = null;
120
121      if (tpmPolicyChanged) tpmEnabled = this.form.tpmPolicyOn;
122      settings = { bootSource, overrideEnabled, tpmEnabled };
123
124      this.$store
125        .dispatch('serverBootSettings/saveSettings', settings)
126        .then((message) => this.successToast(message))
127        .catch(({ message }) => this.errorToast(message))
128        .finally(() => {
129          this.v$.form.$reset();
130          this.endLoader();
131        });
132    },
133    onChangeSelect(selectedOption) {
134      this.v$.form.bootOption.$touch();
135      // Disable one time boot if selected boot option is 'None'
136      if (selectedOption === 'None') this.form.oneTimeBoot = false;
137    },
138  },
139};
140</script>
141