First step from vuex to pinia
This commit is contained in:
69
web/src/stores/session.ts
Normal file
69
web/src/stores/session.ts
Normal file
@ -0,0 +1,69 @@
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
interface State {
|
||||
Token: string | null
|
||||
User: string | null
|
||||
Budgets: Map<string, Budget>,
|
||||
}
|
||||
|
||||
export interface Budget {
|
||||
ID: string
|
||||
Name: string
|
||||
AvailableBalance: number
|
||||
}
|
||||
|
||||
export const useSessionStore = defineStore('session', {
|
||||
// convert to a function
|
||||
state: (): State => ({
|
||||
Token: null,
|
||||
User: null,
|
||||
Budgets: new Map<string, Budget>(),
|
||||
}),
|
||||
getters: {
|
||||
Budgets(): IterableIterator<Budget> {
|
||||
return this.Budgets.values();
|
||||
},
|
||||
/*// must define return type because of using `this`
|
||||
fullUserDetails (state): FullUserDetails {
|
||||
// import from other stores
|
||||
const authPreferencesStore = useAuthPreferencesStore()
|
||||
const authEmailStore = useAuthEmailStore()
|
||||
return {
|
||||
...state,
|
||||
// other getters now on `this`
|
||||
fullName: this.fullName,
|
||||
...authPreferencesStore.$state,
|
||||
...authEmailStore.details
|
||||
}
|
||||
|
||||
// alternative if other modules are still in Vuex
|
||||
// return {
|
||||
// ...state,
|
||||
// fullName: this.fullName,
|
||||
// ...vuexStore.state.auth.preferences,
|
||||
// ...vuexStore.getters['auth/email'].details
|
||||
// }
|
||||
}*/
|
||||
},
|
||||
actions: {
|
||||
loginSuccess(x : any) {
|
||||
this.User = x.User;
|
||||
this.Token = x.Token;
|
||||
this.Budgets = x.Budgets;
|
||||
},
|
||||
login(login: any) {
|
||||
return fetch("/api/v1/user/login", { method: "POST", body: JSON.stringify(login) })
|
||||
.then(x => x.json())
|
||||
.then(x => this.loginSuccess(x));
|
||||
},
|
||||
register(login : any) {
|
||||
return fetch("/api/v1/user/register", { method: "POST", body: JSON.stringify(login) })
|
||||
.then(x => x.json())
|
||||
.then(x => this.loginSuccess(x))
|
||||
},
|
||||
// easily reset state using `$reset`
|
||||
logout() {
|
||||
this.$reset()
|
||||
}
|
||||
}
|
||||
})
|
Reference in New Issue
Block a user