Setup, react dev tools, logging
This commit is contained in:
parent
e6b1e331af
commit
84a0b22cd3
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@ -8,7 +8,7 @@
|
||||
"type": "chrome",
|
||||
"request": "launch",
|
||||
"name": "Launch Chrome",
|
||||
"url": "http://localhost:8080",
|
||||
"url": "http://127.0.0.1:3000",
|
||||
"webRoot": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
|
||||
8
chapter08/.directory
Normal file
8
chapter08/.directory
Normal file
@ -0,0 +1,8 @@
|
||||
[Dolphin]
|
||||
Timestamp=2018,7,29,12,22,28
|
||||
Version=4
|
||||
ViewMode=1
|
||||
VisibleRoles=Details_text,Details_size,Details_modificationtime,Details_destination,Details_permissions,Details_owner,Details_group,CustomizedDetails
|
||||
|
||||
[Settings]
|
||||
HiddenFilesShown=true
|
||||
8
chapter09/.directory
Normal file
8
chapter09/.directory
Normal file
@ -0,0 +1,8 @@
|
||||
[Dolphin]
|
||||
Timestamp=2018,7,29,12,22,28
|
||||
Version=4
|
||||
ViewMode=1
|
||||
VisibleRoles=Details_text,Details_size,Details_modificationtime,Details_destination,Details_permissions,Details_owner,Details_group,CustomizedDetails
|
||||
|
||||
[Settings]
|
||||
HiddenFilesShown=true
|
||||
25
chapter09/.eslintrc
Normal file
25
chapter09/.eslintrc
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"parser": "babel-eslint",
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2017,
|
||||
"sourceType": "module"
|
||||
},
|
||||
"env": {
|
||||
"node": true,
|
||||
"browser": true,
|
||||
"es6": true,
|
||||
"jest": true
|
||||
},
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:flowtype/recommended",
|
||||
"plugin:react/recommended"
|
||||
],
|
||||
"plugins": ["babel", "flowtype", "react"],
|
||||
"rules": {
|
||||
"no-console": "off",
|
||||
"no-var": "error",
|
||||
"prefer-const": "error",
|
||||
"flowtype/no-types-missing-file-annotation": 0
|
||||
}
|
||||
}
|
||||
17
chapter09/.flowconfig
Normal file
17
chapter09/.flowconfig
Normal file
@ -0,0 +1,17 @@
|
||||
[ignore]
|
||||
.*/node_modules/.*
|
||||
|
||||
[include]
|
||||
|
||||
[libs]
|
||||
|
||||
[lints]
|
||||
all=warn
|
||||
untyped-type-import=off
|
||||
unsafe-getters-setters=off
|
||||
|
||||
[options]
|
||||
include_warnings=true
|
||||
module.file_ext=.scss
|
||||
|
||||
[strict]
|
||||
21
chapter09/.gitignore
vendored
Normal file
21
chapter09/.gitignore
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
# See https://help.github.com/ignore-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
5
chapter09/.prettierrc
Normal file
5
chapter09/.prettierrc
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"tabWidth": 4,
|
||||
"printWidth": 75
|
||||
}
|
||||
|
||||
2444
chapter09/README.md
Normal file
2444
chapter09/README.md
Normal file
File diff suppressed because it is too large
Load Diff
12
chapter09/config-overrides.js
Normal file
12
chapter09/config-overrides.js
Normal file
@ -0,0 +1,12 @@
|
||||
const rewireEslint = require("react-app-rewire-eslint");
|
||||
|
||||
function overrideEslintOptions(options) {
|
||||
// do stuff with the eslint options...
|
||||
return options;
|
||||
}
|
||||
|
||||
/* global module */
|
||||
module.exports = function override(config, env) {
|
||||
config = rewireEslint(config, env, overrideEslintOptions);
|
||||
return config;
|
||||
};
|
||||
15117
chapter09/package-lock.json
generated
Normal file
15117
chapter09/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
44
chapter09/package.json
Normal file
44
chapter09/package.json
Normal file
@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "chapter09",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"babel-core": "^6.26.3",
|
||||
"babel-eslint": "^8.2.5",
|
||||
"babel-runtime": "^6.26.0",
|
||||
"eslint": "^5.0.1",
|
||||
"eslint-config-recommended": "^3.0.0",
|
||||
"eslint-plugin-babel": "^5.1.0",
|
||||
"eslint-plugin-flowtype": "^2.49.3",
|
||||
"eslint-plugin-react": "^7.10.0",
|
||||
"flow": "^0.2.3",
|
||||
"flow-bin": "^0.75.0",
|
||||
"flow-coverage-report": "^0.5.0",
|
||||
"flow-typed": "^2.4.0",
|
||||
"react-app-rewire-eslint": "^0.2.3",
|
||||
"react-app-rewired": "^1.5.2",
|
||||
"react-devtools": "^3.2.3",
|
||||
"react-scripts": "1.1.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.18.0",
|
||||
"connected-react-router": "^4.3.0",
|
||||
"react": "^16.4.1",
|
||||
"react-dom": "^16.4.1",
|
||||
"react-loadable": "^5.4.0",
|
||||
"react-redux": "^5.0.7",
|
||||
"react-router-dom": "^4.3.1",
|
||||
"redux": "^4.0.0",
|
||||
"redux-logger": "^3.0.6",
|
||||
"redux-thunk": "^2.3.0"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-app-rewired start",
|
||||
"build": "react-app-rewired build",
|
||||
"test": "react-app-rewired test --env=jsdom",
|
||||
"eject": "react-app-rewired eject",
|
||||
"flow": "flow",
|
||||
"addTypes": "flow-typed install",
|
||||
"devtools": "react-devtools"
|
||||
}
|
||||
}
|
||||
BIN
chapter09/public/favicon.ico
Normal file
BIN
chapter09/public/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.8 KiB |
44
chapter09/public/index.html
Normal file
44
chapter09/public/index.html
Normal file
@ -0,0 +1,44 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<!-- <script src="http://192.168.1.200:8097"></script> -->
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<meta name="theme-color" content="#000000">
|
||||
<!--
|
||||
manifest.json provides metadata used when your web app is added to the
|
||||
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
|
||||
-->
|
||||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
|
||||
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
|
||||
<!--
|
||||
Notice the use of %PUBLIC_URL% in the tags above.
|
||||
It will be replaced with the URL of the `public` folder during the build.
|
||||
Only files inside the `public` folder can be referenced from the HTML.
|
||||
|
||||
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
|
||||
work correctly both with client-side routing and a non-root public URL.
|
||||
Learn how to configure a non-root public URL by running `npm run build`.
|
||||
-->
|
||||
<title>React App</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<noscript>
|
||||
You need to enable JavaScript to run this app.
|
||||
</noscript>
|
||||
<div id="root"></div>
|
||||
<!--
|
||||
This HTML file is a template.
|
||||
If you open it directly in the browser, you will see an empty page.
|
||||
|
||||
You can add webfonts, meta tags, or analytics to this file.
|
||||
The build step will place the bundled scripts into the <body> tag.
|
||||
|
||||
To begin the development, run `npm start` or `yarn start`.
|
||||
To create a production bundle, use `npm run build` or `yarn build`.
|
||||
-->
|
||||
</body>
|
||||
|
||||
</html>
|
||||
15
chapter09/public/manifest.json
Normal file
15
chapter09/public/manifest.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"short_name": "React App",
|
||||
"name": "Create React App Sample",
|
||||
"icons": [
|
||||
{
|
||||
"src": "favicon.ico",
|
||||
"sizes": "64x64 32x32 24x24 16x16",
|
||||
"type": "image/x-icon"
|
||||
}
|
||||
],
|
||||
"start_url": "./index.html",
|
||||
"display": "standalone",
|
||||
"theme_color": "#000000",
|
||||
"background_color": "#ffffff"
|
||||
}
|
||||
23
chapter09/src/App.counter.js
Normal file
23
chapter09/src/App.counter.js
Normal file
@ -0,0 +1,23 @@
|
||||
/* @flow */
|
||||
|
||||
import React, { Component, Fragment } from "react";
|
||||
import { Provider } from "react-redux";
|
||||
|
||||
import { store } from "./counterApp/store";
|
||||
import { ConnectedCounter, ConnectedClicksDisplay } from "./counterApp";
|
||||
|
||||
class App extends Component<{}> {
|
||||
render() {
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<Fragment>
|
||||
<ConnectedCounter />
|
||||
<hr />
|
||||
<ConnectedClicksDisplay />
|
||||
</Fragment>
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default App;
|
||||
26
chapter09/src/App.regions.js
Normal file
26
chapter09/src/App.regions.js
Normal file
@ -0,0 +1,26 @@
|
||||
/* @flow */
|
||||
|
||||
import React, { Component, Fragment } from "react";
|
||||
import { Provider } from "react-redux";
|
||||
|
||||
import {
|
||||
ConnectedCountrySelect,
|
||||
ConnectedRegionsTable
|
||||
} from "./regionsApp";
|
||||
|
||||
import { store } from "./regionsApp/store";
|
||||
|
||||
class App extends Component<{}> {
|
||||
render() {
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<Fragment>
|
||||
<ConnectedCountrySelect />
|
||||
<ConnectedRegionsTable />
|
||||
</Fragment>
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default App;
|
||||
16
chapter09/src/counterApp/clicksDisplay.component.js
Normal file
16
chapter09/src/counterApp/clicksDisplay.component.js
Normal file
@ -0,0 +1,16 @@
|
||||
/* @flow */
|
||||
|
||||
import React from "react";
|
||||
import { PropTypes } from "prop-types";
|
||||
|
||||
export class ClicksDisplay extends React.PureComponent<{
|
||||
clicks: number
|
||||
}> {
|
||||
static propTypes = {
|
||||
clicks: PropTypes.number.isRequired
|
||||
};
|
||||
|
||||
render() {
|
||||
return <div>Clicks so far: {this.props.clicks}</div>;
|
||||
}
|
||||
}
|
||||
11
chapter09/src/counterApp/clicksDisplay.connected.js
Normal file
11
chapter09/src/counterApp/clicksDisplay.connected.js
Normal file
@ -0,0 +1,11 @@
|
||||
/* @flow */
|
||||
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { ClicksDisplay } from "./clicksDisplay.component";
|
||||
|
||||
const getProps = state => ({
|
||||
clicks: state.clicks
|
||||
});
|
||||
|
||||
export const ConnectedClicksDisplay = connect(getProps)(ClicksDisplay);
|
||||
29
chapter09/src/counterApp/counter.actions.js
Normal file
29
chapter09/src/counterApp/counter.actions.js
Normal file
@ -0,0 +1,29 @@
|
||||
/* @flow */
|
||||
|
||||
export const COUNTER_INCREMENT = "counter:increment";
|
||||
|
||||
export const COUNTER_RESET = "counter:reset";
|
||||
|
||||
export type CounterAction = {
|
||||
type: string,
|
||||
value?: number
|
||||
};
|
||||
|
||||
export const reset = () =>
|
||||
({
|
||||
type: COUNTER_RESET
|
||||
}: CounterAction);
|
||||
|
||||
export const increment = (inc: number) =>
|
||||
({
|
||||
type: COUNTER_INCREMENT,
|
||||
value: inc
|
||||
}: CounterAction);
|
||||
|
||||
export const decrement = (dec: number) =>
|
||||
({
|
||||
type: COUNTER_INCREMENT,
|
||||
value: -dec
|
||||
}: CounterAction);
|
||||
|
||||
// returning increment(-dec) would have worked as well
|
||||
37
chapter09/src/counterApp/counter.component.js
Normal file
37
chapter09/src/counterApp/counter.component.js
Normal file
@ -0,0 +1,37 @@
|
||||
/* @flow */
|
||||
|
||||
import React from "react";
|
||||
import { PropTypes } from "prop-types";
|
||||
|
||||
import {
|
||||
increment,
|
||||
decrement,
|
||||
reset,
|
||||
CounterAction
|
||||
} from "./counter.actions";
|
||||
|
||||
export class Counter extends React.PureComponent<{
|
||||
count: number,
|
||||
dispatch: CounterAction => any
|
||||
}> {
|
||||
static propTypes = {
|
||||
count: PropTypes.number.isRequired,
|
||||
dispatch: PropTypes.func.isRequired
|
||||
};
|
||||
|
||||
onAdd1 = () => this.props.dispatch(increment(1));
|
||||
onSub2 = () => this.props.dispatch(decrement(2));
|
||||
onReset = () => this.props.dispatch(reset());
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
Value: {this.props.count}
|
||||
<br />
|
||||
<button onClick={this.onAdd1}>Add 1</button>
|
||||
<button onClick={this.onSub2}>Subtract 2</button>
|
||||
<button onClick={this.onReset}>Reset</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
9
chapter09/src/counterApp/counter.connected.js
Normal file
9
chapter09/src/counterApp/counter.connected.js
Normal file
@ -0,0 +1,9 @@
|
||||
/* @flow */
|
||||
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { Counter } from "./counter.component";
|
||||
|
||||
const getProps = state => ({ count: state.count });
|
||||
|
||||
export const ConnectedCounter = connect(getProps)(Counter);
|
||||
28
chapter09/src/counterApp/counter.reducer.js
Normal file
28
chapter09/src/counterApp/counter.reducer.js
Normal file
@ -0,0 +1,28 @@
|
||||
/* @flow */
|
||||
|
||||
import { COUNTER_INCREMENT, COUNTER_RESET } from "./counter.actions";
|
||||
|
||||
import type { CounterAction } from "./counter.actions.js";
|
||||
|
||||
export const reducer = (
|
||||
state = {
|
||||
// initial state
|
||||
count: 0,
|
||||
clicks: 0
|
||||
},
|
||||
action: CounterAction
|
||||
) => {
|
||||
switch (action.type) {
|
||||
case COUNTER_INCREMENT:
|
||||
return {
|
||||
count: state.count + action.value,
|
||||
clicks: state.clicks + 1
|
||||
};
|
||||
|
||||
case COUNTER_RESET:
|
||||
return { count: 0, clicks: state.clicks + 1 };
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
14
chapter09/src/counterApp/index.js
Normal file
14
chapter09/src/counterApp/index.js
Normal file
@ -0,0 +1,14 @@
|
||||
/* @flow */
|
||||
|
||||
import { Counter } from "./counter.component.js";
|
||||
import { ConnectedCounter } from "./counter.connected.js";
|
||||
|
||||
import { ClicksDisplay } from "./clicksDisplay.component.js";
|
||||
import { ConnectedClicksDisplay } from "./clicksDisplay.connected.js";
|
||||
|
||||
export {
|
||||
Counter,
|
||||
ConnectedCounter,
|
||||
ClicksDisplay,
|
||||
ConnectedClicksDisplay
|
||||
};
|
||||
10
chapter09/src/counterApp/store.js
Normal file
10
chapter09/src/counterApp/store.js
Normal file
@ -0,0 +1,10 @@
|
||||
/* @flow */
|
||||
|
||||
import { createStore, applyMiddleware } from "redux";
|
||||
import { createLogger } from "redux-logger";
|
||||
|
||||
import { reducer } from "./counter.reducer.js";
|
||||
|
||||
const logger = createLogger({ diff: true, duration: true });
|
||||
|
||||
export const store = createStore(reducer, applyMiddleware(logger));
|
||||
5
chapter09/src/general.css
Normal file
5
chapter09/src/general.css
Normal file
@ -0,0 +1,5 @@
|
||||
.bordered {
|
||||
border: 1px solid blue;
|
||||
padding: 2px;
|
||||
margin: 4px;
|
||||
}
|
||||
8
chapter09/src/index.js
Normal file
8
chapter09/src/index.js
Normal file
@ -0,0 +1,8 @@
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom";
|
||||
|
||||
import App from "./App.regions";
|
||||
import registerServiceWorker from "./registerServiceWorker";
|
||||
|
||||
ReactDOM.render(<App />, document.getElementById("root"));
|
||||
registerServiceWorker();
|
||||
53
chapter09/src/regionsApp/countrySelect.component.js
Normal file
53
chapter09/src/regionsApp/countrySelect.component.js
Normal file
@ -0,0 +1,53 @@
|
||||
/* @flow */
|
||||
|
||||
import React from "react";
|
||||
import PropTypes from "prop-types";
|
||||
|
||||
import "../general.css";
|
||||
|
||||
export class CountrySelect extends React.PureComponent<{
|
||||
dispatch: ({}) => any
|
||||
}> {
|
||||
static propTypes = {
|
||||
loading: PropTypes.bool.isRequired,
|
||||
list: PropTypes.arrayOf(PropTypes.object).isRequired,
|
||||
onSelect: PropTypes.func.isRequired,
|
||||
getCountries: PropTypes.func.isRequired
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
if (this.props.list.length === 0) {
|
||||
this.props.getCountries();
|
||||
}
|
||||
}
|
||||
|
||||
onSelect = (e: { target: HTMLOptionElement }) =>
|
||||
this.props.onSelect(e.target.value);
|
||||
|
||||
render() {
|
||||
if (this.props.loading) {
|
||||
return <div className="bordered">Loading countries...</div>;
|
||||
} else {
|
||||
const sortedCountries = [...this.props.list].sort(
|
||||
(a, b) => (a.countryName < b.countryName ? -1 : 1)
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="bordered">
|
||||
Country:
|
||||
<select onChange={this.onSelect}>
|
||||
<option value="">Select a country:</option>
|
||||
{sortedCountries.map(x => (
|
||||
<option
|
||||
key={x.countryCode}
|
||||
value={x.countryCode}
|
||||
>
|
||||
{x.countryName}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
chapter09/src/regionsApp/countrySelect.connected.js
Normal file
21
chapter09/src/regionsApp/countrySelect.connected.js
Normal file
@ -0,0 +1,21 @@
|
||||
/* @flow */
|
||||
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { CountrySelect } from "./countrySelect.component";
|
||||
import { getCountries, getRegions } from "./world.actions";
|
||||
|
||||
const getProps = state => ({
|
||||
list: state.countries,
|
||||
loading: state.loadingCountries
|
||||
});
|
||||
|
||||
const getDispatch = dispatch => ({
|
||||
getCountries: () => dispatch(getCountries()),
|
||||
onSelect: c => dispatch(getRegions(c))
|
||||
});
|
||||
|
||||
export const ConnectedCountrySelect = connect(
|
||||
getProps,
|
||||
getDispatch
|
||||
)(CountrySelect);
|
||||
6
chapter09/src/regionsApp/index.js
Normal file
6
chapter09/src/regionsApp/index.js
Normal file
@ -0,0 +1,6 @@
|
||||
/* @flow */
|
||||
|
||||
import { ConnectedCountrySelect } from "./countrySelect.connected.js";
|
||||
import { ConnectedRegionsTable } from "./regionsTable.connected.js";
|
||||
|
||||
export { ConnectedCountrySelect, ConnectedRegionsTable };
|
||||
41
chapter09/src/regionsApp/regionsTable.component.js
Normal file
41
chapter09/src/regionsApp/regionsTable.component.js
Normal file
@ -0,0 +1,41 @@
|
||||
/* @flow */
|
||||
|
||||
import React from "react";
|
||||
import PropTypes from "prop-types";
|
||||
|
||||
import "../general.css";
|
||||
|
||||
export class RegionsTable extends React.PureComponent<{
|
||||
list: Array<{
|
||||
regionCode: string,
|
||||
regionName: string
|
||||
}>
|
||||
}> {
|
||||
static propTypes = {
|
||||
list: PropTypes.arrayOf(PropTypes.object).isRequired
|
||||
};
|
||||
|
||||
static defaultProps = {
|
||||
list: []
|
||||
};
|
||||
|
||||
render() {
|
||||
if (this.props.list.length === 0) {
|
||||
return <div className="bordered">No regions.</div>;
|
||||
} else {
|
||||
const ordered = [...this.props.list].sort(
|
||||
(a, b) => (a.regionName < b.regionName ? -1 : 1)
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="bordered">
|
||||
{ordered.map(x => (
|
||||
<div key={x.countryCode + "-" + x.regionCode}>
|
||||
{x.regionName}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
12
chapter09/src/regionsApp/regionsTable.connected.js
Normal file
12
chapter09/src/regionsApp/regionsTable.connected.js
Normal file
@ -0,0 +1,12 @@
|
||||
/* @flow */
|
||||
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { RegionsTable } from "./regionsTable.component";
|
||||
|
||||
const getProps = state => ({
|
||||
list: state.regions,
|
||||
loading: state.loadingRegions
|
||||
});
|
||||
|
||||
export const ConnectedRegionsTable = connect(getProps)(RegionsTable);
|
||||
9
chapter09/src/regionsApp/serviceApi.js
Normal file
9
chapter09/src/regionsApp/serviceApi.js
Normal file
@ -0,0 +1,9 @@
|
||||
/* @flow */
|
||||
|
||||
import axios from "axios";
|
||||
|
||||
export const getCountriesAPI = () =>
|
||||
axios.get(`http://fk-server:8080/countries`);
|
||||
|
||||
export const getRegionsAPI = country =>
|
||||
axios.get(`http://fk-server:8080/regions/${country}`);
|
||||
11
chapter09/src/regionsApp/store.js
Normal file
11
chapter09/src/regionsApp/store.js
Normal file
@ -0,0 +1,11 @@
|
||||
/* @flow */
|
||||
|
||||
import { createStore, applyMiddleware } from "redux";
|
||||
import thunk from "redux-thunk";
|
||||
import { createLogger } from "redux-logger";
|
||||
|
||||
import { reducer } from "./worlds.reducer.js";
|
||||
|
||||
const logger = createLogger({ duration: true });
|
||||
|
||||
export const store = createStore(reducer, applyMiddleware(thunk, logger));
|
||||
108
chapter09/src/regionsApp/world.actions.js
Normal file
108
chapter09/src/regionsApp/world.actions.js
Normal file
@ -0,0 +1,108 @@
|
||||
/* @flow */
|
||||
|
||||
import { getCountriesAPI, getRegionsAPI } from "./serviceApi";
|
||||
|
||||
// Countries actions
|
||||
|
||||
export const COUNTRIES_REQUEST = "countries:request";
|
||||
export const COUNTRIES_SUCCESS = "countries:success";
|
||||
export const COUNTRIES_FAILURE = "countries:failure";
|
||||
|
||||
export type CountriesAction = {
|
||||
type: string,
|
||||
country?: string,
|
||||
listOfCountries?: [object]
|
||||
};
|
||||
|
||||
export const countriesRequest = () =>
|
||||
({
|
||||
type: COUNTRIES_REQUEST
|
||||
}: CountriesActions);
|
||||
|
||||
export const countriesSuccess = (listOfCountries: []) =>
|
||||
({
|
||||
type: COUNTRIES_SUCCESS,
|
||||
listOfCountries
|
||||
}: CountriesActions);
|
||||
|
||||
export const countriesFailure = () =>
|
||||
({
|
||||
type: COUNTRIES_FAILURE
|
||||
}: CountriesActions);
|
||||
|
||||
// Regions actions
|
||||
|
||||
export const REGIONS_REQUEST = "regions:request";
|
||||
export const REGIONS_SUCCESS = "regions:success";
|
||||
export const REGIONS_FAILURE = "regions:failure";
|
||||
|
||||
export type RegionsAction = {
|
||||
type: string,
|
||||
listOfRegions?: [object]
|
||||
};
|
||||
|
||||
export const regionsRequest = (country: string) =>
|
||||
({
|
||||
type: REGIONS_REQUEST,
|
||||
country
|
||||
}: RegionsActions);
|
||||
|
||||
export const regionsSuccess = (listOfRegions: [{}]) =>
|
||||
({
|
||||
type: REGIONS_SUCCESS,
|
||||
listOfRegions
|
||||
}: RegionsActions);
|
||||
|
||||
export const regionsFailure = () =>
|
||||
({
|
||||
type: REGIONS_FAILURE
|
||||
}: RegionsActions);
|
||||
|
||||
// Complex Actions:
|
||||
|
||||
export const getCountries = () => async dispatch => {
|
||||
try {
|
||||
dispatch(countriesRequest());
|
||||
// the next line delays execution for 5 seconds:
|
||||
// await new Promise(resolve => setTimeout(resolve, 5000));
|
||||
const result = await getCountriesAPI();
|
||||
dispatch(countriesSuccess(result.data));
|
||||
} catch (e) {
|
||||
dispatch(countriesFailure());
|
||||
}
|
||||
};
|
||||
|
||||
export const getRegions = (country: string) => async dispatch => {
|
||||
if (country) {
|
||||
try {
|
||||
dispatch(regionsRequest(country));
|
||||
const result = await getRegionsAPI(country);
|
||||
dispatch(regionsSuccess(result.data));
|
||||
} catch (e) {
|
||||
dispatch(regionsFailure());
|
||||
}
|
||||
} else {
|
||||
dispatch(regionsFailure());
|
||||
}
|
||||
};
|
||||
|
||||
export const getRegions2 = (country: string) => async (
|
||||
dispatch,
|
||||
getState
|
||||
) => {
|
||||
if (country === getState().currentCountry) {
|
||||
console.log("Hey! You are getting the same country as before!");
|
||||
}
|
||||
|
||||
if (country) {
|
||||
try {
|
||||
dispatch(regionsRequest(country));
|
||||
const result = await getRegionsAPI(country);
|
||||
dispatch(regionsSuccess(result.data));
|
||||
} catch (e) {
|
||||
dispatch(regionsFailure());
|
||||
}
|
||||
} else {
|
||||
dispatch(regionsFailure());
|
||||
}
|
||||
};
|
||||
74
chapter09/src/regionsApp/worlds.reducer.js
Normal file
74
chapter09/src/regionsApp/worlds.reducer.js
Normal file
@ -0,0 +1,74 @@
|
||||
/* @flow */
|
||||
|
||||
import {
|
||||
COUNTRIES_REQUEST,
|
||||
COUNTRIES_SUCCESS,
|
||||
COUNTRIES_FAILURE,
|
||||
REGIONS_REQUEST,
|
||||
REGIONS_SUCCESS,
|
||||
REGIONS_FAILURE
|
||||
} from "./world.actions";
|
||||
|
||||
import type { CountriesAction, RegionsAction } from "./world.actions";
|
||||
|
||||
// import type { CounterAction } from "./world.actions.js";
|
||||
|
||||
export const reducer = (
|
||||
state: object = {
|
||||
// initial state
|
||||
loadingCountries: false,
|
||||
currentCountry: "",
|
||||
countries: [],
|
||||
loadingRegions: false,
|
||||
regions: []
|
||||
},
|
||||
action: CountriesAction | RegionsAction
|
||||
) => {
|
||||
switch (action.type) {
|
||||
case COUNTRIES_REQUEST:
|
||||
return {
|
||||
...state,
|
||||
loadingCountries: true,
|
||||
countries: []
|
||||
};
|
||||
|
||||
case COUNTRIES_SUCCESS:
|
||||
return {
|
||||
...state,
|
||||
loadingCountries: false,
|
||||
countries: action.listOfCountries
|
||||
};
|
||||
|
||||
case COUNTRIES_FAILURE:
|
||||
return {
|
||||
...state,
|
||||
loadingCountries: false,
|
||||
countries: []
|
||||
};
|
||||
|
||||
case REGIONS_REQUEST:
|
||||
return {
|
||||
...state,
|
||||
loadingRegions: true,
|
||||
currentCountry: action.country,
|
||||
regions: []
|
||||
};
|
||||
|
||||
case REGIONS_SUCCESS:
|
||||
return {
|
||||
...state,
|
||||
loadingRegions: false,
|
||||
regions: action.listOfRegions
|
||||
};
|
||||
|
||||
case REGIONS_FAILURE:
|
||||
return {
|
||||
...state,
|
||||
loadingRegions: false,
|
||||
regions: []
|
||||
};
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
117
chapter09/src/registerServiceWorker.js
Normal file
117
chapter09/src/registerServiceWorker.js
Normal file
@ -0,0 +1,117 @@
|
||||
// In production, we register a service worker to serve assets from local cache.
|
||||
|
||||
// This lets the app load faster on subsequent visits in production, and gives
|
||||
// it offline capabilities. However, it also means that developers (and users)
|
||||
// will only see deployed updates on the "N+1" visit to a page, since previously
|
||||
// cached resources are updated in the background.
|
||||
|
||||
// To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
|
||||
// This link also includes instructions on opting out of this behavior.
|
||||
|
||||
const isLocalhost = Boolean(
|
||||
window.location.hostname === 'localhost' ||
|
||||
// [::1] is the IPv6 localhost address.
|
||||
window.location.hostname === '[::1]' ||
|
||||
// 127.0.0.1/8 is considered localhost for IPv4.
|
||||
window.location.hostname.match(
|
||||
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
|
||||
)
|
||||
);
|
||||
|
||||
export default function register() {
|
||||
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
|
||||
// The URL constructor is available in all browsers that support SW.
|
||||
const publicUrl = new URL(process.env.PUBLIC_URL, window.location);
|
||||
if (publicUrl.origin !== window.location.origin) {
|
||||
// Our service worker won't work if PUBLIC_URL is on a different origin
|
||||
// from what our page is served on. This might happen if a CDN is used to
|
||||
// serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374
|
||||
return;
|
||||
}
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
|
||||
|
||||
if (isLocalhost) {
|
||||
// This is running on localhost. Lets check if a service worker still exists or not.
|
||||
checkValidServiceWorker(swUrl);
|
||||
|
||||
// Add some additional logging to localhost, pointing developers to the
|
||||
// service worker/PWA documentation.
|
||||
navigator.serviceWorker.ready.then(() => {
|
||||
console.log(
|
||||
'This web app is being served cache-first by a service ' +
|
||||
'worker. To learn more, visit https://goo.gl/SC7cgQ'
|
||||
);
|
||||
});
|
||||
} else {
|
||||
// Is not local host. Just register service worker
|
||||
registerValidSW(swUrl);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function registerValidSW(swUrl) {
|
||||
navigator.serviceWorker
|
||||
.register(swUrl)
|
||||
.then(registration => {
|
||||
registration.onupdatefound = () => {
|
||||
const installingWorker = registration.installing;
|
||||
installingWorker.onstatechange = () => {
|
||||
if (installingWorker.state === 'installed') {
|
||||
if (navigator.serviceWorker.controller) {
|
||||
// At this point, the old content will have been purged and
|
||||
// the fresh content will have been added to the cache.
|
||||
// It's the perfect time to display a "New content is
|
||||
// available; please refresh." message in your web app.
|
||||
console.log('New content is available; please refresh.');
|
||||
} else {
|
||||
// At this point, everything has been precached.
|
||||
// It's the perfect time to display a
|
||||
// "Content is cached for offline use." message.
|
||||
console.log('Content is cached for offline use.');
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error during service worker registration:', error);
|
||||
});
|
||||
}
|
||||
|
||||
function checkValidServiceWorker(swUrl) {
|
||||
// Check if the service worker can be found. If it can't reload the page.
|
||||
fetch(swUrl)
|
||||
.then(response => {
|
||||
// Ensure service worker exists, and that we really are getting a JS file.
|
||||
if (
|
||||
response.status === 404 ||
|
||||
response.headers.get('content-type').indexOf('javascript') === -1
|
||||
) {
|
||||
// No service worker found. Probably a different app. Reload the page.
|
||||
navigator.serviceWorker.ready.then(registration => {
|
||||
registration.unregister().then(() => {
|
||||
window.location.reload();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// Service worker found. Proceed as normal.
|
||||
registerValidSW(swUrl);
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
console.log(
|
||||
'No internet connection found. App is running in offline mode.'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export function unregister() {
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.ready.then(registration => {
|
||||
registration.unregister();
|
||||
});
|
||||
}
|
||||
}
|
||||
3
fixwatcherlimit
Normal file
3
fixwatcherlimit
Normal file
@ -0,0 +1,3 @@
|
||||
> sudo sysctl fs.inotify.max_user_watches=200000
|
||||
[sudo] password for root:
|
||||
fs.inotify.max_user_watches = 200000
|
||||
Loading…
x
Reference in New Issue
Block a user