Skip to content

Commit 0f9bfbb

Browse files
committed
add prettier
1 parent c9c2b82 commit 0f9bfbb

File tree

9 files changed

+174
-173
lines changed

9 files changed

+174
-173
lines changed

.prettierrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"printWidth": 120,
3+
"tabWidth": 4,
4+
"parser": "babel",
5+
"semi": true,
6+
"singleQuote": true,
7+
"bracketSpacing": false
8+
}

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"start": "react-scripts start",
1414
"build": "react-scripts build",
1515
"test": "react-scripts test",
16-
"eject": "react-scripts eject"
16+
"eject": "react-scripts eject",
17+
"prettier": "prettier --write 'src/**/*.js'"
1718
},
1819
"eslintConfig": {
1920
"extends": "react-app"
@@ -29,5 +30,8 @@
2930
"last 1 firefox version",
3031
"last 1 safari version"
3132
]
33+
},
34+
"devDependencies": {
35+
"prettier": "^1.18.2"
3236
}
3337
}

src/App.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,52 @@
11
import React from 'react';
22
import './App.css';
3-
import {Input} from "./input/Input";
4-
import {Json} from "./tree/Json";
3+
import {Input} from './input/Input';
4+
import {Json} from './tree/Json';
55

66
export class App extends React.Component {
77
constructor() {
88
super();
9-
this.state = {json: {}}
9+
this.state = {json: {}};
1010
}
1111

12-
dataLoaded = (t) => {
12+
dataLoaded = t => {
1313
this.setState(() => ({json: t, error: false, loading: false}));
14-
}
14+
};
1515

1616
onError = () => {
1717
this.setState(() => ({json: null, error: true, loading: false}));
18-
}
18+
};
1919

2020
onLoading = () => {
2121
this.setState(() => ({loading: true}));
22-
}
23-
22+
};
2423

2524
renderData() {
2625
if (this.state.loading) {
27-
return null
26+
return null;
2827
}
29-
return this.state.error ? <div className="error">Error loading</div> : <Json className="json-container" json={this.state.json}/>
30-
28+
return this.state.error ? (
29+
<div className="error">Error loading</div>
30+
) : (
31+
<Json className="json-container" json={this.state.json} />
32+
);
3133
}
3234

3335
renderLoader() {
3436
if (!this.state.loading) {
35-
return null
37+
return null;
3638
}
37-
return <span>Loading....</span>
38-
39+
return <span>Loading....</span>;
3940
}
4041

4142
render() {
4243
return (
4344
<div className="app">
44-
<Input onDataLoaded={this.dataLoaded} onError={this.onError} onLoading={this.onLoading}/>
45+
<Input onDataLoaded={this.dataLoaded} onError={this.onError} onLoading={this.onLoading} />
4546
{this.renderLoader()}
4647
{this.renderData()}
4748
</div>
48-
)
49+
);
4950
}
5051
}
5152

src/App.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ReactDOM from 'react-dom';
33
import App from './App';
44

55
it('renders without crashing', () => {
6-
const div = document.createElement('div');
7-
ReactDOM.render(<App />, div);
8-
ReactDOM.unmountComponentAtNode(div);
6+
const div = document.createElement('div');
7+
ReactDOM.render(<App />, div);
8+
ReactDOM.unmountComponentAtNode(div);
99
});

src/input/Input.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
2-
import {JSON, request} from "./api";
3-
import styles from './Input.module.css'
2+
import {JSON, request} from './api';
3+
import styles from './Input.module.css';
44

55
export class Input extends React.Component {
66
constructor(props) {
@@ -12,7 +12,6 @@ export class Input extends React.Component {
1212
this.onDataLoaded = this.props.onDataLoaded;
1313
this.onError = this.props.onError;
1414
this.onLoading = this.props.onLoading;
15-
1615
}
1716

1817
componentDidMount() {
@@ -37,9 +36,11 @@ export class Input extends React.Component {
3736
console.log(styles);
3837
return (
3938
<form className={styles.form} onSubmit={this.handleSubmit}>
40-
<input className={styles.input} type="text" value={this.state.value} onChange={this.handleChange}/>
41-
<button className={styles.btn} onClick={this.handleSubmit}>Go!</button>
39+
<input className={styles.input} type="text" value={this.state.value} onChange={this.handleChange} />
40+
<button className={styles.btn} onClick={this.handleSubmit}>
41+
Go!
42+
</button>
4243
</form>
43-
)
44+
);
4445
}
45-
}
46+
}

