1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const SystemStore = {
5  namespaced: true,
6  state: {
7    systems: [],
8  },
9  getters: {
10    systems: (state) => state.systems,
11  },
12  mutations: {
13    setSystemInfo: (state, data) => {
14      const system = {};
15      system.assetTag = data.AssetTag;
16      system.description = data.Description;
17      system.firmwareVersion = data.BiosVersion;
18      system.hardwareType = data.Name;
19      system.health = data.Status?.Health;
20      system.totalSystemMemoryGiB = data.MemorySummary?.TotalSystemMemoryGiB;
21      system.id = data.Id;
22      system.locationIndicatorActive = data.LocationIndicatorActive;
23      system.locationNumber = data.Location?.PartLocation?.ServiceLabel;
24      system.manufacturer = data.Manufacturer;
25      system.model = data.Model;
26      system.processorSummaryCount = data.ProcessorSummary?.Count;
27      system.processorSummaryCoreCount = data.ProcessorSummary?.CoreCount;
28      system.powerState = data.PowerState;
29      system.serialNumber = data.SerialNumber;
30      system.healthRollup = data.Status?.HealthRollup;
31      system.subModel = data.SubModel;
32      system.statusState = data.Status?.State;
33      system.systemType = data.SystemType;
34      state.systems = [system];
35    },
36  },
37  actions: {
38    async getSystem({ commit }) {
39      return await api
40        .get(`${await this.dispatch('global/getSystemPath')}`)
41        .then(({ data }) => commit('setSystemInfo', data))
42        .catch((error) => console.log(error));
43    },
44    async changeIdentifyLedState({ commit }, ledState) {
45      return await api
46        .patch(`${await this.dispatch('global/getSystemPath')}`, {
47          LocationIndicatorActive: ledState,
48        })
49        .then(() => {
50          if (ledState) {
51            return i18n.global.t(
52              'pageInventory.toast.successEnableIdentifyLed',
53            );
54          } else {
55            return i18n.global.t(
56              'pageInventory.toast.successDisableIdentifyLed',
57            );
58          }
59        })
60        .catch((error) => {
61          commit('setSystemInfo', this.state.system.systems[0]);
62          console.log('error', error);
63          if (ledState) {
64            throw new Error(
65              i18n.global.t('pageInventory.toast.errorEnableIdentifyLed'),
66            );
67          } else {
68            throw new Error(
69              i18n.global.t('pageInventory.toast.errorDisableIdentifyLed'),
70            );
71          }
72        });
73    },
74  },
75};
76
77export default SystemStore;
78