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