Skip to content

Commit 9cec702

Browse files
committed
chore: add prettier
1 parent 08fe548 commit 9cec702

File tree

9 files changed

+122
-64
lines changed

9 files changed

+122
-64
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
es
2+
lib
3+
node_modules

.prettierrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.formatOnSave": true
3+
}

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"devDependencies": {
3030
"enzyme": "^3.6.0",
3131
"nwb": "^0.25.2",
32+
"prettier": "^2.4.1",
3233
"react": "^16.5.2",
3334
"react-dom": "^16.5.2",
3435
"react-router-dom": "^4.3.1"

src/LinkedIn.js

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
import React, { Component } from 'react';
2-
import PropTypes from 'prop-types';
1+
import React, { Component } from "react";
2+
import PropTypes from "prop-types";
33

4-
const getPopupPositionProperties = ({
5-
width = 600,
6-
height = 600,
7-
}) => {
8-
const left = (screen.width / 2) - (width / 2);
9-
const top = (screen.height / 2) - (height / 2);
4+
const getPopupPositionProperties = ({ width = 600, height = 600 }) => {
5+
const left = screen.width / 2 - width / 2;
6+
const top = screen.height / 2 - height / 2;
107
return `left=${left},top=${top},width=${width},height=${height}`;
11-
}
8+
};
129

13-
const generateRandomString = (length=20) => {
14-
var result = '';
15-
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
10+
const generateRandomString = (length = 20) => {
11+
var result = "";
12+
var characters =
13+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
1614
var charactersLength = characters.length;
17-
for ( var i = 0; i < length; i++ ) {
18-
result += characters.charAt(Math.floor(Math.random() * charactersLength));
15+
for (var i = 0; i < length; i++) {
16+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
1917
}
2018
return result;
21-
}
19+
};
2220

23-
const LINKEDIN_OAUTH2_STATE = 'linkedin_oauth2_state'
21+
const LINKEDIN_OAUTH2_STATE = "linkedin_oauth2_state";
2422

2523
export class LinkedIn extends Component {
2624
static propTypes = {
@@ -32,41 +30,46 @@ export class LinkedIn extends Component {
3230
clientId: PropTypes.string.isRequired,
3331
redirectUri: PropTypes.string.isRequired,
3432
renderElement: PropTypes.func,
35-
}
33+
};
3634

3735
componentWillUnmount() {
38-
window.removeEventListener('message', this.receiveMessage, false);
36+
window.removeEventListener("message", this.receiveMessage, false);
3937
if (this.popup && !this.popup.closed) this.popup.close();
4038
}
4139

4240
getUrl = () => {
43-
const { redirectUri, clientId, scope, supportIE, redirectPath } = this.props;
41+
const { redirectUri, clientId, scope, supportIE, redirectPath } =
42+
this.props;
4443
const scopeParam = `&scope=${supportIE ? scope : encodeURI(scope)}`;
4544
const state = generateRandomString();
4645
localStorage.setItem(LINKEDIN_OAUTH2_STATE, state);
4746
const linkedInAuthenLink = `https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=${clientId}&redirect_uri=${redirectUri}${scopeParam}&state=${state}`;
4847
if (supportIE) {
49-
const redirectLink = `${window.location.origin}${redirectPath}?linkedin_redirect_url=${encodeURIComponent(linkedInAuthenLink)}`;
48+
const redirectLink = `${
49+
window.location.origin
50+
}${redirectPath}?linkedin_redirect_url=${encodeURIComponent(
51+
linkedInAuthenLink
52+
)}`;
5053
return redirectLink;
5154
}
5255
return linkedInAuthenLink;
53-
}
56+
};
5457

5558
receiveMessage = (event) => {
5659
const state = localStorage.getItem(LINKEDIN_OAUTH2_STATE);
5760
if (event.origin === window.location.origin) {
58-
if (event.data.errorMessage && event.data.from === 'Linked In') {
61+
if (event.data.errorMessage && event.data.from === "Linked In") {
5962
// Prevent CSRF attack by testing state
6063
if (event.data.state !== state) {
6164
this.popup && this.popup.close();
6265
return;
6366
}
6467
this.props.onFailure(event.data);
6568
this.popup && this.popup.close();
66-
} else if (event.data.code && event.data.from === 'Linked In') {
69+
} else if (event.data.code && event.data.from === "Linked In") {
6770
// Prevent CSRF attack by testing state
6871
if (event.data.state !== state) {
69-
console.error('State does not match');
72+
console.error("State does not match");
7073
this.popup && this.popup.close();
7174
return;
7275
}
@@ -81,32 +84,42 @@ export class LinkedIn extends Component {
8184
e.preventDefault();
8285
}
8386
this.props.onClick && this.props.onClick();
84-
this.popup = window.open(this.getUrl(), '_blank', getPopupPositionProperties({ width: 600, height: 600 }));
85-
window.removeEventListener('message', this.receiveMessage, false);
86-
window.addEventListener('message', this.receiveMessage, false);
87-
}
88-
87+
this.popup = window.open(
88+
this.getUrl(),
89+
"_blank",
90+
getPopupPositionProperties({ width: 600, height: 600 })
91+
);
92+
window.removeEventListener("message", this.receiveMessage, false);
93+
window.addEventListener("message", this.receiveMessage, false);
94+
};
8995

9096
render() {
9197
const { className, disabled, children, renderElement, style } = this.props;
9298
if (renderElement) {
93-
return renderElement({ onClick: this.handleConnectLinkedInClick, disabled })
99+
return renderElement({
100+
onClick: this.handleConnectLinkedInClick,
101+
disabled,
102+
});
94103
}
95104
return (
96105
<button
97106
type="button"
98107
onClick={this.handleConnectLinkedInClick}
99108
className={className}
100109
disabled={disabled}
101-
style={style ? style : {
102-
background: 'none',
103-
color: 'inherit',
104-
border: 'none',
105-
padding: 0,
106-
cursor: 'pointer',
107-
font: 'inherit',
108-
outline: 'inherit',
109-
}}
110+
style={
111+
style
112+
? style
113+
: {
114+
background: "none",
115+
color: "inherit",
116+
border: "none",
117+
padding: 0,
118+
cursor: "pointer",
119+
font: "inherit",
120+
outline: "inherit",
121+
}
122+
}
110123
>
111124
{children}
112125
</button>
@@ -116,9 +129,15 @@ export class LinkedIn extends Component {
116129

117130
LinkedIn.defaultProps = {
118131
disabled: false,
119-
children: (<img src={require('../assets/linkedin.png')} alt="Log in with Linked In" style={{ maxWidth: '180px' }} />),
132+
children: (
133+
<img
134+
src={require("../assets/linkedin.png")}
135+
alt="Log in with Linked In"
136+
style={{ maxWidth: "180px" }}
137+
/>
138+
),
120139
supportIE: false,
121-
redirectPath: '/linkedin',
122-
scope: 'r_emailaddress',
140+
redirectPath: "/linkedin",
141+
scope: "r_emailaddress",
123142
};
124143
export default LinkedIn;

src/LinkedInPopUp.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
import { Component } from 'react';
2-
import QueryString from 'query-string';
1+
import { Component } from "react";
2+
import QueryString from "query-string";
33

44
class LinkedInPopUp extends Component {
55
componentDidMount() {
66
const params = QueryString.parse(window.location.search);
77
if (params.error) {
8-
const errorMessage = params.error_description || 'Login failed. Please try again.';
9-
window.opener && window.opener.postMessage({ error: params.error, state: params.state, errorMessage, from: 'Linked In' }, window.location.origin);
8+
const errorMessage =
9+
params.error_description || "Login failed. Please try again.";
10+
window.opener &&
11+
window.opener.postMessage(
12+
{
13+
error: params.error,
14+
state: params.state,
15+
errorMessage,
16+
from: "Linked In",
17+
},
18+
window.location.origin
19+
);
1020
// Close tab if user cancelled login
11-
if (params.error === 'user_cancelled_login') {
21+
if (params.error === "user_cancelled_login") {
1222
window.close();
1323
}
1424
}
1525
if (params.code) {
16-
window.opener && window.opener.postMessage({ code: params.code, state: params.state, from: 'Linked In'}, window.location.origin);
26+
window.opener &&
27+
window.opener.postMessage(
28+
{ code: params.code, state: params.state, from: "Linked In" },
29+
window.location.origin
30+
);
1731
}
18-
if(params.linkedin_redirect_url) {
19-
window.location.href = params.linkedin_redirect_url
32+
if (params.linkedin_redirect_url) {
33+
window.location.href = params.linkedin_redirect_url;
2034
}
2135
}
2236
render() {

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export {default as LinkedIn} from './LinkedIn';
2-
export {default as LinkedInPopUp} from './LinkedInPopUp';
3-
export {default} from './LinkedIn';
1+
export { default as LinkedIn } from "./LinkedIn";
2+
export { default as LinkedInPopUp } from "./LinkedInPopUp";
3+
export { default } from "./LinkedIn";

tests/index-test.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import expect from 'expect'
2-
import React from 'react'
3-
import { shallow } from 'enzyme';
4-
import Component from 'src/'
1+
import expect from "expect";
2+
import React from "react";
3+
import { shallow } from "enzyme";
4+
import Component from "src/";
55

6-
describe('Component', () => {
6+
describe("Component", () => {
77
let wrapper;
88
beforeEach(() => {
9-
wrapper = shallow(
10-
<Component />
11-
)
9+
wrapper = shallow(<Component />);
1210
});
1311

14-
it('render successfully', () => {
12+
it("render successfully", () => {
1513
expect(wrapper.length).toBe(1);
16-
})
17-
})
14+
});
15+
});

0 commit comments

Comments
 (0)