Skip to content

Commit 00fbdb5

Browse files
committed
refactor: 💡 restructure into context-like API
1 parent b22fb0a commit 00fbdb5

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import {createElement as h} from 'react';
22
import {storiesOf} from '@storybook/react';
3-
import {GoogleAuthButton} from '..';
3+
import {createGoogleAuthContext} from '..';
44
// import ShowDocs from '../../../.storybook/ShowDocs'
55

66
const clientId = '305188012168-htfit0k0u4vegn0f6hn10rcqoj1m77ca.apps.googleusercontent.com';
7+
const ctx1 = createGoogleAuthContext(clientId);
78

89
storiesOf('Context/GoogleAuthButton', module)
910
// .add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/GoogleAuthButton.md')}))
1011
.add('Default', () =>
11-
<GoogleAuthButton clientId={clientId}>{(state) =>
12-
<button>Sign in with Google!</button>
13-
}</GoogleAuthButton>
12+
<ctx1.Provider>
13+
<ctx1.Consumer>{({signIn}) =>
14+
<button onClick={signIn}>Sign in with Google!</button>
15+
}</ctx1.Consumer>
16+
</ctx1.Provider>
1417
);
File renamed without changes.

‎src/GoogleAuth/index.ts‎

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,54 @@
1-
import {Component} from 'react';
2-
import renderProp from '../util/renderProp';
3-
import {IUniversalInterfaceProps} from '../typing';
1+
import {h} from '../util';
2+
import {Component, createContext} from 'react';
3+
import {getGapiAuthInstance} from './gapi';
44

5-
export interface IGoogleAuthButtonProps extends IUniversalInterfaceProps<IGoogleAuthButtonState> {
6-
clientId: string;
5+
export interface IGoogleAuthProviderProps {
6+
children;
77
}
88

9-
export interface IGoogleAuthButtonState {
10-
loading?: boolean;
9+
export interface IGoogleAuthProviderState {
10+
loading?: boolean;
11+
signIn: () => Promise<void>;
12+
signOut: () => Promise<void>;
13+
user: any | null;
1114
}
1215

13-
export class GoogleAuthButton extends Component<IGoogleAuthButtonProps, IGoogleAuthButtonState> {
16+
export interface IGoogleAuthConsumerProps {
17+
children: (state: IGoogleAuthProviderState) => React.ReactNode;
18+
}
19+
20+
export const createGoogleAuthContext = (clientId: string) => {
21+
const context = createContext({});
22+
23+
let googleAuth;
24+
const getAuthInstance = async () => {
25+
if (!googleAuth) {
26+
googleAuth = await getGapiAuthInstance(clientId);
27+
}
28+
29+
return googleAuth;
30+
};
31+
32+
class Provider extends Component<IGoogleAuthProviderProps, IGoogleAuthProviderState> {
33+
state: IGoogleAuthProviderState = {
34+
signIn: async () => {
35+
const googleAuth = await getAuthInstance();
36+
await googleAuth.signIn();
37+
},
38+
signOut: async () => {
39+
const googleAuth = await getAuthInstance();
40+
await googleAuth.signOut();
41+
},
42+
user: null,
43+
};
44+
1445
render () {
15-
return renderProp(this.props, this.state);
46+
return h(context.Provider, {value: this.state}, this.props.children);
1647
}
17-
}
48+
}
49+
50+
return {
51+
Provider,
52+
Consumer: context.Consumer as React.SFC<IGoogleAuthConsumerProps>,
53+
};
54+
};

0 commit comments

Comments
 (0)