vuex-crud icon indicating copy to clipboard operation
vuex-crud copied to clipboard

react to request success/error with actions

Open ConnorJBishop opened this issue 6 years ago • 0 comments

First of all, Id like to say this is a great little library, great work!

My only issue is that we can only react to request errors/successes with custom mutations. I think it would be much more helpful if we could react with actions instead, giving us more flexibility, with access to rootState, ability to dispatch further actions etc ...

Although it would be a breaking change it seems a relatively straightforward one. For example, for the onFetchListSuccess and onFetchListError you could change

if (only.includes('FETCH_LIST')) {
    Object.assign(crudActions, {
      /**
       * GET /api/<resourceName>
       *
       * Fetch list of resources.
       */
      fetchList({ commit }, { config, customUrl, customUrlFnArgs = [] } = {}) {
        commit('fetchListStart');

        return client.get(urlGetter({ customUrl, customUrlFnArgs }), config)
          .then((res) => {
            const parsedResponse = parseList(res);

            commit('fetchListSuccess', parsedResponse);

            return parsedResponse;
          })
          .catch((err) => {
            const parsedError = parseError(err);

            commit('fetchListError', parsedError);

            return Promise.reject(parsedError);
          });
      }
    });
  }

to

if (only.includes('FETCH_LIST')) {
    Object.assign(crudActions, {
      /**
       * GET /api/<resourceName>
       *
       * Fetch list of resources.
       */
      fetchList({ commit, dispatch }, { config, customUrl, customUrlFnArgs = [] } = {}) {
        commit('fetchListStart');

        return client.get(urlGetter({ customUrl, customUrlFnArgs }), config)
          .then((res) => {
            const parsedResponse = parseList(res);

            dispatch('fetchListSuccess', parsedResponse);

            return parsedResponse;
          })
          .catch((err) => {
            const parsedError = parseError(err);

            dispatch('fetchListError', parsedError);

            return Promise.reject(parsedError);
          });
      }
    });
  }

I think that would work but forgive me if I'm missing something and this feature cant be implemented. If it can I do think it will make the library a lot more flexible/powerful.

Thanks, Connor

ConnorJBishop avatar May 23 '18 07:05 ConnorJBishop