mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-12 01:52:49 +00:00
client - use mobx as state container
This commit is contained in:
5
client/app/stores/stores.ts
Normal file
5
client/app/stores/stores.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import userStore from './user-store';
|
||||
|
||||
export const stores = {
|
||||
userStore,
|
||||
};
|
||||
38
client/app/stores/user-store.ts
Normal file
38
client/app/stores/user-store.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { action, observable } from 'mobx';
|
||||
import { UserModel } from '../model';
|
||||
import { resetAuthorizationHeader, setAuthorizationHeader } from '../axios/axios';
|
||||
|
||||
export class UserStore {
|
||||
|
||||
@observable user?: UserModel;
|
||||
|
||||
constructor() {
|
||||
// use timeout or axios won't be defined
|
||||
setTimeout(() => {
|
||||
this.getUserFromStorage();
|
||||
});
|
||||
}
|
||||
|
||||
@action setUser(user: UserModel) {
|
||||
localStorage.setItem('user', JSON.stringify(user));
|
||||
this.getUserFromStorage();
|
||||
}
|
||||
|
||||
@action private getUserFromStorage(): void {
|
||||
const u = localStorage.getItem('user');
|
||||
if (u) {
|
||||
this.user = JSON.parse(u);
|
||||
setAuthorizationHeader(this.user!.token);
|
||||
}
|
||||
}
|
||||
|
||||
// when the user logs out
|
||||
@action resetUser() {
|
||||
this.user = undefined;
|
||||
resetAuthorizationHeader();
|
||||
localStorage.removeItem('user');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default new UserStore();
|
||||
Reference in New Issue
Block a user