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          MemorySize,
30          LocationIndicatorActive,
31          Location,
32        } = data;
33        return {
34          id: Id,
35          health: Status.Health,
36          baseModuleType: BaseModuleType,
37          busWidthBits: BusWidthBits,
38          capacityMiB: CapacityMiB,
39          dataWidthBits: DataWidthBits,
40          operatingSpeedMhz: OperatingSpeedMhz,
41          enabled: Enabled,
42          partNumber: PartNumber,
43          serialNumber: SerialNumber,
44          statusState: Status.State,
45          sparePartNumber: SparePartNumber,
46          description: Description,
47          memoryType: MemoryType,
48          memorySize: MemorySize,
49          identifyLed: LocationIndicatorActive,
50          uri: data['@odata.id'],
51          locationNumber: Location?.PartLocation?.ServiceLabel,
52        };
53      });
54    },
55  },
56  actions: {
57    async getDimms({ commit }) {
58      return await api
59        .get('/redfish/v1/Systems/system/Memory')
60        .then(({ data: { Members } }) => {
61          const promises = Members.map((item) => api.get(item['@odata.id']));
62          return api.all(promises);
63        })
64        .then((response) => commit('setMemoryInfo', response))
65        .catch((error) => console.log(error));
66    },
67    async updateIdentifyLedValue({ dispatch }, led) {
68      const uri = led.uri;
69      const updatedIdentifyLedValue = {
70        LocationIndicatorActive: led.identifyLed,
71      };
72      return await api.patch(uri, updatedIdentifyLedValue).catch((error) => {
73        dispatch('getDimms');
74        console.log('error', error);
75        if (led.identifyLed) {
76          throw new Error(i18n.t('pageInventory.toast.errorEnableIdentifyLed'));
77        } else {
78          throw new Error(
79            i18n.t('pageInventory.toast.errorDisableIdentifyLed')
80          );
81        }
82      });
83    },
84  },
85};
86
87export default MemoryStore;
88