Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a useModule composable #1826

Closed
petervmeijgaard opened this issue Sep 10, 2020 · 2 comments
Closed

Add a useModule composable #1826

petervmeijgaard opened this issue Sep 10, 2020 · 2 comments

Comments

@petervmeijgaard
Copy link

petervmeijgaard commented Sep 10, 2020

What problem does this feature solve?

Currently, the only way to use the store using the composition API is by creating a store with useStore. Using modules this way, results in messier code than it should be. I would like to introduce a useModule hook, which will export commit, dispatch, getters and state the same way as useStore does.

What does the proposed API look like?

Basic example

Current situation

// useAuth.js
import { useStore } from 'vuex';

export const useAuth = () => {
  const store = useStore();
  const isAuthenticated = computed(() => store.state.auth.isAuthenticated));
  const login = (payload) => {
    store.commit('auth/login', payload);
  };
  const logout = () => {
    store.dispatch('auth/logout');
  };

  export const {
    isAuthenticated,
    login,
    logout,
  };
};

Proposed situation:

// useAuth.js
import { useModule } from 'vuex';

export const useAuth = () => {
  const auth = useModule('auth');
  const isAuthenticated = computed(() => auth.state.isAuthenticated));
  const login = (payload) => {
    auth.commit('login', payload);
  };
  const logout = () => {
    auth.dispatch('logout');
  };

  export const {
    isAuthenticated,
    login,
    logout,
  };
};

Multiple store modules with nested modules

Current situation:

// useAuth.js
import { useModule } from 'vuex';

export const useAuth = () => {
  const store = useStore();

  const isAuthenticated = computed(() => store.state.auth.isAuthenticated);
  const myDetailsInformation = computed(() => store.state.account.myDetails.information);

  const login = async (payload) => {
    await auth.dispatch('auth/login', payload);
    await myDetails.dispatch('account/my-details/fetch-information');
  };
  const logout = () => {
    store.commit('logout');
  };

  export const {
    isAuthenticated,
    myDetailsInformation,
    login,
    logout,
  };
};

Proposed situation:

// useAuth.js
import { useModule } from 'vuex';

export const useAuth = () => {
  const auth = useModule('auth');
  const myDetails = useModule('account/my-details');

  const isAuthenticated = computed(() => auth.state.isAuthenticated);
  const myDetailsInformation = computed(() => myDetails.state.information);

  const login = async (payload) => {
    await auth.dispatch('login', payload);
    await myDetails.dispatch('fetch-information');
  };
  const logout = () => {
    store.commit('logout');
  };

  export const {
    isAuthenticated,
    myDetailsInformation,
    login,
    logout,
  };
};
@petervmeijgaard
Copy link
Author

Oh, I saw that this is basically the same as proposed in #1725

@petervmeijgaard
Copy link
Author

And a dupe from #1695 as well. Closing this one 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant