Sellsuki / vuex-async-module

vuex-async-module
GNU Affero General Public License v3.0
4 stars 4 forks source link

vuex-async-module

NPM version Coveralls github Build Status Standard Version Maintainability Test Coverage

What is this about ??


Dependencies


Installation

Install via npm

npm install vuex-async-module

Install via yarn

yarn add vuex-async-module

Usage

createVuexAsyncModule

Create vuex store by using our "createVuexAsyncModule" function.
When vuex store was created you don't need to know or write anything inside vuex, But all you need to know is about all functions we provided for you to get the job done easily.

import Vue from 'vue'
import Vuex from 'vuex'
import { createVuexAsyncModule } from 'vuex-async-module'

Vue.use(Vuex)

export const store = new Vuex.Store({
  modules: {
    Info: {
      ...createVuexAsyncModule('info')
    }
  }
})

State

This is module state created by createVuexAsyncModule

  state: {
      data: null, // data from AJAX call, You can customize this by using beforeSave callback (look at next section)
      pending: false, // loading status (true = loading, false = done)
      statusCode: 0, // response status code
  },

mapGetters

One getter available for each module and it represent a state itself.
The name of getter is module name then follow by "State" for example module "info" and getter will be "infoState"

<template>
  <h1>{{infoState.something}}</h1>
</template>

<script>
import {mapGetters} from 'vuex'

export default {
  computed: {
    ...mapGetters(['infoState'])
  }
}
</script>

mapActions

One action available for each module and it can be customize for any endpoint you need.
The name of action is "request" follow by module name then "Async" for example module "info" and action will be "requestInfoAsync"
Once you call action with provided endpoint the function will save reponsed data on state immediately.
Note that axiosConfig is the same config from axios libary itself.

<script>
import {mapActions} from 'vuex'

export default {
  methods: {
    ...mapActions(['requestInfoAsync']),
    getInfo () {
      // after this called all response data will be saved as module state automaticly.
      this.requestInfoAsync({
        axiosConfig: {
          url: '//jsonbin.io/b/5a01dc7471fdfc4fe9d09cdb'
        }
      })
    }
  }
}
</script>

export default { methods: { ...mapActions(['requestInfoAsync']), getInfo () { this.requestInfoAsync({ axiosConfig: { url: '//jsonbin.io/b/5a01dc7471fdfc4fe9d09cdb' }, beforeSave: (data, state) => { // data = reponse data when our request already success // state = last data state

      // filter then return to save in module state (last state will be gone by this)
      return data.map(item => item.name)
    }
  })
}

} }


* Handle action with promise using then, catch
```vue
<script>
import {mapActions} from 'vuex'

export default {
  methods: {
    ...mapActions(['requestInfoAsync']),
    getInfo () {
      this.requestInfoAsync({
        axiosConfig: {
          url: '//jsonbin.io/b/5a01dc7471fdfc4fe9d09cdb'
        }
      }).then(res => {
        console.log(res)
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

export default { methods: { ...mapActions(['requestInfoAsync']), getInfo () { try { await this.requestInfoAsync({ axiosConfig: { url: '//jsonbin.io/b/5a01dc7471fdfc4fe9d09cdb' } }) } catch (e) { console.log(e) } } } }


* Handle action with callback
```vue
<script>
import {mapActions} from 'vuex'

export default {
  methods: {
    ...mapActions(['requestInfoAsync']),
    getInfo () {
      this.requestInfoAsync({
        axiosConfig: {
          url: '//jsonbin.io/b/5a01dc7471fdfc4fe9d09cdb'
        },
        onSuccess: (res) => {
          console.log(res)
        },
        onError: (err) => {
          console.log(err)
        }
      })
    }
  }
}
</script>

mapGetters

When data from AJAX call saved to state then you can map getter to sync your data to the template.
One getter available for each module and it represent a state itself.
The name of getter is module name then follow by "State" for example module "info" and getter will be "infoState"

<template>
  <div v-if="infoState.pending === false">
    <h1>{{infoState.data.something}}</h1>
  </div>
  <div v-else>
    Loading...
  </div>
</template>

import {mapGetters} from 'vuex'

export default {
  computed: {
    ...mapGetters(['infoState'])
  }
}

Example

We provided a lot of way using our libary including.


Unit Test [For Contributors]

If you clone this repo and did some changes do not forget to update the test file then..

Test via npm

npm run test

Test via yarn

yarn test