Skip to content

Commit c5c09f1

Browse files
committed
feat: add LinkedIn component (render props)
1 parent 02f5818 commit c5c09f1

File tree

8 files changed

+99
-30
lines changed

8 files changed

+99
-30
lines changed

.eslintrc.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ module.exports = {
1616

1717
rules: {
1818
'@typescript-eslint/no-unused-vars': 'error',
19-
// to enforce using type for object type definitions, can be type or interface
20-
'@typescript-eslint/consistent-type-definitions': ['error', 'type'],
2119
},
2220

2321
env: {

preview/App.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import React from 'react';
22
import { BrowserRouter, Route, Switch } from 'react-router-dom';
33

44
import { LinkedInCallback } from '../src/LinkedInCallback';
5-
import LinkedInPage from './LinkedInPage';
5+
// import LinkedInPage from './LinkedInPageHook';
6+
import LinkedInPage from './LinkedInPageRenderProps';
67

78
function App() {
89
return (
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22

33
import { useLinkedIn } from '../src/useLinkedIn';
44
import linkedin from '../assets/linkedin.png';
@@ -17,19 +17,19 @@ function LinkedInPage() {
1717
setErrorMessage(error.errorMessage);
1818
},
1919
});
20-
const [code, setCode] = React.useState('');
21-
const [errorMessage, setErrorMessage] = React.useState('');
20+
const [code, setCode] = useState('');
21+
const [errorMessage, setErrorMessage] = useState('');
2222

2323
return (
2424
<div>
25-
<div style={{ cursor: 'pointer' }} onClick={linkedInLogin}>
26-
<img
27-
src={linkedin}
28-
alt="Log in with Linked In"
29-
style={{ maxWidth: '180px' }}
30-
/>
31-
</div>
32-
25+
hooks
26+
<br />
27+
<img
28+
onClick={linkedInLogin}
29+
src={linkedin}
30+
alt="Sign in with Linked In"
31+
style={{ maxWidth: '180px', cursor: 'pointer' }}
32+
/>
3333
{!code && <div>No code</div>}
3434
{code && <div>Code: {code}</div>}
3535
{errorMessage && <div>{errorMessage}</div>}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, { useState } from 'react';
2+
3+
import { LinkedIn } from '../src/LinkedIn';
4+
import linkedin from '../assets/linkedin.png';
5+
6+
function LinkedInPage() {
7+
const [code, setCode] = useState('');
8+
const [errorMessage, setErrorMessage] = useState('');
9+
10+
return (
11+
<div>
12+
render props
13+
<br />
14+
<LinkedIn
15+
clientId="86vhj2q7ukf83q"
16+
redirectUri={`${window.location.origin}/linkedin`}
17+
onSuccess={(code) => {
18+
console.log(code);
19+
setCode(code);
20+
}}
21+
scope="r_emailaddress r_liteprofile"
22+
onError={(error) => {
23+
console.log(error);
24+
setErrorMessage(error.errorMessage);
25+
}}
26+
>
27+
{({ linkedInLogin }) => (
28+
<img
29+
onClick={linkedInLogin}
30+
src={linkedin}
31+
alt="Sign in with Linked In"
32+
style={{ maxWidth: '180px', cursor: 'pointer' }}
33+
/>
34+
)}
35+
</LinkedIn>
36+
{!code && <div>No code</div>}
37+
{code && <div>Code: {code}</div>}
38+
{errorMessage && <div>{errorMessage}</div>}
39+
</div>
40+
);
41+
}
42+
43+
export default LinkedInPage;

src/LinkedIn.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { LinkedInType } from './types';
2+
import { useLinkedIn } from './useLinkedIn';
3+
4+
export function LinkedIn({
5+
children,
6+
redirectUri,
7+
clientId,
8+
onSuccess,
9+
onError,
10+
scope,
11+
closePopupMessage,
12+
}: LinkedInType) {
13+
const { linkedInLogin } = useLinkedIn({
14+
redirectUri,
15+
clientId,
16+
onSuccess,
17+
onError,
18+
scope,
19+
closePopupMessage,
20+
});
21+
return children({ linkedInLogin });
22+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './useLinkedIn';
2+
export * from './LinkedIn';
23
export * from './LinkedInCallback';

src/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export interface useLinkedInType {
2+
redirectUri: string;
3+
clientId: string;
4+
onSuccess: (code: string) => void;
5+
onError?: ({
6+
error,
7+
errorMessage,
8+
}: {
9+
error: string;
10+
errorMessage: string;
11+
}) => void;
12+
scope?: string;
13+
closePopupMessage?: string;
14+
}
15+
16+
export interface LinkedInType extends useLinkedInType {
17+
children: ({ linkedInLogin }) => JSX.Element;
18+
}

src/useLinkedIn.tsx

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useCallback, useEffect, useRef } from 'react';
2+
import { useLinkedInType } from './types';
23
import { LINKEDIN_OAUTH2_STATE } from './utils';
34

45
const getPopupPositionProperties = ({ width = 600, height = 600 }) => {
@@ -18,29 +19,14 @@ const generateRandomString = (length = 20) => {
1819
return result;
1920
};
2021

21-
type LinkedInType = {
22-
redirectUri: string;
23-
clientId: string;
24-
onSuccess: (code: string) => void;
25-
onError?: ({
26-
error,
27-
errorMessage,
28-
}: {
29-
error: string;
30-
errorMessage: string;
31-
}) => void;
32-
scope?: string;
33-
closePopupMessage?: string;
34-
};
35-
3622
export function useLinkedIn({
3723
redirectUri,
3824
clientId,
3925
onSuccess,
4026
onError,
4127
scope = 'r_emailaddress',
4228
closePopupMessage = 'User closed the popup',
43-
}: LinkedInType) {
29+
}: useLinkedInType) {
4430
const popupRef = useRef<Window>(null);
4531
const popUpIntervalRef = useRef<number>(null);
4632

0 commit comments

Comments
 (0)