Skip to content

Commit

Permalink
Update React hooks & Context api with MobX
Browse files Browse the repository at this point in the history
  • Loading branch information
amiru11 committed Feb 19, 2020
1 parent 07fa886 commit 4d62952
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 79 deletions.
21 changes: 20 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"react-app-polyfill": "^1.0.6",
"react-dom": "^16.12.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.1",
"react-scripts": "^3.4.0",
"typescript": "~3.7.2"
},
"scripts": {
Expand All @@ -34,6 +34,25 @@
"eslintConfig": {
"extends": "react-app"
},
"babel": {
"presets": [
"react-app"
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
[
"@babel/plugin-proposal-class-properties",
{
"loose": true
}
]
]
},
"browserslist": {
"production": [
">0.2%",
Expand Down
34 changes: 34 additions & 0 deletions src/api/posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import request from 'lib/request';
import { AxiosResponse } from 'axios';

export const getPosts = (): Promise<AxiosResponse> =>
request({
url: `https://jsonplaceholder.typicode.com/posts/`,
method: 'GET',
});

export const getPostById = (id: number): Promise<AxiosResponse> =>
request({
url: `https://jsonplaceholder.typicode.com/posts/${id}`,
method: 'GET',
});

export const createPosts = ({ data }: any): Promise<AxiosResponse> =>
request({
url: `https://jsonplaceholder.typicode.com/posts/`,
method: 'POST',
data,
headers: { 'Content-type': 'application/json; charset=UTF-8' },
});

export const editPosts = (id: number): Promise<AxiosResponse> =>
request({
url: `https://jsonplaceholder.typicode.com/posts/${id}`,
method: 'PUT',
});

export const deletePosts = (id: number): Promise<AxiosResponse> =>
request({
url: `https://jsonplaceholder.typicode.com/posts/${id}`,
method: 'DELETE',
});
26 changes: 26 additions & 0 deletions src/components/common/Input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, { Component } from 'react';

interface IInputProps {
type?: string;
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
value: string;
id?: string;
name: string;
}

class InputComponent extends Component<IInputProps> {
render() {
const { id, name, type, onChange, value } = this.props;
return (
<input
id={id ?? ''}
name={name}
type={type ?? 'text'}
onChange={onChange}
value={value ?? ''}
/>
);
}
}

export default InputComponent;
19 changes: 19 additions & 0 deletions src/components/common/TextArea.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, { Component } from 'react';

interface ITextAreaProps {
onChange: (event: React.ChangeEvent<HTMLTextAreaElement>) => void;
value: string;
id?: string;
name: string;
}

class InputComponent extends Component<ITextAreaProps> {
render() {
const { id, name, onChange, value } = this.props;
return (
<textarea id={id ?? ''} name={name} onChange={onChange} value={value} />
);
}
}

export default InputComponent;
4 changes: 4 additions & 0 deletions src/hooks/useStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react';
import RootStore from 'stores';

export default () => React.useContext(RootStore);
12 changes: 12 additions & 0 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Axios, { AxiosRequestConfig } from 'axios';

export default ({ ...config }: AxiosRequestConfig): Promise<any> =>
new Promise((resolve, reject) => {
Axios({ ...config })
.then(res => {
resolve(res);
})
.catch(err => {
reject(err);
});
});
Empty file added src/pages/counter/Counter.scss
Empty file.
25 changes: 25 additions & 0 deletions src/pages/counter/Counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import { observer } from 'mobx-react';
import classnames from 'classnames/bind';

import useStore from 'hooks/useStore';

import styles from './Counter.scss';

const cx = classnames.bind(styles);

const Counter = observer(() => {
const { counterStore } = useStore();
const { counter, increase, decrease } = counterStore;
return (
<div className={cx('counter-container')}>
<h2>{counter}</h2>
<div>
<button onClick={increase}>+</button>
<button onClick={decrease}>-</button>
</div>
</div>
);
});

export default Counter;
Empty file added src/pages/home/Home.scss
Empty file.
19 changes: 19 additions & 0 deletions src/pages/home/Home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { Link } from 'react-router-dom';
import classnames from 'classnames/bind';

import styles from './Home.scss';

const cx = classnames.bind(styles);

function Home():JSX.Element {
return (
<>
<section className={cx('section-2')}>
<Link to="/counter">Go to Counter test</Link>
</section>
</>
);
}

export default Home;
15 changes: 14 additions & 1 deletion src/shared/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import React from 'react';
import { Switch, Route } from 'react-router-dom';
import classnames from 'classnames/bind';

import Home from 'pages/home/Home';
import Counter from 'pages/counter/Counter';

import styles from './App.scss';

const cx = classnames.bind(styles);

function App(): JSX.Element {
return <div className={cx('App')}></div>;
return (
<div className={cx('app-container')}>
<main className={cx('main-container')}>
<Switch>
<Route path="/" component={Home} exact />
<Route path="/counter" component={Counter} />
</Switch>
</main>
</div>
);
}

export default App;
18 changes: 18 additions & 0 deletions src/stores/Couter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { observable, action } from 'mobx';

export default class CounterStore {
@observable
public counter: number = 0;

@action
increase = (): void => {
this.counter++;
console.log('counter', this.counter);
};

@action
decrease = (): void => {
this.counter--;
console.log('counter', this.counter);
};
}
8 changes: 8 additions & 0 deletions src/stores/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import CounterStore from './Couter';

const RootStore = React.createContext({
counterStore: new CounterStore(),
});

export default RootStore;
9 changes: 9 additions & 0 deletions src/stores/interfaces/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface ICounterStore {
counter: number;
increase: () => void;
decrease: () => void;
}

export interface IRootStore {
counterStore?: ICounterStore;
}
Loading

0 comments on commit 4d62952

Please sign in to comment.