Files
poe-auto-pricer/app/stores/app.store.ts

138 lines
3.3 KiB
TypeScript

import Store from 'electron-store';
import * as _ from 'lodash';
import { action, computed, observable } from 'mobx';
import { IItemPrice, 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(this.sessionCookieHeader);
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() {
this.username = undefined;
this.sessionID = undefined;
this.activeLeague = undefined;
this.selectedTabIndex = 0;
this.stashTabs = [];
this.stashItems = [];
this.leagues = [];
store.clear();
}
@action
public setSessionID(sessionID: string) {
this.sessionID = sessionID;
StorageService.storeSessionID(sessionID);
}
@computed
public get sessionCookieHeader(): { Cookie: string } {
return {
Cookie: `POESESSID=${this.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.sessionCookieHeader);
this.setUsername(username);
this.loadLeagues();
}
@action
public async loadItems(tabIndex: number | string) {
PoeService.clearRequestQueue();
const data = await PoeService.getStash(this.username!, this.activeLeague!.id, tabIndex);
this.stashTabs = data.tabs;
this.stashItems = data.items;
}
/**
* Updates the price of an item in the list - updates based on id
*/
@action
public updateItemPrice(id: string, itemPrice: IItemPrice) {
const item = _.find(this.stashItems, i => i.id === id);
if (item) {
item.itemPrice = itemPrice;
}
}
}