1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const MemoryStore = {
5  namespaced: true,
6  state: {
7    dimms: [],
8  },
9  getters: {
10    dimms: (state) => state.dimms,
11  },
12  mutations: {
13    setMemoryInfo: (state, data) => {
14      state.dimms = data.map(({ data }) => {
15        const {
16          Id,
17          Status = {},
18          BaseModuleType,
19          BusWidthBits,
20          CapacityMiB,
21          DataWidthBits,
22          Enabled,
23          OperatingSpeedMhz,
24          PartNumber,
25          SerialNumber,
26          SparePartNumber,
27          Description,
28          MemoryType,
29          LocationIndicatorActive,
30          Location,
31        } = data;
32        return {
33          id: Id,
34          health: Status.Health,
35          baseModuleType: BaseModuleType,
36          busWidthBits: BusWidthBits,
37          capacityMiB: CapacityMiB,
38          dataWidthBits: DataWidthBits,
39          operatingSpeedMhz: OperatingSpeedMhz,
40          enabled: Enabled,
41          partNumber: PartNumber,
42          serialNumber: SerialNumber,
43          statusState: Status.State,
44          sparePartNumber: SparePartNumber,
45          description: Description,
46          memoryType: MemoryType,
47          identifyLed: LocationIndicatorActive,
48          uri: data['@odata.id'],
49          locationNumber: Location?.PartLocation?.ServiceLabel,
50        };
51      });
52    },
53  },
54  actions: {
55    async getDimms({ commit }) {
56      return await api
57        .get('/redfish/v1/Systems/system/Memory')
58        .then(({ data: { Members } }) => {
59          const promises = Members.map((item) => api.get(item['@odata.id']));
60          return api.all(promises);
61        })
62        .then((response) => commit('setMemoryInfo', response))
63        .catch((error) => console.log(error));
64    },
65    async updateIdentifyLedValue({ dispatch }, led) {
66      const uri = led.uri;
67      const updatedIdentifyLedValue = {
68        LocationIndicatorActive: led.identifyLed,
69      };
70      return await api.patch(uri, updatedIdentifyLedValue).catch((error) => {
71        dispatch('getDimms');
72        console.log('error', error);
73        if (led.identifyLed) {
74          throw new Error(i18n.t('pageInventory.toast.errorEnableIdentifyLed'));
75        } else {
76          throw new Error(
77            i18n.t('pageInventory.toast.errorDisableIdentifyLed')
78          );
79        }
80      });
81    },
82  },
83};
84
85export default MemoryStore;
86