summaryrefslogtreecommitdiffstats
path: root/webapp/src/store/groups.js
blob: 4b1ec5afce1435c5ef394e2394ca091ddf6f4ff2 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import Vue from 'vue'
import axios from 'axios'

export default {
  namespaced: true,
  state: {
    groupList: [],
    clientList: [],
    configList: [],
    configNames: {},
    tabChain: [],
    activeTab: 0,
    dialog: {
      show: false,
      info: {}
    }
  },
  mutations: {
    setGroupList: (state, list) => { state.groupList = list },
    setClientList: (state, list) => { state.clientList = list },
    setConfigList: (state, list) => {
      state.configList = list
      var names = {}
      list.forEach(config => {
        names[config.id] = config.name
      })
      state.configNames = names
    },
    setActiveTab: (state, index) => { state.activeTab = index },
    setShowAll: (state, { index, value }) => { state.tabChain[index].tabShowAll = value },
    deleteFromTabChain: (state, { index, count }) => { state.tabChain.splice(index, count) },
    setTab: (state, { index, item }) => {
      if (state.tabChain.length > index + 1 && (state.tabChain[index].tabType !== item.tabType || state.tabChain[index].id !== item.id)) {
        state.tabChain = state.tabChain.slice(0, index + 1)
      }
      state.tabChain.splice(index, 1, item)
    },
    setTabLoading (state, index) {
      if (state.tabChain.length > index) Vue.set(state.tabChain[index], 'loading', true)
    },
    setDialog (state, { show, info }) {
      if (info !== undefined) state.dialog.info = info
      if (show !== undefined) state.dialog.show = show
    }
  },
  actions: {
    loadConfigs (context) {
      axios.get('/api/configurator/configs').then(result => {
        context.commit('setConfigList', result.data.map(x => ({ id: x.id, name: x.name || x.id })))
      })
    },
    loadGroupList (context) {
      axios.get('/api/groups').then(response => {
        context.commit('setGroupList', Object.freeze(response.data))
      })
    },
    loadClientList (context) {
      axios.get('/api/clients').then(response => {
        context.commit('setClientList', Object.freeze(response.data))
      })
    },
    loadLists (context) {
      context.dispatch('loadGroupList')
      context.dispatch('loadClientList')
    },
    loadGroup (context, { id, name, tabIndex, switchTab, reload, save, placeholderName }) {
      if (!reload && context.state.tabChain.length > tabIndex && context.state.tabChain[tabIndex].id === id) {
        if (switchTab) context.commit('setActiveTab', tabIndex)
      } else {
        const showAll = context.state.tabChain.length > tabIndex &&
                        context.state.tabChain[tabIndex].id === id &&
                        context.state.tabChain[tabIndex].tabShowAll
        if (context.state.tabChain.length <= tabIndex || context.state.tabChain[tabIndex].id !== id) {
          context.commit('setTab', { index: tabIndex, item: { id, name, tabType: 'group', tabShowAll: showAll, subgroups: [], clients: [] } })
        }
        if (!save) context.commit('setTabLoading', tabIndex)
        if (switchTab) context.commit('setActiveTab', tabIndex)
        axios.get('/api/groups/' + id + (showAll ? '?all' : '')).then(res => {
          res.data.tabType = 'group'
          res.data.tabShowAll = showAll
          res.data.subgroups = Object.freeze(res.data.subgroups)
          context.commit('setTab', { index: tabIndex, item: res.data })
        }).catch(err => {
          console.log(err)
          if (switchTab) context.commit('setActiveTab', tabIndex - 1)
          context.commit('deleteFromTabChain', { index: tabIndex, count: context.state.tabChain.length - tabIndex })
        })
      }
    },
    loadClient (context, { id, name, tabIndex, switchTab, reload }) {
      if (!reload && context.state.tabChain.length > tabIndex && context.state.tabChain[tabIndex].id === id) {
        if (switchTab) context.commit('setActiveTab', tabIndex)
      } else {
        if (context.state.tabChain.length <= tabIndex || context.state.tabChain[tabIndex].id !== id) {
          context.commit('setTab', { index: tabIndex, item: { id, name, tabType: 'client' } })
        }
        if (switchTab) context.commit('setActiveTab', tabIndex)
        axios.get('/api/clients/' + id).then(res => {
          res.data.tabType = 'client'
          context.commit('setTab', { index: tabIndex, item: res.data })
        }).catch(err => {
          console.log(err)
          if (switchTab) context.commit('setActiveTab', tabIndex - 1)
          context.commit('deleteFromTabChain', { index: tabIndex, count: context.state.tabChain.length - tabIndex })
        })
      }
    },
    reload (context) {
      context.dispatch('loadConfigs')
      context.dispatch('loadLists')
      context.state.tabChain.forEach((item, index) => {
        if (item.id === 'create') return
        if (item.tabType === 'group') context.dispatch('loadGroup', { id: item.id, tabIndex: index, reload: true, tabShowAll: item.tabShowAll })
        else if (item.tabType === 'client') context.dispatch('loadClient', { id: item.id, tabIndex: index, reload: true })
      })
    },
    saveGroup (context, { id, data, parents, ipranges, tabIndex, callback }) {
      const parentIds = parents.map(x => x.id)
      const url = id === 'create' ? '/api/groups' : '/api/groups/' + id
      axios.post(url, { data, parentIds, ipranges }).then(res => {
        if (res.data.id) {
          if (callback) callback(res.data.id)
          context.commit('setTab', {
            index: tabIndex,
            item: {
              ...context.state.tabChain[tabIndex],
              ...data,
              id: res.data.id,
              parents,
              ipranges
            }
          })
          if (parentIds && tabIndex > 1 && !parentIds.includes(context.state.tabChain[tabIndex - 1].id)) {
            context.commit('deleteFromTabChain', { index: 1, count: tabIndex - 1 })
            context.commit('setActiveTab', 1)
          }
          context.dispatch('reload')
        }
      })
    },
    saveClient (context, { id, data, groups, tabIndex, callback }) {
      const groupIds = groups.map(x => x.id)
      const url = id === 'create' ? '/api/clients' : '/api/clients/' + id
      axios.post(url, { data, groupIds }).then(res => {
        if (res.data.id) {
          if (callback) callback(res.data.id)
          context.commit('setTab', {
            index: tabIndex,
            item: {
              ...context.state.tabChain[tabIndex],
              ...data,
              id: res.data.id,
              groups
            }
          })
          if (groupIds && tabIndex > 1 && !groupIds.includes(context.state.tabChain[tabIndex - 1].id)) {
            context.commit('deleteFromTabChain', { index: 1, count: tabIndex - 1 })
            context.commit('setActiveTab', 1)
          }
          context.dispatch('reload')
        }
      })
    },
    deleteGroups (context, { selected, callback }) {
      const ids = selected.map(x => x.id)
      axios.post('/api/groups/?delete', { ids }).then(() => {
        var i = 1
        while (i < context.state.tabChain.length) {
          if (context.state.tabChain[i].tabType === 'group' && ids.includes(context.state.tabChain[i].id)) {
            context.commit('deleteFromTabChain', { index: i, count: context.state.tabChain.length - i })
            break
          }
          i++
        }
        context.dispatch('reload')
        if (callback) callback()
      })
    },
    deleteClients (context, { selected, callback }) {
      const ids = selected.map(x => x.id)
      axios.post('/api/clients/?delete', { ids }).then(() => {
        const index = context.state.tabChain.length - 1
        const item = context.state.tabChain[index]
        if (item.tabType === 'client' && ids.includes(item.id)) context.commit('deleteFromTabChain', { index, count: 1 })
        context.dispatch('reload')
        if (callback) callback()
      })
    },
    removeSubroups (context, { tabIndex, selected, callback }) {
      const id = context.state.tabChain[tabIndex].id
      const ids = selected.map(x => x.id)
      axios.post('/api/groups/' + id + '/subgroups/?delete', { ids }).then(() => {
        if (context.state.tabChain.length > tabIndex + 1 &&
            context.state.tabChain[tabIndex + 1].tabType === 'group' &&
            ids.includes(context.state.tabChain[tabIndex + 1].id)) {
          context.commit('deleteFromTabChain', { index: tabIndex + 1, count: context.state.tabChain.length - (tabIndex + 1) })
        }
        context.dispatch('reload')
        if (callback) callback()
      })
    },
    removeClients (context, { tabIndex, selected, callback }) {
      const id = context.state.tabChain[tabIndex].id
      const ids = selected.map(x => x.id)
      axios.post('/api/groups/' + id + '/clients/?delete', { ids }).then(() => {
        if (context.state.tabChain.length > tabIndex + 1 &&
            context.state.tabChain[tabIndex + 1].tabType === 'client' &&
            ids.includes(context.state.tabChain[tabIndex + 1].id)) {
          context.commit('deleteFromTabChain', { index: tabIndex + 1, count: 1 })
        }
        context.dispatch('reload')
        if (callback) callback()
      })
    },
    addSubgroups (context, { tabIndex, selected, callback }) {
      const id = context.state.tabChain[tabIndex].id
      const ids = selected.map(x => x.id)
      axios.post('/api/groups/' + id + '/subgroups', { ids }).then(() => {
        context.dispatch('reload')
        if (callback) callback()
      })
    },
    addClients (context, { tabIndex, selected, callback }) {
      const id = context.state.tabChain[tabIndex].id
      const ids = selected.map(x => x.id)
      axios.post('/api/groups/' + id + '/clients', { ids }).then(() => {
        context.dispatch('reload')
        if (callback) callback()
      })
    }
  }
}