1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const PowerPolicyStore = {
5  namespaced: true,
6  state: {
7    powerRestoreCurrentPolicy: null,
8    powerRestorePolicies: [],
9  },
10  getters: {
11    powerRestoreCurrentPolicy: (state) => state.powerRestoreCurrentPolicy,
12    powerRestorePolicies: (state) => state.powerRestorePolicies,
13  },
14  mutations: {
15    setPowerRestoreCurrentPolicy: (state, powerRestoreCurrentPolicy) =>
16      (state.powerRestoreCurrentPolicy = powerRestoreCurrentPolicy),
17    setPowerRestorePolicies: (state, powerRestorePolicies) =>
18      (state.powerRestorePolicies = powerRestorePolicies),
19  },
20  actions: {
21    async getPowerRestorePolicies({ commit }) {
22      return await api
23        .get('/redfish/v1/JsonSchemas/ComputerSystem')
24        .then(async (response) => {
25          if (
26            response.data?.Location.length > 0 &&
27            response.data?.Location[0].Uri
28          ) {
29            return await api.get(response.data?.Location[0].Uri).then(
30              ({
31                data: {
32                  definitions: { PowerRestorePolicyTypes = {} },
33                },
34              }) => {
35                let powerPoliciesData = PowerRestorePolicyTypes.enum.map(
36                  (powerState) => {
37                    let desc = `${i18n.global.t(
38                      `pagePowerRestorePolicy.policies.${powerState}`,
39                    )} - ${
40                      PowerRestorePolicyTypes.enumDescriptions[powerState]
41                    }`;
42                    return {
43                      state: powerState,
44                      desc,
45                    };
46                  },
47                );
48                commit('setPowerRestorePolicies', powerPoliciesData);
49              },
50            );
51          }
52        });
53    },
54    async getPowerRestoreCurrentPolicy({ commit }) {
55      return await api
56        .get(`${await this.dispatch('global/getSystemPath')}`)
57        .then(({ data: { PowerRestorePolicy } }) => {
58          commit('setPowerRestoreCurrentPolicy', PowerRestorePolicy);
59        })
60        .catch((error) => console.log(error));
61    },
62    async setPowerRestorePolicy({ dispatch }, powerPolicy) {
63      const data = { PowerRestorePolicy: powerPolicy };
64
65      return await api
66        .patch(`${await this.dispatch('global/getSystemPath')}`, data)
67        .then(() => {
68          dispatch('getPowerRestoreCurrentPolicy');
69          return i18n.global.t(
70            'pagePowerRestorePolicy.toast.successSaveSettings',
71          );
72        })
73        .catch((error) => {
74          console.log(error);
75          throw new Error(
76            i18n.global.t('pagePowerRestorePolicy.toast.errorSaveSettings'),
77          );
78        });
79    },
80  },
81};
82
83export default PowerPolicyStore;
84