summaryrefslogtreecommitdiff
path: root/src/store/modules/Configuration/DateTimeSettingsStore.js
blob: 9da0cb417f6aab2e81efdcef75a87a5c806f5c48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import api from '../../api';
import i18n from '@/i18n';

const DateTimeStore = {
  namespaced: true,
  state: {
    ntpServers: [],
    isNtpProtocolEnabled: null
  },
  getters: {
    ntpServers: state => state.ntpServers,
    isNtpProtocolEnabled: state => state.isNtpProtocolEnabled
  },
  mutations: {
    setNtpServers: (state, ntpServers) => (state.ntpServers = ntpServers),
    setIsNtpProtocolEnabled: (state, isNtpProtocolEnabled) =>
      (state.isNtpProtocolEnabled = isNtpProtocolEnabled)
  },
  actions: {
    async getNtpData({ commit }) {
      return await api
        .get('/redfish/v1/Managers/bmc/NetworkProtocol')
        .then(response => {
          const ntpServers = response.data.NTP.NTPServers;
          const isNtpProtocolEnabled = response.data.NTP.ProtocolEnabled;
          commit('setNtpServers', ntpServers);
          commit('setIsNtpProtocolEnabled', isNtpProtocolEnabled);
        })
        .catch(error => {
          console.log(error);
        });
    },
    async updateDateTimeSettings(_, dateTimeForm) {
      const ntpData = {
        NTP: {
          ProtocolEnabled: dateTimeForm.ntpProtocolEnabled
        }
      };

      if (dateTimeForm.ntpProtocolEnabled) {
        ntpData.NTP.NTPServers = dateTimeForm.ntpServersArray;
      }
      return await api
        .patch(`/redfish/v1/Managers/bmc/NetworkProtocol`, ntpData)
        .then(() => {
          if (!dateTimeForm.ntpProtocolEnabled) {
            const dateTimeData = {
              DateTime: dateTimeForm.updatedDateTime
            };
            api.patch(`/redfish/v1/Managers/bmc`, dateTimeData);
          }
        })
        .then(() => {
          return i18n.t(
            'pageDateTimeSettings.toast.successSaveDateTimeSettings'
          );
        })
        .catch(error => {
          console.log(error);
          throw new Error(
            i18n.t('pageDateTimeSettings.toast.errorSaveDateTimeSettings')
          );
        });
    }
  }
};

export default DateTimeStore;