1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-01-11 09:32:51 +00:00

client - refactor services

This commit is contained in:
2018-01-10 01:35:26 +00:00
parent e63514ff45
commit 8a39f687a8
2 changed files with 33 additions and 29 deletions

View File

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

View File

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