Last active
December 18, 2017 04:35
-
-
Save mitchell-garcia/b957962289c753e8660c9157ec63eb26 to your computer and use it in GitHub Desktop.
Store Module Example with vuex-typex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UserModule from "./userModule" | |
UserModule | |
.dispatchLoginUser(({ | |
username: 111 | |
})) | |
.then(() => console.log('Logged in')) | |
.catch(err => console.error(err)); | |
UserModule.getTokenButChristmas(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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