115 lines
2.7 KiB
TypeScript
115 lines
2.7 KiB
TypeScript
import Store from 'electron-store';
|
|
import { action, computed, observable } from 'mobx';
|
|
import { setHeader } from '../http';
|
|
import { ILeague } from '../model';
|
|
import { IItem } from '../model/item';
|
|
import { IStashTab } from '../model/stash-tab';
|
|
import { PoeService } from '../services';
|
|
import { StorageService } from '../services/storage.service';
|
|
const store = new Store();
|
|
|
|
export class AppStore {
|
|
@observable
|
|
public username?: string;
|
|
@observable
|
|
public sessionID?: string;
|
|
@observable
|
|
public activeLeague: ILeague;
|
|
@observable
|
|
public selectedTabIndex: number = 0;
|
|
@observable
|
|
public stashTabs: IStashTab[] = [];
|
|
@observable
|
|
public stashItems: IItem[] = [];
|
|
@observable
|
|
public leagues: ILeague[] = [];
|
|
@observable
|
|
public appReady: boolean;
|
|
|
|
constructor() {
|
|
this.bootstrap();
|
|
}
|
|
|
|
private async bootstrap() {
|
|
try {
|
|
this.username = StorageService.getUsername();
|
|
this.sessionID = StorageService.getSessionID();
|
|
if (this.sessionID) {
|
|
await this.loadLeagues();
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
this.appReady = true;
|
|
}
|
|
|
|
@action
|
|
private async loadLeagues() {
|
|
try {
|
|
const leagues = await PoeService.getLeagues();
|
|
if (!leagues) {
|
|
return;
|
|
}
|
|
this.leagues = leagues;
|
|
// set active league if not set
|
|
if (!this.activeLeague && this.leagues!.length > 0) {
|
|
this.setActiveLeague(this.leagues[0].id);
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
}
|
|
|
|
@computed
|
|
public get isLoggedIn() {
|
|
return this.username && this.sessionID;
|
|
}
|
|
|
|
@action
|
|
public resetState() {
|
|
delete this.username;
|
|
delete this.sessionID;
|
|
setHeader('Cookie', '');
|
|
store.clear();
|
|
}
|
|
|
|
@action
|
|
public setSessionID(sessionID: string) {
|
|
setHeader('Cookie', `POESESSID=${sessionID}`);
|
|
this.sessionID = sessionID;
|
|
StorageService.storeSessionID(sessionID);
|
|
}
|
|
|
|
@action
|
|
public setUsername(username: string) {
|
|
this.username = username;
|
|
StorageService.storeUsername(username);
|
|
}
|
|
|
|
@action
|
|
public setActiveLeague(id: string) {
|
|
const league = this.leagues.find(l => l.id === id);
|
|
if (league) {
|
|
this.activeLeague = league;
|
|
this.stashTabs = [];
|
|
this.selectedTabIndex = 0;
|
|
this.stashItems = [];
|
|
this.loadItems(this.selectedTabIndex);
|
|
}
|
|
}
|
|
|
|
@action
|
|
public async performLogin(sessionID: string) {
|
|
this.setSessionID(sessionID);
|
|
const username = await PoeService.getUsername();
|
|
this.setUsername(username);
|
|
}
|
|
|
|
@action
|
|
public async loadItems(tabIndex: number | string) {
|
|
const data = await PoeService.getStash(this.username!, this.activeLeague.id, tabIndex);
|
|
this.stashTabs = data.tabs;
|
|
this.stashItems = data.items;
|
|
}
|
|
}
|