summaryrefslogblamecommitdiffstats
path: root/webapp/src/store/groups.js
blob: 6c5a23cdeab0b5f351e592e341de6c9d897f86b7 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                     




                         



                    
                   

                 

              







                                                                                        
                                                              






                                                                                                        
       
      




                                                                                                            
      







                            


            





                                                                                                              
         
      
































                                                                                                                                         

        











                                                                                                                                  
        


     
import Vue from 'vue'
import axios from 'axios'

export default {
  namespaced: true,
  state: {
    groupList: [],
    clientList: [],
    groupCache: {},
    clientCache: {},
    groupChain: [],
    client: null,
    activeTab: 0
  },
  mutations: {
    setGroupList: (state, list) => { state.groupList = list },
    setClientList: (state, list) => { state.clientList = list },
    setActiveTab: (state, tabIndex) => { state.activeTab = tabIndex },
    setGroupChain: (state, chain) => { state.groupChain = chain },
    setClient: (state, id) => { state.client = state.clientCache[id] },
    setGroupInChain (state, { id, tabIndex }) {
      if (state.groupChain.length <= tabIndex || state.groupChain[tabIndex].id !== id) {
        state.client = null
        state.groupChain = state.groupChain.slice(0, tabIndex)
        state.groupChain.push(state.groupCache[id])
      }
    },
    cacheGroup (state, group) {
      if ('id' in group) {
        if (!(group.id in state.groupCache)) state.groupCache[group.id] = group
        else Object.keys(group).forEach(key => { Vue.set(state.groupCache[group.id], key, group[key]) })
      }
    },
    cacheClient (state, client) {
      if ('id' in client) {
        if (!(client.id in state.clientCache)) state.clientCache[client.id] = client
        else Object.keys(client).forEach(key => { Vue.set(state.clientCache[client.id], key, client[key]) })
      }
    },
    resetStore (state) {
      state.groupList = []
      state.clientList = []
      state.groupCache = {}
      state.clientCache = {}
      state.groupChain = []
      state.client = null
      state.activeTab = 0
    }
  },
  actions: {
    loadLists (context) {
      var getGroupList = axios.get('/api/groups/getList')
      var getClientList = axios.get('/api/clients/getList')
      axios.all([getGroupList, getClientList]).then(axios.spread((groupResponse, clientResponse) => {
        context.commit('setGroupList', groupResponse.data.map(x => ({ value: x.id, text: x.name || x.id })))
        context.commit('setClientList', clientResponse.data.map(x => ({ value: x.id, text: x.name || x.id })))
      }))
    },
    loadRoot (context) {
      var getGroups = axios.get('/api/groups/getSubGroups?id=' + 0)
      var getClients = axios.get('/api/groups/getClients?id=' + 0)
      axios.all([getGroups, getClients]).then(axios.spread((groupResponse, clientResponse) => {
        groupResponse.data.forEach(group => { context.commit('cacheGroup', group) })
        clientResponse.data.forEach(client => { context.commit('cacheClient', client) })
        context.commit('setGroupChain', [{ id: 0, subGroups: groupResponse.data, clients: clientResponse.data }])
      }))
    },
    loadGroupIntoTab (context, { id, tabIndex }) {
      var cached = null
      if (context.state.groupCache[id].fullyCached) {
        cached = new Promise(resolve => resolve())
      } else {
        var getParents = axios.get('/api/groups/getParents?id=' + id)
        var getSubGroups = axios.get('/api/groups/getSubGroups?id=' + id)
        var getClients = axios.get('/api/groups/getClients?id=' + id)
        cached = axios.all([getParents, getSubGroups, getClients]).then(axios.spread((parentResponse, groupResponse, clientResponse) => {
          parentResponse.data.forEach(group => { context.commit('cacheGroup', group) })
          groupResponse.data.forEach(group => { context.commit('cacheGroup', group) })
          clientResponse.data.forEach(client => { context.commit('cacheClient', client) })
          context.commit('cacheGroup', {
            id,
            fullyCached: true,
            parents: parentResponse.data.map(x => context.state.groupCache[x.id]),
            subGroups: groupResponse.data.map(x => context.state.groupCache[x.id]),
            clients: clientResponse.data.map(x => context.state.clientCache[x.id])
          })
        }))
      }
      cached.then(() => {
        context.commit('setGroupInChain', { id, tabIndex })
        context.commit('setActiveTab', tabIndex)
      })
    },
    loadClientIntoTab (context, id) {
      var cached = null
      if (context.state.clientCache[id].fullyCached) {
        cached = new Promise(resolve => resolve())
      } else {
        cached = axios.get('/api/clients/getGroups?id=' + id).then(response => {
          context.commit('cacheClient', { id, fullyCached: true, groups: response.data.map(x => context.state.groupCache[x.id]) })
        })
      }
      cached.then(() => {
        context.commit('setClient', id)
        context.commit('setActiveTab', context.state.groupChain.length)
      })
    }
  }
}