src/input/api.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ export const JSON = {
66
arr: [1, 2, 3],
77
arrObj: [{c: 2, d: 3, e: {f: 5}}, {m: 4}]
88
}
9-
}
9+
};
1010

1111
export function request(url) {
12-
return axios.get(url)
13-
.then(function (response) {
14-
return response.data;
15-
});
16-
12+
return axios.get(url).then(function(response) {
13+
return response.data;
14+
});
1715
}
18-

src/serviceWorker.js

+95-102
Original file line numberDiff line numberDiff line change
@@ -11,125 +11,118 @@
1111
// opt-in, read https://bit.ly/CRA-PWA
1212

1313
const isLocalhost = Boolean(
14-
window.location.hostname === 'localhost' ||
15-
// [::1] is the IPv6 localhost address.
16-
window.location.hostname === '[::1]' ||
17-
// 127.0.0.1/8 is considered localhost for IPv4.
18-
window.location.hostname.match(
19-
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
20-
)
14+
window.location.hostname === 'localhost' ||
15+
// [::1] is the IPv6 localhost address.
16+
window.location.hostname === '[::1]' ||
17+
// 127.0.0.1/8 is considered localhost for IPv4.
18+
window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/)
2119
);
2220

2321
export function register(config) {
24-
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
25-
// The URL constructor is available in all browsers that support SW.
26-
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
27-
if (publicUrl.origin !== window.location.origin) {
28-
// Our service worker won't work if PUBLIC_URL is on a different origin
29-
// from what our page is served on. This might happen if a CDN is used to
30-
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
31-
return;
32-
}
22+
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
23+
// The URL constructor is available in all browsers that support SW.
24+
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
25+
if (publicUrl.origin !== window.location.origin) {
26+
// Our service worker won't work if PUBLIC_URL is on a different origin
27+
// from what our page is served on. This might happen if a CDN is used to
28+
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
29+
return;
30+
}
3331

34-
window.addEventListener('load', () => {
35-
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
32+
window.addEventListener('load', () => {
33+
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
3634

37-
if (isLocalhost) {
38-
// This is running on localhost. Let's check if a service worker still exists or not.
39-
checkValidServiceWorker(swUrl, config);
35+
if (isLocalhost) {
36+
// This is running on localhost. Let's check if a service worker still exists or not.
37+
checkValidServiceWorker(swUrl, config);
4038

41-
// Add some additional logging to localhost, pointing developers to the
42-
// service worker/PWA documentation.
43-
navigator.serviceWorker.ready.then(() => {
44-
console.log(
45-
'This web app is being served cache-first by a service ' +
46-
'worker. To learn more, visit https://bit.ly/CRA-PWA'
47-
);
39+
// Add some additional logging to localhost, pointing developers to the
40+
// service worker/PWA documentation.
41+
navigator.serviceWorker.ready.then(() => {
42+
console.log(
43+
'This web app is being served cache-first by a service ' +
44+
'worker. To learn more, visit https://bit.ly/CRA-PWA'
45+
);
46+
});
47+
} else {
48+
// Is not localhost. Just register service worker
49+
registerValidSW(swUrl, config);
50+
}
4851
});
49-
} else {
50-
// Is not localhost. Just register service worker
51-
registerValidSW(swUrl, config);
52-
}
53-
});
54-
}
52+
}
5553
}
5654

