Skip to content

Commit f4e4b8f

Browse files
author
Emre Avcılar
committed
cra typescript integration with redux and redux-thunk
1 parent 31816e5 commit f4e4b8f

35 files changed

+26264
-0
lines changed

Diff for: .gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

Diff for: package-lock.json

+14,670
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "typescript-trial",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^4.2.4",
7+
"@testing-library/react": "^9.3.2",
8+
"@testing-library/user-event": "^7.1.2",
9+
"@types/jest": "^24.0.0",
10+
"@types/node": "^12.0.0",
11+
"axios": "^0.19.2",
12+
"react": "^16.12.0",
13+
"react-dom": "^16.12.0",
14+
"react-redux": "^7.1.3",
15+
"react-router-dom": "^5.1.2",
16+
"react-scripts": "3.3.1",
17+
"redux": "^4.0.5",
18+
"redux-thunk": "^2.3.0",
19+
"typescript": "~3.7.2"
20+
},
21+
"scripts": {
22+
"start": "react-scripts start",
23+
"build": "react-scripts build",
24+
"test": "react-scripts test",
25+
"eject": "react-scripts eject"
26+
},
27+
"eslintConfig": {
28+
"extends": "react-app"
29+
},
30+
"browserslist": {
31+
"production": [
32+
">0.2%",
33+
"not dead",
34+
"not op_mini all"
35+
],
36+
"development": [
37+
"last 1 chrome version",
38+
"last 1 firefox version",
39+
"last 1 safari version"
40+
]
41+
},
42+
"devDependencies": {
43+
"@types/react": "^16.9.19",
44+
"@types/react-dom": "^16.9.5",
45+
"@types/react-redux": "^7.1.7",
46+
"@types/react-router": "^5.1.4",
47+
"@types/react-router-dom": "^5.1.3",
48+
"@types/redux-logger": "^3.0.7",
49+
"node-sass": "^4.13.1",
50+
"redux-devtools": "^3.5.0",
51+
"redux-devtools-extension": "^2.13.8",
52+
"redux-logger": "^3.0.6"
53+
}
54+
}

Diff for: public/favicon.ico

3.08 KB
Binary file not shown.

Diff for: public/index.html

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>

Diff for: public/logo192.png

5.22 KB
Loading

Diff for: public/logo512.png

9.44 KB
Loading

Diff for: public/manifest.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
},
10+
{
11+
"src": "logo192.png",
12+
"type": "image/png",
13+
"sizes": "192x192"
14+
},
15+
{
16+
"src": "logo512.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
],
21+
"start_url": ".",
22+
"display": "standalone",
23+
"theme_color": "#000000",
24+
"background_color": "#ffffff"
25+
}

Diff for: public/robots.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# https://www.robotstxt.org/robotstxt.html
2+
User-agent: *

Diff for: src/App.css

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.App {
2+
text-align: center;
3+
}
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+
}
38+
}

Diff for: src/App.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as React from "react";
2+
import Routes from "./components/common/routes";
3+
4+
const App: React.SFC<{}> = () => {
5+
return (
6+
<>
7+
<Routes />
8+
</>
9+
);
10+
};
11+
12+
export default App;

Diff for: src/components/common/layout/footer/footer.scss

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
footer{
2+
background-color: lightblue;
3+
4+
}

Diff for: src/components/common/layout/footer/index.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React, {FunctionComponent} from 'react';
2+
import './footer.scss';
3+
4+
interface IProps{
5+
}
6+
7+
const Footer: FunctionComponent<IProps> = props => {
8+
return (
9+
<footer>
10+
<p>THIS IS FOOTER</p>
11+
</footer>
12+
)
13+
}
14+
export default Footer;

Diff for: src/components/common/layout/header/header.scss

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
header{
2+
background-color: gray;
3+
display: flex;
4+
img{
5+
width:50px;
6+
height:50px;
7+
}
8+
9+
ul {
10+
li{
11+
display: flex;
12+
a{
13+
display: flex;
14+
}
15+
}
16+
}
17+
}

Diff for: src/components/common/layout/header/index.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, { FunctionComponent } from 'react';
2+
import Logo from '../../../../logo.svg';
3+
import {Link} from 'react-router-dom';
4+
import './header.scss';
5+
6+
7+
interface IProps {
8+
username: string
9+
}
10+
11+
const Header: FunctionComponent<IProps> = props => {
12+
return (
13+
<header>
14+
<img src={Logo} alt="logo" />
15+
<p>Hello, {props.username} </p>
16+
17+
<nav>
18+
<ul>
19+
<li><Link to="/">Home</Link></li>
20+
<li><Link to="/about">About</Link></li>
21+
</ul>
22+
</nav>
23+
</header>
24+
)
25+
}
26+
export default Header;

