1<template>
2  <b-container fluid="xl">
3    <page-title :description="$t('pagePowerRestorePolicy.description')" />
4
5    <b-row>
6      <b-col sm="8" md="6" xl="12">
7        <b-form-group :label="$t('pagePowerRestorePolicy.powerPoliciesLabel')">
8          <b-form-radio-group
9            v-model="currentPowerRestorePolicy"
10            :options="options"
11            name="power-restore-policy"
12            stacked
13          ></b-form-radio-group>
14        </b-form-group>
15      </b-col>
16    </b-row>
17
18    <b-button variant="primary" type="submit" @click="submitForm">
19      {{ $t('global.action.saveSettings') }}
20    </b-button>
21  </b-container>
22</template>
23
24<script>
25import PageTitle from '@/components/Global/PageTitle';
26import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
27import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
28import BVToastMixin from '@/components/Mixins/BVToastMixin';
29
30export default {
31  name: 'PowerRestorePolicy',
32  components: { PageTitle },
33  mixins: [VuelidateMixin, BVToastMixin, LoadingBarMixin],
34  beforeRouteLeave(to, from, next) {
35    this.hideLoader();
36    next();
37  },
38  data() {
39    return {
40      policyValue: null,
41      options: [],
42    };
43  },
44  computed: {
45    powerRestorePolicies() {
46      return this.$store.getters['powerPolicy/powerRestorePolicies'];
47    },
48    currentPowerRestorePolicy: {
49      get() {
50        return this.$store.getters['powerPolicy/powerRestoreCurrentPolicy'];
51      },
52      set(policy) {
53        this.policyValue = policy;
54      },
55    },
56  },
57  created() {
58    this.startLoader();
59    this.renderPowerRestoreSettings();
60  },
61  methods: {
62    renderPowerRestoreSettings() {
63      Promise.all([
64        this.$store.dispatch('powerPolicy/getPowerRestorePolicies'),
65        this.$store.dispatch('powerPolicy/getPowerRestoreCurrentPolicy'),
66      ]).finally(() => {
67        this.options.length = 0;
68        this.powerRestorePolicies.map((item) => {
69          this.options.push({
70            text: this.$t(`pagePowerRestorePolicy.policiesDesc.${item.state}`),
71            value: `${item.state}`,
72          });
73        });
74        this.endLoader();
75      });
76    },
77    submitForm() {
78      this.startLoader();
79      this.$store
80        .dispatch(
81          'powerPolicy/setPowerRestorePolicy',
82          this.policyValue || this.currentPowerRestorePolicy,
83        )
84        .then((message) => this.successToast(message))
85        .catch(({ message }) => this.errorToast(message))
86        .finally(() => {
87          this.renderPowerRestoreSettings();
88        });
89    },
90  },
91};
92</script>
93