No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    How to use Quasar plugins (Notify, Loading) inside Vuex modules?

    Help
    4
    11
    6939
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      mas last edited by mas

      I’m trying to initiate/stop ‘Loading’ or display ‘Notify’ messages right in Vuex upon updating certain properties or upon receiving certain responses from axios rather than repeat the same code in multiple components. What’s the correct way to import these Quasar plugins in Vuex?

      Thank you.

      stukki 1 Reply Last reply Reply Quote 0
      • metalsadman
        metalsadman last edited by metalsadman

        @mas If you are using axios, you can do it in your axios boot file, then use axios request/response interceptors. Let’s see what have you tried first…

        1 Reply Last reply Reply Quote 0
        • stukki
          stukki @mas last edited by

          @mas
          To use Notify in Vuex with Axios, I’ve implemented as follows

          import { Notify } from 'quasar'
          
          ...
          
          
          export const postAction = (context) => {
            Vue.prototype.$axios.post(process.env.API + 'xxxx/' + 'xxxx/', {
              id: context.state.selectedPersonId,
              title: context.state.personSelectedAllData.person.title,
              ....
              
                })
              .then(function (response) {
                if (response.status === 200) {
                    Notify.create({
                      type: 'positive',
                      color: 'positive',
                      timeout: 1000,
                      position: 'center',
                      message: 'Yeah. Data saved. Great Job!'
                    })
                }
              })
              .catch(function (error) {
                console.log(error)
                  Notify.create({
                    type: 'warning',
                    color: 'warning',
                    timeout: 1000,
                    position: 'center',
                    message: 'Uups. Something went wrong!'
                  })
              })
          }```
          M 1 Reply Last reply Reply Quote 3
          • M
            mas last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • M
              mas @stukki last edited by

              @stukki, thank you - exactly what I needed!

              1 Reply Last reply Reply Quote 0
              • danielcommesse
                danielcommesse last edited by danielcommesse

                @mas you could do it simpler like this:

                export function myAPI (context, data) {
                  this._vm.$q.loading.show()
                  this._vm.$axios.post(`my-API-path`, data).then(response => {
                    this._vm.$q.loading.hide()
                    this._vm.$q.notify({
                      message: 'Your message',
                      color: 'positive'
                    }
                  }).catch(error => {
                    this._vm.$q.loading.hide()
                    this._vm.$q.notify({
                      message: 'Your error',
                      color: 'negative'
                    }  
                  })
                }
                
                danielcommesse M 2 Replies Last reply Reply Quote 3
                • danielcommesse
                  danielcommesse @danielcommesse last edited by danielcommesse

                  @mas You may even return the $axios promise and catch it at the component level:

                  export function myAPI (context, data) {
                    this._vm.$q.loading.show()
                    return this._vm.$axios.post(`my-API-path`, data)
                  }
                  

                  Then in your component

                  import { mapActions } from 'vuex'
                  export default {
                    methods: {
                      ...mapActions(['myAPI'])
                      // Or if you're action is in a module
                      ...mapActions('you-module', ['myAPI'])
                    },
                    hitMyAPI () {
                      this.myAPI().then(response => {
                        this.$q.loading.hide()
                        this.$q.notify({
                          message: 'Your message',
                          color: 'positive'
                        }
                      }).catch(error => {
                        this.$q.loading.hide()
                        this.$q.notify({
                          message: 'Your error',
                          color: 'negative'
                        }
                      })
                    }
                  }
                  
                  1 Reply Last reply Reply Quote 0
                  • M
                    mas @danielcommesse last edited by

                    @danielcoliev , thank you for suggestion. But which of the above options would be more preferable for performance and maybe other reasons?

                    1 Reply Last reply Reply Quote 0
                    • danielcommesse
                      danielcommesse last edited by danielcommesse

                      @mas For performance I really don’t know, I don’t think it really makes a significant difference, in my case I use to catch the axios promise in the action when is an action that I use only one time or the then/catch proccess is the same wherever I use it, but I have times where I have a same action that I use in different places, and depending where I use it the then/catch proccess is different, then I handle it at the component level. For example:

                      I have an endpoint in my API where I retrieve notifications, but I show them on different places and devices (cordova, pwa, etc…) and I handle the then/catch different based on where the notifications are shown and which device is being shown on. For that I prefer to handle them at the component level and not crowd my actions file. Hope it helps!

                      1 Reply Last reply Reply Quote 0
                      • metalsadman
                        metalsadman last edited by metalsadman

                        if you want to handle every request/response, better do it in your axios boot file via interceptors like i stated above, boot file has access to the store too.
                        example:

                        import { Loading, QSpinnerGears, Notify } from 'quasar'
                        import axios from 'axios'
                        export default ({ Vue, store }) => {
                        ...
                          // add a request interceptor
                          axios.interceptors.request.use(config => {
                            // change some state in your store here
                            // Do something before request is sent
                            Loading.show({
                              spinner: QSpinnerGears,
                              // other props
                            })
                            return config
                          }, error => {
                            // Do something with request error
                            Notify.create(...)
                            Loading.hide()
                            return Promise.reject(error)
                          })
                        // Add a response interceptor
                          axios.interceptors.response.use(response => {
                            // Do something with response data
                            // call some store functions etc..
                            Loading.hide()
                            return response
                          }, error => {
                           Notify.create(...)
                           Loading.hide()
                            // Do something with response error       
                            return Promise.reject(error)
                          })
                          Vue.prototype.$axios = axios
                        }
                        

                        https://github.com/axios/axios#interceptors

                        1 Reply Last reply Reply Quote 1
                        • M
                          mas last edited by

                          @metalsadman, @danielcoliev , thank you! It took a while to understand some things properly)

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post