From: Eduardo San Martin Morote Date: Fri, 8 Jul 2022 07:45:53 +0000 (+0200) Subject: chore: add test of whole store X-Git-Tag: @pinia/nuxt@0.2.0~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ed5800b3ca28dfd3bbcdcc5e0556b42364228fa;p=thirdparty%2Fvuejs%2Fpinia.git chore: add test of whole store --- diff --git a/packages/playground/src/stores/wholeStore.ts b/packages/playground/src/stores/wholeStore.ts new file mode 100644 index 00000000..3f59971e --- /dev/null +++ b/packages/playground/src/stores/wholeStore.ts @@ -0,0 +1,46 @@ +interface User { + id: string +} +type State = { type: T } +type AuthStateLoggingIn = State<'loggingIn'> +type AuthStateLoggedIn = State<'loggedIn'> & { user: User } +type AuthStateError = State<'error'> & { errorMsg: string } +type AuhtStateLoggedOut = State<'loggedOut'> + +export type AuthState = + | AuthStateLoggingIn + | AuthStateLoggedIn + | AuthStateError + | AuhtStateLoggedOut + +import { acceptHMRUpdate, defineStore } from 'pinia' + +const delay = (t: number) => new Promise((r) => setTimeout(r, t)) + +export const useWholeStore = defineStore('whole', { + state: (): AuthState => ({ type: 'loggedIn', user: { id: '1' } }), + + getters: { + errorMsg: (state) => (state.type === 'error' ? state.errorMsg : ''), + }, + + actions: { + async login() { + try { + await delay(1000) + this.$patch({ type: 'loggedIn', user: { id: '1' } }) + } catch (e) { + const error = e as Error + this.$patch({ type: 'error', errorMsg: error.message }) + } + }, + + logout() { + this.$patch({ type: 'loggedOut' }) + }, + }, +}) + +if (import.meta.hot) { + import.meta.hot.accept(acceptHMRUpdate(useWholeStore, import.meta.hot)) +}