Skip to content

Instantly share code, notes, and snippets.

@mitchell-garcia
Last active December 18, 2017 04:35
Show Gist options
  • Save mitchell-garcia/b957962289c753e8660c9157ec63eb26 to your computer and use it in GitHub Desktop.
Save mitchell-garcia/b957962289c753e8660c9157ec63eb26 to your computer and use it in GitHub Desktop.
Store Module Example with vuex-typex
import Vue from 'vue'
import { getStoreBuilder } from 'vuex-typex';
import Vuex, { Store } from 'vuex'
import { UserModule } from "./userModule";
export interface RootState {
user: UserModule;
}
Vue.use(Vuex);
export default getStoreBuilder<RootState>().vuexStore();
import UserModule from "./userModule"
UserModule
.dispatchLoginUser(({
username: 111
}))
.then(() => console.log('Logged in'))
.catch(err => console.error(err));
UserModule.getTokenButChristmas();
import { getStoreBuilder } from 'vuex-typex'
import { ActionContext } from 'vuex';
import { RootState } from './' // Typed root state
import ApiClient from './../services/ApiClient';
// Typed initial state of this module
export type UserModule = { token: string };
const initialState: UserModule = { token: "" };
const userModule = getStoreBuilder<RootState>().module("user", initialState);
function loginUser(context: ActionContext<UserModule, RootState>, payload: { username: string }): Promise<void> {
return ApiClient.loginUser(payload.username, "").then(resp => user.commitUserName(resp));
}
function getTokenButChristmas(context: UserModule) {
return `${context.token}-falalalala`;
}
function commitUserName(context: UserModule, token: string) {
context.token = token;
}
const user = {
dispatchLoginUser: userModule.dispatch(loginUser),
getTokenButChristmas: userModule.read(getTokenButChristmas),
commitUserName: userModule.commit(commitUserName)
};
export default user;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment