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




                         

                   
                 
                 


                   
              
     

              

                                                                
                                                                
                                                            

                                                                                             

                                                                                                                                          
       
                                           
      

                                                      
                                                      


            
                         
                                                                                                      

                                                    
        
      
                        

                                                                                                                    

                                                                                                                    
      

                                                                                                              
                                                               






                                                                       
      

                                                                                                              
                                                               






                                                                       

                      
                                   

                                                                 

                                                                                                                          

        
                                                            
                                                                           
                                                                                                                            







                                                                                                        
                                                                           
                                                                                                                             
                                                                                                      



                                                                                 
        
      

                                          




                                                                                                            
                 





                                  

                                           





                                                                                                                         






































                                                                                                                              


     
import axios from 'axios'

export default {
  namespaced: true,
  state: {
    groupList: [],
    clientList: [],
    tabChain: [],
    activeTab: 0,
    showAll: false,
    dialog: {
      show: false,
      info: {}
    }
  },
  mutations: {
    setGroupList: (state, list) => { state.groupList = list },
    setClientList: (state, list) => { state.clientList = list },
    setActiveTab: (state, index) => { state.activeTab = index },
    setShowAll: (state, value) => { state.showAll = 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)
    },
    setDialog (state, { show, info, selected }) {
      if (info !== undefined) state.dialog.info = info
      if (show !== undefined) state.dialog.show = show
    }
  },
  actions: {
    loadLists (context) {
      Promise.all([axios.get('/api/groups/getList'), axios.get('/api/clients/getList')]).then(res => {
        context.commit('setGroupList', res[0].data)
        context.commit('setClientList', res[1].data)
      })
    },
    loadHome (context) {
      var apiFunction = context.state.showAll ? 'getAll' : 'getTopLevel'
      Promise.all([axios.get('/api/groups/' + apiFunction), axios.get('/api/clients/' + apiFunction)]).then(res => {
        context.commit('setTab', { index: 0, item: { tabType: 'home', groups: res[0].data, clients: res[1].data } })
      })
    },
    loadGroup (context, { id, tabIndex, switchTab, reload }) {
      if (!reload && context.state.tabChain.length > tabIndex && context.state.tabChain[tabIndex].id === id) {
        if (switchTab) context.commit('setActiveTab', tabIndex)
      } else {
        axios.get('/api/groups/getGroup?id=' + id).then(res => {
          res.data.tabType = 'group'
          context.commit('setTab', { index: tabIndex, item: res.data })
          if (switchTab) context.commit('setActiveTab', tabIndex)
        })
      }
    },
    loadClient (context, { tabType, id, tabIndex, switchTab, reload }) {
      if (!reload && context.state.tabChain.length > tabIndex && context.state.tabChain[tabIndex].id === id) {
        if (switchTab) context.commit('setActiveTab', tabIndex)
      } else {
        axios.get('/api/clients/getClient?id=' + id).then(res => {
          res.data.tabType = 'client'
          context.commit('setTab', { index: tabIndex, item: res.data })
          if (switchTab) context.commit('setActiveTab', tabIndex)
        })
      }
    },
    reload (context) {
      context.dispatch('loadLists')
      context.state.tabChain.forEach((item, index) => {
        if (item.tabType === 'home') context.dispatch('loadHome')
        else if (item.tabType === 'group') context.dispatch('loadGroup', { id: item.id, tabIndex: index, reload: true })
        else if (item.tabType === 'client') context.dispatch('loadClient', { id: item.id, tabIndex: index, reload: true })
      })
    },
    saveGroup (context, { id, info, parentIds, tabIndex }) {
      axios.post('/api/groups/save', { id, info, parentIds }).then(res => {
        if (!id) context.commit('setTab', { index: tabIndex, item: { id: res.data.id, name: info.name, tabType: 'group' } })
        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, info, groupIds, tabIndex }) {
      axios.post('/api/clients/save', { id, info, groupIds }).then(res => {
        if (!id) context.commit('setTab', { index: tabIndex, item: { id: res.data.id, name: info.name, tabType: 'client' } })
        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 }) {
      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')
      })
    },
    deleteClients (context, { selected }) {
      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')
      })
    },
    removeSubroups (context, { tabIndex, selected }) {
      const id = context.state.tabChain[tabIndex].id
      const ids = selected.map(x => x.id)
      axios.post('/api/groups/removeSubgroups', { id, 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')
      })
    },
    removeClients (context, { tabIndex, selected }) {
      const id = context.state.tabChain[tabIndex].id
      const ids = selected.map(x => x.id)
      axios.post('/api/groups/removeClients', { id, 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')
      })
    },
    addSubgroups (context, { tabIndex, selected }) {
      const id = context.state.tabChain[tabIndex].id
      const ids = selected.map(x => x.id)
      console.log(ids)
      axios.post('/api/groups/addSubgroups', { id, ids }).then(() => {
        context.dispatch('reload')
      })
    },
    addClients (context, { tabIndex, selected }) {
      const id = context.state.tabChain[tabIndex].id
      const ids = selected.map(x => x.id)
      axios.post('/api/groups/addClients', { id, ids }).then(() => {
        context.dispatch('reload')
      })
    }
  }
}