46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import {
|
|
Action,
|
|
getModule,
|
|
Module,
|
|
Mutation,
|
|
MutationAction,
|
|
VuexModule
|
|
} from 'vuex-module-decorators';
|
|
import { LoginSubmit, User } from '@/models';
|
|
import api from '@/services/pm-api-client';
|
|
import store from '@/store';
|
|
import { logService } from '@/services/logging';
|
|
|
|
@Module({ namespaced: true, name: 'user', store, dynamic: true })
|
|
class UserStoreModule extends VuexModule {
|
|
public user: User | null = null;
|
|
public users: User[] = [];
|
|
|
|
private log = logService.getLogger('/store-modules/user');
|
|
|
|
@MutationAction({ mutate: ['user']})
|
|
public async fetchUser() {
|
|
return { user: await api.getUser() };
|
|
}
|
|
|
|
@MutationAction({ mutate: ['users'] })
|
|
public async fetchAllUsers() {
|
|
return { users: await api.getAllUsers() };
|
|
}
|
|
|
|
@MutationAction({ mutate: ['users'] })
|
|
public async createUser(user: User) {
|
|
const newUser = await api.createUser(user);
|
|
return { users: this.users.concat([user]) };
|
|
}
|
|
|
|
@MutationAction({ mutate: ['users'] })
|
|
public async deleteUser(userId: string) {
|
|
await api.deleteUser(userId);
|
|
return { users: this.users.filter((u) => u.id !== userId) };
|
|
}
|
|
}
|
|
|
|
export const userStore = getModule(UserStoreModule);
|
|
export default userStore;
|