mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-10 17:12:48 +00:00
client - add services
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import Wrapper from './Wrapper';
|
import { Routes } from './routes';
|
||||||
import 'babel-polyfill';
|
import 'babel-polyfill';
|
||||||
|
|
||||||
ReactDOM.render(<Wrapper />, document.getElementById('app'));
|
ReactDOM.render(<Routes/>, document.getElementById('app'));
|
||||||
|
|||||||
@@ -18,8 +18,6 @@ export class LoginButton extends React.Component<Props, State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
console.log(process.env);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div {...this.props}>
|
<div {...this.props}>
|
||||||
<img src={require('../../assets/login-bot-left.gif')} />
|
<img src={require('../../assets/login-bot-left.gif')} />
|
||||||
|
|||||||
5
client/app/model/category.ts
Normal file
5
client/app/model/category.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export interface CategoryModel {
|
||||||
|
id: number;
|
||||||
|
category: string;
|
||||||
|
title: string;
|
||||||
|
}
|
||||||
2
client/app/model/index.ts
Normal file
2
client/app/model/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './category';
|
||||||
|
export * from './user';
|
||||||
3
client/app/model/user.ts
Normal file
3
client/app/model/user.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export interface UserModel {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,3 +3,5 @@ export * from './home/home';
|
|||||||
export * from './not-found/not-found';
|
export * from './not-found/not-found';
|
||||||
export * from './oauth/oauth';
|
export * from './oauth/oauth';
|
||||||
export * from './realms/realms';
|
export * from './realms/realms';
|
||||||
|
export * from './user-account/user-account';
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { RouteComponentProps } from 'react-router-dom';
|
import { RouteComponentProps } from 'react-router-dom';
|
||||||
import { parse } from 'query-string';
|
import { parse } from 'query-string';
|
||||||
import axios from '../../axios/axios';
|
import { UserService } from '../../services';
|
||||||
|
|
||||||
interface Props extends RouteComponentProps<any> {}
|
interface Props extends RouteComponentProps<any> {}
|
||||||
|
|
||||||
@@ -15,9 +15,7 @@ export class Oauth extends React.Component<Props, State> {
|
|||||||
private async login(queryString: string) {
|
private async login(queryString: string) {
|
||||||
try {
|
try {
|
||||||
const code = parse(queryString).code;
|
const code = parse(queryString).code;
|
||||||
const res = await axios.post('/api/battlenet/authorize', { code });
|
await UserService.authorize(code);
|
||||||
const account = res.data.data;
|
|
||||||
localStorage.setItem('account', JSON.stringify(account));
|
|
||||||
window.opener.location.reload();
|
window.opener.location.reload();
|
||||||
// TODO: this doesn't work on mobile currently
|
// TODO: this doesn't work on mobile currently
|
||||||
window.close();
|
window.close();
|
||||||
|
|||||||
@@ -1,25 +1,21 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { chain } from 'lodash';
|
import { chain } from 'lodash';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link, RouteComponentProps } from 'react-router-dom';
|
||||||
import axios from '../../axios/axios';
|
import { CategoryService } from '../../services';
|
||||||
|
import { CategoryModel } from '../../model';
|
||||||
import { ContentContainer } from '../../components';
|
import { ContentContainer } from '../../components';
|
||||||
import './realms.scss';
|
import './realms.scss';
|
||||||
import header_realmforums from '../../assets/header-realmforums.gif';
|
import header_realmforums from '../../assets/header-realmforums.gif';
|
||||||
import realms_large from '../../assets/realms-large.gif';
|
import realms_large from '../../assets/realms-large.gif';
|
||||||
|
|
||||||
interface Props {}
|
interface Props extends RouteComponentProps<any> {}
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
realms: Realm[];
|
realms: CategoryModel[];
|
||||||
}
|
|
||||||
|
|
||||||
interface Realm {
|
|
||||||
id: number;
|
|
||||||
category: string;
|
|
||||||
title: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Realms extends React.Component<Props, State> {
|
export class Realms extends React.Component<Props, State> {
|
||||||
|
|
||||||
constructor(props: Props) {
|
constructor(props: Props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = {
|
this.state = {
|
||||||
@@ -29,8 +25,8 @@ export class Realms extends React.Component<Props, State> {
|
|||||||
|
|
||||||
async componentDidMount() {
|
async componentDidMount() {
|
||||||
try {
|
try {
|
||||||
const res = await axios.get('/api/category');
|
const res = await CategoryService.getCategories();
|
||||||
const realms = chain(res.data.data)
|
const realms = chain(res)
|
||||||
.filter({ category: 'realm' })
|
.filter({ category: 'realm' })
|
||||||
.orderBy(['title'])
|
.orderBy(['title'])
|
||||||
.value();
|
.value();
|
||||||
@@ -40,7 +36,7 @@ export class Realms extends React.Component<Props, State> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private renderRealms(realms: Realm[]): any {
|
private renderRealms(realms: CategoryModel[]): any {
|
||||||
return realms.map((realm) => {
|
return realms.map((realm) => {
|
||||||
return (
|
return (
|
||||||
<li key={realm.id}>
|
<li key={realm.id}>
|
||||||
|
|||||||
21
client/app/pages/user-account/user-account.tsx
Normal file
21
client/app/pages/user-account/user-account.tsx
Normal file
@@ -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<any> {}
|
||||||
|
|
||||||
|
interface State {}
|
||||||
|
|
||||||
|
export class UserAccount extends React.Component<Props, State> {
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
console.log(UserService.getUser());
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<ContentContainer></ContentContainer>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { BrowserRouter, Route, Switch } from 'react-router-dom';
|
import { BrowserRouter, Route, Switch } from 'react-router-dom';
|
||||||
|
|
||||||
import { Footer, Header } from './components';
|
import { Footer, Header } from './components';
|
||||||
import { Forum, Home, NotFound, Oauth, Realms } from './pages';
|
import { Forum, Home, NotFound, Oauth, Realms, UserAccount } from './pages';
|
||||||
|
|
||||||
// styling
|
// styling
|
||||||
import './scss/index.scss';
|
import './scss/index.scss';
|
||||||
@@ -11,18 +10,7 @@ interface Props {}
|
|||||||
|
|
||||||
interface State {}
|
interface State {}
|
||||||
|
|
||||||
export default class Wrapper extends React.Component<Props, State> {
|
export class Routes extends React.Component<Props, State> {
|
||||||
constructor(props: Props) {
|
|
||||||
super(props);
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
const storedAccount = localStorage.getItem('account');
|
|
||||||
|
|
||||||
if (storedAccount) {
|
|
||||||
console.log(JSON.parse(storedAccount));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public render() {
|
public render() {
|
||||||
return (
|
return (
|
||||||
@@ -34,6 +22,7 @@ export default class Wrapper extends React.Component<Props, State> {
|
|||||||
<Route exact path="/realms" component={Realms} />
|
<Route exact path="/realms" component={Realms} />
|
||||||
<Route exact path="/f/:id" component={Forum} />
|
<Route exact path="/f/:id" component={Forum} />
|
||||||
<Route exact path="/oauth" component={Oauth} />
|
<Route exact path="/oauth" component={Oauth} />
|
||||||
|
<Route exact path="/user-account" component={UserAccount} />
|
||||||
<Route component={NotFound} />
|
<Route component={NotFound} />
|
||||||
</Switch>
|
</Switch>
|
||||||
<Footer />
|
<Footer />
|
||||||
18
client/app/services/category.service.ts
Normal file
18
client/app/services/category.service.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
2
client/app/services/index.ts
Normal file
2
client/app/services/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './category.service';
|
||||||
|
export * from './user.service';
|
||||||
24
client/app/services/user.service.ts
Normal file
24
client/app/services/user.service.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user