Diff for: src/components/common/routes/index.tsx

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import { Switch, Route, Redirect, withRouter } from 'react-router-dom';
3+
import Home from '../../../containers/home';
4+
import About from '../../../containers/about';
5+
import Header from '../layout/header';
6+
import Footer from '../layout/footer';
7+
8+
interface IProps {
9+
// readonly user: User
10+
}
11+
12+
interface IState {
13+
14+
}
15+
16+
class Routes extends React.Component<IProps,IState> {
17+
constructor(props: IProps){
18+
super(props);
19+
console.log('router starts')
20+
}
21+
22+
render() : JSX.Element | null {
23+
return (
24+
<main>
25+
<Header username="Emre"/>
26+
<section className="page-container">
27+
<Switch>
28+
<Route exact path="/" component={Home} />
29+
<Route path="/about/:userId" component={About} />
30+
</Switch>
31+
</section>
32+
<Footer />
33+
</main>
34+
)
35+
}
36+
}
37+
38+
export default Routes;

Diff for: src/containers/about/actionTypes.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { IPerson } from "./types";
2+
import { IPostListItem } from "../home/types";
3+
4+
// Create Action Constants
5+
export enum AboutActionTypes {
6+
GET_PERSON_DETAIL_DATA = "GET_PERSON_DETAIL_DATA",
7+
GET_POSTS_DATA_BY_USER_ID = "GET_POSTS_DATA_BY_USER_ID"
8+
}
9+
10+
// Interface for Get People Detail Action Type
11+
export interface IGetPeopleDetailAction {
12+
type: typeof AboutActionTypes.GET_PERSON_DETAIL_DATA,
13+
payload: IPerson
14+
}
15+
16+
// Interface for Get Posts of user Action Type
17+
export interface IGetPostsByUserIdAction {
18+
type: typeof AboutActionTypes.GET_POSTS_DATA_BY_USER_ID,
19+
payload: IPostListItem[]
20+
}
21+
22+
/*
23+
Combine the action types with a union (we assume there are more)
24+
example: export type HomeActions = IGetPeopleListAction | IGetOneAction ...
25+
*/
26+
export type AboutActions = IGetPeopleDetailAction | IGetPostsByUserIdAction

Diff for: src/containers/about/actions.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Import redux types
2+
import { ActionCreator, Dispatch } from 'redux';
3+
import { ThunkAction } from 'redux-thunk';
4+
import axios from 'axios';
5+
6+
// Import Home Typing
7+
import { IAboutState } from './reducer';
8+
import { AboutActionTypes, IGetPeopleDetailAction, IGetPostsByUserIdAction } from './actionTypes';
9+
10+
/* Get All Action
11+
<Promise<Return Type>, State Interface, Type of Param, Type of Action> */
12+
export const getAllPostsByUserId: ActionCreator<
13+
ThunkAction<Promise<any>, IAboutState, {}, IGetPostsByUserIdAction>
14+
> = (userId) => {
15+
return async (dispatch: Dispatch) => {
16+
axios.get('https://jsonplaceholder.typicode.com/posts?userId='+userId)
17+
.then((res)=>{
18+
console.log('res ', res)
19+
20+
if(res.status === 200 && res.data){
21+
dispatch({
22+
payload: res.data,
23+
type: AboutActionTypes.GET_POSTS_DATA_BY_USER_ID,
24+
});
25+
}
26+
})
27+
.catch((err)=>{
28+
console.log('getAllPostsByUserId err >> ' , err)
29+
})
30+
}};
31+
32+
export const getUserDetailData: ActionCreator<
33+
ThunkAction<Promise<any>, IAboutState, {}, IGetPeopleDetailAction>
34+
> = (userId) => {
35+
return async (dispatch: Dispatch) => {
36+
axios.get('https://jsonplaceholder.typicode.com/users/'+userId)
37+
.then((res)=>{
38+
console.log('res ', res)
39+
40+
if(res.status === 200 && res.data){
41+
dispatch({
42+
payload: res.data,
43+
type: AboutActionTypes.GET_PERSON_DETAIL_DATA,
44+
});
45+
}
46+
})
47+
.catch((err)=>{
48+
console.log('getUserDetailData err >> ' , err)
49+
})
50+
}};

0 commit comments

Comments
 (0)