5755
function registerValidSW(swUrl, config) {
58-
navigator.serviceWorker
59-
.register(swUrl)
60-
.then(registration => {
61-
registration.onupdatefound = () => {
62-
const installingWorker = registration.installing;
63-
if (installingWorker == null) {
64-
return;
65-
}
66-
installingWorker.onstatechange = () => {
67-
if (installingWorker.state === 'installed') {
68-
if (navigator.serviceWorker.controller) {
69-
// At this point, the updated precached content has been fetched,
70-
// but the previous service worker will still serve the older
71-
// content until all client tabs are closed.
72-
console.log(
73-
'New content is available and will be used when all ' +
74-
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
75-
);
56+
navigator.serviceWorker
57+
.register(swUrl)
58+
.then(registration => {
59+
registration.onupdatefound = () => {
60+
const installingWorker = registration.installing;
61+
if (installingWorker == null) {
62+
return;
63+
}
64+
installingWorker.onstatechange = () => {
65+
if (installingWorker.state === 'installed') {
66+
if (navigator.serviceWorker.controller) {
67+
// At this point, the updated precached content has been fetched,
68+
// but the previous service worker will still serve the older
69+
// content until all client tabs are closed.
70+
console.log(
71+
'New content is available and will be used when all ' +
72+
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
73+
);
7674

77-
// Execute callback
78-
if (config && config.onUpdate) {
79-
config.onUpdate(registration);
80-
}
81-
} else {
82-
// At this point, everything has been precached.
83-
// It's the perfect time to display a
84-
// "Content is cached for offline use." message.
85-
console.log('Content is cached for offline use.');
75+
// Execute callback
76+
if (config && config.onUpdate) {
77+
config.onUpdate(registration);
78+
}
79+
} else {
80+
// At this point, everything has been precached.
81+
// It's the perfect time to display a
82+
// "Content is cached for offline use." message.
83+
console.log('Content is cached for offline use.');
8684

87-
// Execute callback
88-
if (config && config.onSuccess) {
89-
config.onSuccess(registration);
90-
}
91-
}
92-
}
93-
};
94-
};
95-
})
96-
.catch(error => {
97-
console.error('Error during service worker registration:', error);
98-
});
85+
// Execute callback
86+
if (config && config.onSuccess) {
87+
config.onSuccess(registration);
88+
}
89+
}
90+
}
91+
};
92+
};
93+
})
94+
.catch(error => {
95+
console.error('Error during service worker registration:', error);
96+
});
9997
}
10098

10199
function checkValidServiceWorker(swUrl, config) {
102-
// Check if the service worker can be found. If it can't reload the page.
103-
fetch(swUrl)
104-
.then(response => {
105-
// Ensure service worker exists, and that we really are getting a JS file.
106-
const contentType = response.headers.get('content-type');
107-
if (
108-
response.status === 404 ||
109-
(contentType != null && contentType.indexOf('javascript') === -1)
110-
) {
111-
// No service worker found. Probably a different app. Reload the page.
112-
navigator.serviceWorker.ready.then(registration => {
113-
registration.unregister().then(() => {
114-
window.location.reload();
115-
});
100+
// Check if the service worker can be found. If it can't reload the page.
101+
fetch(swUrl)
102+
.then(response => {
103+
// Ensure service worker exists, and that we really are getting a JS file.
104+
const contentType = response.headers.get('content-type');
105+
if (response.status === 404 || (contentType != null && contentType.indexOf('javascript') === -1)) {
106+
// No service worker found. Probably a different app. Reload the page.
107+
navigator.serviceWorker.ready.then(registration => {
108+
registration.unregister().then(() => {
109+
window.location.reload();
110+
});
111+
});
112+
} else {
113+
// Service worker found. Proceed as normal.
114+
registerValidSW(swUrl, config);
115+
}
116+
})
117+
.catch(() => {
118+
console.log('No internet connection found. App is running in offline mode.');
116119
});
117-
} else {
118-
// Service worker found. Proceed as normal.
119-
registerValidSW(swUrl, config);
120-
}
121-
})
122-
.catch(() => {
123-
console.log(
124-
'No internet connection found. App is running in offline mode.'
125-
);
126-
});
127120
}
128121

129122
export function unregister() {
130-
if ('serviceWorker' in navigator) {
131-
navigator.serviceWorker.ready.then(registration => {
132-
registration.unregister();
133-
});
134-
}
123+
if ('serviceWorker' in navigator) {
124+
navigator.serviceWorker.ready.then(registration => {
125+
registration.unregister();
126+
});
127+
}
135128
}

0 commit comments

Comments
 (0)