1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-01-12 01:52:49 +00:00

client - add services

This commit is contained in:
2018-01-08 22:27:21 -06:00
parent 5ffa2753ed
commit 43615213d8
13 changed files with 93 additions and 35 deletions

View File

@@ -0,0 +1,18 @@
import { cloneDeep } from 'lodash';
import axios from '../axios/axios';
import { CategoryModel } from '../model';
export class CategoryService {
private static categoryCache: CategoryModel[];
public static async getCategories(): Promise<CategoryModel[]> {
// return cache if it exists
if (CategoryService.categoryCache) {
return Promise.resolve(cloneDeep(CategoryService.categoryCache));
}
const res = await axios.get('/api/category');
CategoryService.categoryCache = cloneDeep(res.data.data);
return res.data.data;
}
}

View File

@@ -0,0 +1,2 @@
export * from './category.service';
export * from './user.service';

View File

@@ -0,0 +1,24 @@
import axios from '../axios/axios';
import { UserModel } from '../model';
export class UserService {
public static storeUser(user: UserModel): void {
localStorage.setItem('user', JSON.stringify(user));
}
public static getUser(): UserModel {
const u = localStorage.getItem('user');
return u ? JSON.parse(u) : null;
}
// fetch user and store in local storage
public static async authorize(code: string): Promise<void> {
try {
const res = await axios.post('/api/battlenet/authorize', { code });
UserService.storeUser(res.data.data);
} catch (e) {
console.error(e);
}
}
}