summaryrefslogtreecommitdiff
path: root/src/store/modules/Settings/PowerPolicyStore.js
blob: 9cbf3c8b01229d10d435ae8f954dff49dcf5a389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import api from '@/store/api';
import i18n from '@/i18n';

const PowerPolicyStore = {
  namespaced: true,
  state: {
    powerRestoreCurrentPolicy: null,
    powerRestorePolicies: [],
  },
  getters: {
    powerRestoreCurrentPolicy: (state) => state.powerRestoreCurrentPolicy,
    powerRestorePolicies: (state) => state.powerRestorePolicies,
  },
  mutations: {
    setPowerRestoreCurrentPolicy: (state, powerRestoreCurrentPolicy) =>
      (state.powerRestoreCurrentPolicy = powerRestoreCurrentPolicy),
    setPowerRestorePolicies: (state, powerRestorePolicies) =>
      (state.powerRestorePolicies = powerRestorePolicies),
  },
  actions: {
    async getPowerRestorePolicies({ commit }) {
      return await api
        .get('/redfish/v1/JsonSchemas/ComputerSystem')
        .then(async (response) => {
          if (
            response.data?.Location.length > 0 &&
            response.data?.Location[0].Uri
          ) {
            return await api.get(response.data?.Location[0].Uri).then(
              ({
                data: {
                  definitions: { PowerRestorePolicyTypes = {} },
                },
              }) => {
                let powerPoliciesData = PowerRestorePolicyTypes.enum.map(
                  (powerState) => {
                    let desc = `${i18n.global.t(
                      `pagePowerRestorePolicy.policies.${powerState}`,
                    )} - ${
                      PowerRestorePolicyTypes.enumDescriptions[powerState]
                    }`;
                    return {
                      state: powerState,
                      desc,
                    };
                  },
                );
                commit('setPowerRestorePolicies', powerPoliciesData);
              },
            );
          }
        });
    },
    async getPowerRestoreCurrentPolicy({ commit }) {
      return await api
        .get(`${await this.dispatch('global/getSystemPath')}`)
        .then(({ data: { PowerRestorePolicy } }) => {
          commit('setPowerRestoreCurrentPolicy', PowerRestorePolicy);
        })
        .catch((error) => console.log(error));
    },
    async setPowerRestorePolicy({ dispatch }, powerPolicy) {
      const data = { PowerRestorePolicy: powerPolicy };

      return await api
        .patch(`${await this.dispatch('global/getSystemPath')}`, data)
        .then(() => {
          dispatch('getPowerRestoreCurrentPolicy');
          return i18n.global.t(
            'pagePowerRestorePolicy.toast.successSaveSettings',
          );
        })
        .catch((error) => {
          console.log(error);
          throw new Error(
            i18n.global.t('pagePowerRestorePolicy.toast.errorSaveSettings'),
          );
        });
    },
  },
};

export default PowerPolicyStore;