Skip to content

Commit 9ab554a

Browse files
committed
setup completed.
1 parent 2721041 commit 9ab554a

File tree

13 files changed

+299
-55
lines changed

13 files changed

+299
-55
lines changed

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@
66
"@testing-library/jest-dom": "^4.2.4",
77
"@testing-library/react": "^9.3.2",
88
"@testing-library/user-event": "^7.1.2",
9+
"@types/axios": "^0.14.0",
910
"@types/jest": "^24.0.0",
1011
"@types/node": "^12.0.0",
1112
"@types/react": "^16.9.0",
1213
"@types/react-dom": "^16.9.0",
14+
"@types/react-redux": "^7.1.6",
15+
"@types/redux": "^3.6.0",
16+
"@types/redux-thunk": "^2.1.0",
17+
"axios": "^0.19.1",
1318
"react": "^16.12.0",
1419
"react-dom": "^16.12.0",
20+
"react-redux": "^7.1.3",
1521
"react-scripts": "3.3.0",
22+
"redux": "^4.0.5",
23+
"redux-thunk": "^2.3.0",
1624
"typescript": "~3.7.2"
1725
},
1826
"scripts": {

src/App.css

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,7 @@
22
text-align: center;
33
}
44

5-
.App-logo {
6-
height: 40vmin;
7-
pointer-events: none;
8-
}
9-
10-
@media (prefers-reduced-motion: no-preference) {
11-
.App-logo {
12-
animation: App-logo-spin infinite 20s linear;
13-
}
14-
}
15-
16-
.App-header {
17-
background-color: #282c34;
18-
min-height: 100vh;
19-
display: flex;
20-
flex-direction: column;
21-
align-items: center;
22-
justify-content: center;
23-
font-size: calc(10px + 2vmin);
24-
color: white;
25-
}
26-
27-
.App-link {
28-
color: #61dafb;
29-
}
30-
31-
@keyframes App-logo-spin {
32-
from {
33-
transform: rotate(0deg);
34-
}
35-
to {
36-
transform: rotate(360deg);
37-
}
5+
.postDiv {
6+
margin: 30;
7+
border: 2px solid #ffabff;
388
}

src/App.tsx

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1-
import React from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
1+
import React from "react";
2+
import logo from "./logo.svg";
3+
import "./App.css";
4+
import Post from "./components/post";
5+
import { Provider } from "react-redux";
6+
import store from "./store";
47

58
const App: React.FC = () => {
69
return (
7-
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.tsx</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
21-
</header>
22-
</div>
10+
<Provider store={store}>
11+
<div className="App">
12+
<Post />
13+
</div>
14+
</Provider>
2315
);
24-
}
16+
};
2517

2618
export default App;

src/actions/post.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Dispatch, AnyAction } from "redux";
2+
import axios from "axios";
3+
import { setFetchPost } from "./types/post";
4+
5+
export const getAllPosts = () => {
6+
return async (dispatch: Dispatch<AnyAction>) => {
7+
axios
8+
.get("http://jsonplaceholder.typicode.com/posts?_limit=10")
9+
.then(res => {
10+
console.log("called.");
11+
dispatch(setFetchPost(res.data));
12+
})
13+
.catch(err => console.log(err));
14+
};
15+
};

src/actions/types/post.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PostState } from "../../reducers/types/Post";
2+
3+
export const FETCH_POST = "@posts/FETCH_POST";
4+
5+
export type FetchPost = {
6+
type: typeof FETCH_POST;
7+
payload: PostState[];
8+
};
9+
10+
export const setFetchPost = (posts: PostState[]) => {
11+
return {
12+
type: FETCH_POST,
13+
payload: posts
14+
};
15+
};

src/components/container.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { connect } from "react-redux";
2+
import { StoreState } from "../store/storetypes";
3+
import { ThunkDispatch } from "redux-thunk";
4+
import { Action } from "redux";
5+
import { getAllPosts } from "../actions/post";
6+
import { PostState } from "../reducers/types/Post";
7+
8+
export interface MapDispatchToProps {
9+
getAllPosts: () => void;
10+
}
11+
12+
const mapDispatchToState = (
13+
dispatch: ThunkDispatch<StoreState, null, Action>
14+
): MapDispatchToProps => {
15+
return {
16+
getAllPosts: () => dispatch(getAllPosts())
17+
};
18+
};
19+
20+
export interface MapStateToProps {
21+
posts: PostState[];
22+
}
23+
24+
const mapStateToProps = (state: StoreState): MapStateToProps => {
25+
return {
26+
posts: state.posts
27+
};
28+
};
29+
30+
export const container = connect(mapStateToProps, mapDispatchToState);
31+
export default container;

src/components/post.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React, { Component } from "react";
2+
3+
import { PostState } from "../reducers/types/Post";
4+
5+
import container, { MapStateToProps, MapDispatchToProps } from "./container";
6+
7+
export type InitialPostState = {
8+
posts: Array<PostState>;
9+
};
10+
11+
export const initialPostValue: InitialPostState = {
12+
posts: []
13+
};
14+
15+
export type PostProps = MapDispatchToProps & MapStateToProps;
16+
17+
export class Post extends Component<PostProps, InitialPostState> {
18+
constructor(props: PostProps) {
19+
super(props);
20+
this.state = {
21+
posts: []
22+
};
23+
}
24+
25+
componentDidMount() {
26+
this.props.getAllPosts();
27+
}
28+
29+
render() {
30+
const { posts } = this.props;
31+
console.log("posts ", posts);
32+
console.log("post length", posts.length);
33+
34+
return (
35+
<div>
36+
{this.props.posts.length > 1 ? (
37+
<div>
38+
{this.props.posts.map((p: PostState) => {
39+
return (
40+
<div key={p.id} className="postDiv">
41+
<div>Title: {p.title}</div>
42+
<div>{`Body ${p.body}`}</div>
43+
</div>
44+
);
45+
})}
46+
</div>
47+
) : (
48+
<div>loading.........</div>
49+
)}
50+
</div>
51+
);
52+
}
53+
}
54+
55+
export default container(Post);

src/reducers/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { combineReducers } from "redux";
2+
import { postReducer as posts } from "./postReducer";
3+
4+
const rootReduer = combineReducers({
5+
posts
6+
});
7+
8+
export default rootReduer;

src/reducers/postReducer.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { PostState } from "./types/Post";
2+
import { Dispatch, AnyAction } from "redux";
3+
import { FETCH_POST } from "../actions/types/post";
4+
5+
export type InitialState = {
6+
posts: Array<PostState>;
7+
};
8+
9+
const initialState: InitialState = {
10+
posts: []
11+
};
12+
13+
export const postReducer = (state = initialState, action: AnyAction) => {
14+
switch (action.type) {
15+
case FETCH_POST:
16+
return action.payload;
17+
18+
default:
19+
return state;
20+
}
21+
};

src/reducers/types/Post.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface PostState {
2+
userId?: number;
3+
id?: number;
4+
title: string;
5+
body: string;
6+
}

0 commit comments

Comments
 (0)