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';
56
57export default {
58  name: 'BootSettings',
59  mixins: [BVToastMixin, LoadingBarMixin],
60  data() {
61    return {
62      form: {
63        bootOption: this.$store.getters['serverBootSettings/bootSource'],
64        oneTimeBoot: this.$store.getters['serverBootSettings/overrideEnabled'],
65        tpmPolicyOn: this.$store.getters['serverBootSettings/tpmEnabled'],
66      },
67    };
68  },
69  computed: {
70    ...mapState('serverBootSettings', [
71      'bootSourceOptions',
72      'bootSource',
73      'overrideEnabled',
74      'tpmEnabled',
75    ]),
76  },
77  watch: {
78    bootSource: function (value) {
79      this.form.bootOption = value;
80    },
81    overrideEnabled: function (value) {
82      this.form.oneTimeBoot = value;
83    },
84    tpmEnabled: function (value) {
85      this.form.tpmPolicyOn = value;
86    },
87  },
88  validations: {
89    // Empty validations to leverage vuelidate form states
90    // to check for changed values
91    form: {
92      bootOption: {},
93      oneTimeBoot: {},
94      tpmPolicyOn: {},
95    },
96  },
97  created() {
98    this.$store
99      .dispatch('serverBootSettings/getTpmPolicy')
100      .finally(() =>
101        this.$root.$emit('server-power-operations-boot-settings-complete'),
102      );
103  },
104  methods: {
105    handleSubmit() {
106      this.startLoader();
107      const tpmPolicyChanged = this.$v.form.tpmPolicyOn.$dirty;
108      let settings;
109      let bootSource = this.form.bootOption;
110      let overrideEnabled = this.form.oneTimeBoot;
111      let tpmEnabled = null;
112
113      if (tpmPolicyChanged) tpmEnabled = this.form.tpmPolicyOn;
114      settings = { bootSource, overrideEnabled, tpmEnabled };
115
116      this.$store
117        .dispatch('serverBootSettings/saveSettings', settings)
118        .then((message) => this.successToast(message))
119        .catch(({ message }) => this.errorToast(message))
120        .finally(() => {
121          this.$v.form.$reset();
122          this.endLoader();
123        });
124    },
125    onChangeSelect(selectedOption) {
126      this.$v.form.bootOption.$touch();
127      // Disable one time boot if selected boot option is 'None'
128      if (selectedOption === 'None') this.form.oneTimeBoot = false;
129    },
130  },
131};
132</script>
133