File tree Expand file tree Collapse file tree 13 files changed +299
-55
lines changed Expand file tree Collapse file tree 13 files changed +299
-55
lines changed Original file line number Diff line number Diff line change 6
6
"@testing-library/jest-dom" : " ^4.2.4" ,
7
7
"@testing-library/react" : " ^9.3.2" ,
8
8
"@testing-library/user-event" : " ^7.1.2" ,
9
+ "@types/axios" : " ^0.14.0" ,
9
10
"@types/jest" : " ^24.0.0" ,
10
11
"@types/node" : " ^12.0.0" ,
11
12
"@types/react" : " ^16.9.0" ,
12
13
"@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" ,
13
18
"react" : " ^16.12.0" ,
14
19
"react-dom" : " ^16.12.0" ,
20
+ "react-redux" : " ^7.1.3" ,
15
21
"react-scripts" : " 3.3.0" ,
22
+ "redux" : " ^4.0.5" ,
23
+ "redux-thunk" : " ^2.3.0" ,
16
24
"typescript" : " ~3.7.2"
17
25
},
18
26
"scripts" : {
Original file line number Diff line number Diff line change 2
2
text-align : center;
3
3
}
4
4
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 ;
38
8
}
Original file line number Diff line number Diff line change 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" ;
4
7
5
8
const App : React . FC = ( ) => {
6
9
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 >
23
15
) ;
24
- }
16
+ } ;
25
17
26
18
export default App ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change
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 ) ;
Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
1
+ export interface PostState {
2
+ userId ?: number ;
3
+ id ?: number ;
4
+ title : string ;
5
+ body : string ;
6
+ }
You can’t perform that action at this time.
0 commit comments