})
diff --git a/client/app/model/category.ts b/client/app/model/category.ts
new file mode 100644
index 0000000..4cac201
--- /dev/null
+++ b/client/app/model/category.ts
@@ -0,0 +1,5 @@
+export interface CategoryModel {
+ id: number;
+ category: string;
+ title: string;
+}
diff --git a/client/app/model/index.ts b/client/app/model/index.ts
new file mode 100644
index 0000000..6ba47fd
--- /dev/null
+++ b/client/app/model/index.ts
@@ -0,0 +1,2 @@
+export * from './category';
+export * from './user';
diff --git a/client/app/model/user.ts b/client/app/model/user.ts
new file mode 100644
index 0000000..7f6132b
--- /dev/null
+++ b/client/app/model/user.ts
@@ -0,0 +1,3 @@
+export interface UserModel {
+
+}
diff --git a/client/app/pages/index.ts b/client/app/pages/index.ts
index 5ebb9d8..45a4002 100644
--- a/client/app/pages/index.ts
+++ b/client/app/pages/index.ts
@@ -3,3 +3,5 @@ export * from './home/home';
export * from './not-found/not-found';
export * from './oauth/oauth';
export * from './realms/realms';
+export * from './user-account/user-account';
+
diff --git a/client/app/pages/oauth/oauth.tsx b/client/app/pages/oauth/oauth.tsx
index 468686e..ea2304f 100644
--- a/client/app/pages/oauth/oauth.tsx
+++ b/client/app/pages/oauth/oauth.tsx
@@ -1,7 +1,7 @@
import React from 'react';
import { RouteComponentProps } from 'react-router-dom';
import { parse } from 'query-string';
-import axios from '../../axios/axios';
+import { UserService } from '../../services';
interface Props extends RouteComponentProps
{}
@@ -15,9 +15,7 @@ export class Oauth extends React.Component {
private async login(queryString: string) {
try {
const code = parse(queryString).code;
- const res = await axios.post('/api/battlenet/authorize', { code });
- const account = res.data.data;
- localStorage.setItem('account', JSON.stringify(account));
+ await UserService.authorize(code);
window.opener.location.reload();
// TODO: this doesn't work on mobile currently
window.close();
diff --git a/client/app/pages/realms/realms.tsx b/client/app/pages/realms/realms.tsx
index c6963ed..2dc997d 100644
--- a/client/app/pages/realms/realms.tsx
+++ b/client/app/pages/realms/realms.tsx
@@ -1,25 +1,21 @@
import React from 'react';
import { chain } from 'lodash';
-import { Link } from 'react-router-dom';
-import axios from '../../axios/axios';
+import { Link, RouteComponentProps } from 'react-router-dom';
+import { CategoryService } from '../../services';
+import { CategoryModel } from '../../model';
import { ContentContainer } from '../../components';
import './realms.scss';
import header_realmforums from '../../assets/header-realmforums.gif';
import realms_large from '../../assets/realms-large.gif';
-interface Props {}
+interface Props extends RouteComponentProps {}
interface State {
- realms: Realm[];
-}
-
-interface Realm {
- id: number;
- category: string;
- title: string;
+ realms: CategoryModel[];
}
export class Realms extends React.Component {
+
constructor(props: Props) {
super(props);
this.state = {
@@ -29,8 +25,8 @@ export class Realms extends React.Component {
async componentDidMount() {
try {
- const res = await axios.get('/api/category');
- const realms = chain(res.data.data)
+ const res = await CategoryService.getCategories();
+ const realms = chain(res)
.filter({ category: 'realm' })
.orderBy(['title'])
.value();
@@ -40,7 +36,7 @@ export class Realms extends React.Component {
}
}
- private renderRealms(realms: Realm[]): any {
+ private renderRealms(realms: CategoryModel[]): any {
return realms.map((realm) => {
return (
diff --git a/client/app/pages/user-account/user-account.tsx b/client/app/pages/user-account/user-account.tsx
new file mode 100644
index 0000000..b36338a
--- /dev/null
+++ b/client/app/pages/user-account/user-account.tsx
@@ -0,0 +1,21 @@
+import React from 'react';
+import { RouteComponentProps } from 'react-router-dom';
+import { ContentContainer } from '../../components';
+import { UserService } from '../../services';
+
+interface Props extends RouteComponentProps {}
+
+interface State {}
+
+export class UserAccount extends React.Component {
+
+ componentDidMount() {
+ console.log(UserService.getUser());
+ }
+
+ render() {
+ return (
+
+ );
+ }
+}
diff --git a/client/app/Wrapper.tsx b/client/app/routes.tsx
similarity index 66%
rename from client/app/Wrapper.tsx
rename to client/app/routes.tsx
index 084ca4f..f070034 100644
--- a/client/app/Wrapper.tsx
+++ b/client/app/routes.tsx
@@ -1,8 +1,7 @@
import React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
-
import { Footer, Header } from './components';
-import { Forum, Home, NotFound, Oauth, Realms } from './pages';
+import { Forum, Home, NotFound, Oauth, Realms, UserAccount } from './pages';
// styling
import './scss/index.scss';
@@ -11,18 +10,7 @@ interface Props {}
interface State {}
-export default class Wrapper extends React.Component {
- constructor(props: Props) {
- super(props);
- }
-
- componentDidMount() {
- const storedAccount = localStorage.getItem('account');
-
- if (storedAccount) {
- console.log(JSON.parse(storedAccount));
- }
- }
+export class Routes extends React.Component {
public render() {
return (
@@ -34,6 +22,7 @@ export default class Wrapper extends React.Component {
+
diff --git a/client/app/services/category.service.ts b/client/app/services/category.service.ts
new file mode 100644
index 0000000..5229cc3
--- /dev/null
+++ b/client/app/services/category.service.ts
@@ -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 {
+ // 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;
+ }
+}
diff --git a/client/app/services/index.ts b/client/app/services/index.ts
new file mode 100644
index 0000000..7b2c63c
--- /dev/null
+++ b/client/app/services/index.ts
@@ -0,0 +1,2 @@
+export * from './category.service';
+export * from './user.service';
diff --git a/client/app/services/user.service.ts b/client/app/services/user.service.ts
new file mode 100644
index 0000000..ca6da2d
--- /dev/null
+++ b/client/app/services/user.service.ts
@@ -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 {
+ try {
+ const res = await axios.post('/api/battlenet/authorize', { code });
+ UserService.storeUser(res.data.data);
+ } catch (e) {
+ console.error(e);
+ }
+ }
+}