1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const PostCodeLogsStore = {
5  namespaced: true,
6  state: {
7    allPostCodes: [],
8  },
9  getters: {
10    allPostCodes: (state) => state.allPostCodes,
11  },
12  mutations: {
13    setAllPostCodes: (state, allPostCodes) =>
14      (state.allPostCodes = allPostCodes),
15  },
16  actions: {
17    async getPostCodesLogData({ commit }) {
18      return await api
19        .get('/redfish/v1/Systems/system/LogServices/PostCodes/Entries')
20        .then(({ data: { Members = [] } = {} }) => {
21          const postCodeLogs = Members.map((log) => {
22            const { Created, MessageArgs, AdditionalDataURI } = log;
23            return {
24              date: new Date(Created),
25              bootCount: MessageArgs[0],
26              timeStampOffset: MessageArgs[1],
27              postCode: MessageArgs[2],
28              uri: AdditionalDataURI,
29            };
30          });
31          commit('setAllPostCodes', postCodeLogs);
32        })
33        .catch((error) => {
34          console.log('POST Codes Log Data:', error);
35        });
36    },
37    async deleteAllPostCodeLogs({ dispatch }, data) {
38      return await api
39        .post(
40          '/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog'
41        )
42        .then(() => dispatch('getPostCodesLogData'))
43        .then(() =>
44          i18n.tc('pagePostCodeLogs.toast.successDelete', data.length)
45        )
46        .catch((error) => {
47          console.log(error);
48          throw new Error(
49            i18n.tc('pagePostCodeLogs.toast.errorDelete', data.length)
50          );
51        });
52    },
53  },
54};
55
56export default PostCodeLogsStore;
57