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(
20          `${await this.dispatch('global/getSystemPath')}/LogServices/PostCodes/Entries`,
21        )
22        .then(({ data: { Members = [] } = {} }) => {
23          const postCodeLogs = Members.map((log) => {
24            const { Created, MessageArgs, AdditionalDataURI } = log;
25            return {
26              date: new Date(Created),
27              bootCount: MessageArgs[0],
28              timeStampOffset: MessageArgs[1],
29              postCode: MessageArgs[2],
30              uri: AdditionalDataURI,
31            };
32          });
33          commit('setAllPostCodes', postCodeLogs);
34        })
35        .catch((error) => {
36          console.log('POST Codes Log Data:', error);
37        });
38    },
39    async deleteAllPostCodeLogs({ dispatch }, data) {
40      return await api
41        .post(
42          `${await this.dispatch('global/getSystemPath')}/LogServices/PostCodes/Actions/LogService.ClearLog`,
43        )
44        .then(() => dispatch('getPostCodesLogData'))
45        .then(() =>
46          i18n.global.t('pagePostCodeLogs.toast.successDelete', data.length),
47        )
48        .catch((error) => {
49          console.log(error);
50          throw new Error(
51            i18n.global.t('pagePostCodeLogs.toast.errorDelete', data.length),
52          );
53        });
54    },
55  },
56};
57
58export default PostCodeLogsStore;
59