summaryrefslogtreecommitdiffstats
path: root/webapp/src/store/groups.js
blob: 49c567a3cb6d74a83ec51b71dedd415d147d7985 (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
import axios from 'axios'

export default {
  namespaced: true,
  state: {
    groupChain: [],
    activeTab: 0,
    editGroupDialog: false,
    editGroup: null
  },
  mutations: {
    updateActiveTab (state, tabIndex) {
      state.activeTab = tabIndex
    },
    setGroupInChain (state, { tabIndex, group }) {
      if (state.groupChain.length <= tabIndex || state.groupChain[tabIndex].id !== group.id) {
        state.groupChain = state.groupChain.slice(0, tabIndex)
        state.groupChain.push(group)
      }
    },
    openEditGroupDialog (state, group) {
      state.editGroupDialog = true
      state.editGroup = group
    },
    closeEditGroupDialog (state) {
      state.editGroupDialog = false
      state.editGroup = null
    }
  },
  actions: {
    loadGroupInChain (context, { id, name, tabIndex, switchTab }) {
      var getSubGroups = axios.get('/api/groups?action=getSubGroups&id=' + id)
      var getClients = axios.get('/api/groups?action=getClients&id=' + id)
      axios.all([getSubGroups, getClients]).then(axios.spread((groupRespsonse, clientResponse) => {
        var group = { id: id, name: name, subGroups: groupRespsonse.data, clients: clientResponse.data }
        context.commit('setGroupInChain', { group: group, tabIndex: tabIndex })
        if (switchTab) context.commit('updateActiveTab', tabIndex)
      }))
    },
    editGroup (context, { id, name }) {
      axios.get('/api/groups?action=getParents&id=' + id).then(res => {
        context.commit('openEditGroupDialog', { id: id, name: name, parents: res.data })
      })
    },
    saveGroup (context) {
      axios.post('/api/groups?action=update&id=' + context.state.editGroup.id).then(res => {
        context.commit('closeEditGroupDialog')
      })
    }
  }
}