1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const ServerLedStore = {
5  namespaced: true,
6  state: {
7    indicatorLedActiveState: false,
8  },
9  getters: {
10    getIndicatorLedActiveState: (state) => state.indicatorLedActiveState,
11  },
12  mutations: {
13    setIndicatorLedActiveState(state, indicatorLedActiveState) {
14      state.indicatorLedActiveState = indicatorLedActiveState;
15    },
16  },
17  actions: {
18    async getIndicatorLedActiveState({ commit }) {
19      return await api
20        .get('/redfish/v1/Systems/system')
21        .then((response) => {
22          commit(
23            'setIndicatorLedActiveState',
24            response.data.LocationIndicatorActive
25          );
26        })
27        .catch((error) => console.log(error));
28    },
29    async saveIndicatorLedActiveState({ commit }, payload) {
30      commit('setIndicatorLedActiveState', payload);
31      return await api
32        .patch('/redfish/v1/Systems/system', {
33          LocationIndicatorActive: payload,
34        })
35        .then(() => {
36          if (payload) {
37            return i18n.t('pageServerLed.toast.successServerLedOn');
38          } else {
39            return i18n.t('pageServerLed.toast.successServerLedOff');
40          }
41        })
42        .catch((error) => {
43          console.log(error);
44          commit('setIndicatorLedActiveState', !payload);
45          if (payload) {
46            throw new Error(i18n.t('pageServerLed.toast.errorServerLedOn'));
47          } else {
48            throw new Error(i18n.t('pageServerLed.toast.errorServerLedOff'));
49          }
50        });
51    },
52  },
53};
54
55export default ServerLedStore;
56