Skip to content
Snippets Groups Projects
index.js 1.53 KiB
// Main store for getting and sharing initTables data
// across components.
// https://v2.nuxt.com/docs/directory-structure/store

export const state = () => ({
  initTables: {}
})

export const getters = {
  getTotalTables(state) {
    return Object.keys(state.initTables).length
  },
  getTotalPhenotypes(state) {
    return Object.values(state.initTables).reduce((total, table) => {
      if (typeof (table.nb_phenotypes) === 'number') {
        return total + table.nb_phenotypes
      } else {
        return total
      }
    }, 0)
  },
  getTotalSNP(state) {
    return Object.values(state.initTables).reduce((total, table) => {
      if (typeof (table.nb_snps) === 'number') {
        return total + table.nb_snps
      } else {
        return total
      }
    }, 0)
  },
}

export const mutations = {
  setInitTables(state, initTables) {
    state.initTables = initTables
  }
}

export const actions = {
  async fetchTables({ commit }) {
    const initTables = {}

    return await this.$axios.$get('/tables')
      .then((initTablesList) => {
        const initMetaRequests = initTablesList.map(initTableName => {
          return this.$axios
            .$post('/initmeta', { initTableName })
            .then(initMeta => {
              initTables[initTableName] = initMeta
            })
        })

        return Promise.all(initMetaRequests)
      })
      .then(() => {
        // Store the initTables all at once to avoid partial fetching
        // if one of the previous requests fail.
        commit('setInitTables', initTables)
      })
  }
}