@tryfinch/react-connect 3.0.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,11 +5,11 @@
5
5
  "license": "MIT",
6
6
  "private": true,
7
7
  "dependencies": {
8
+ "@tryfinch/react-connect": "file:..",
8
9
  "@types/react": "^17.0.37",
9
10
  "prop-types": "^15.6.2",
10
11
  "react": "file:../node_modules/react",
11
- "react-dom": "^16.9.0",
12
- "react-finch-connect": "file:.."
12
+ "react-dom": "^16.9.0"
13
13
  },
14
14
  "scripts": {
15
15
  "start": "react-scripts start",
@@ -1,14 +1,49 @@
1
- .App {
1
+ .container {
2
2
  text-align: center;
3
- }
4
-
5
- .App-header {
6
3
  background-color: #282c34;
7
4
  min-height: 100vh;
8
5
  display: flex;
9
6
  flex-direction: column;
10
7
  align-items: center;
11
8
  justify-content: center;
12
- font-size: calc(10px + 2vmin);
9
+ font-size: calc(7px + 2vmin);
10
+ color: white;
11
+ }
12
+
13
+ a {
13
14
  color: white;
14
15
  }
16
+
17
+ .actions {
18
+ background-color: #4f4f4f;
19
+ padding: 1em;
20
+ border-radius: 10px;
21
+ margin: 1em 0;
22
+ }
23
+
24
+ .cta {
25
+ font-size: calc(7px + 2vmin);
26
+ padding: .5em;
27
+ }
28
+
29
+ .row {
30
+ margin: 2em 0;
31
+ }
32
+
33
+ .top-label {
34
+ display: block;
35
+ margin-bottom: .2em;
36
+ }
37
+
38
+ input[type=checkbox] {
39
+ width: 2em;
40
+ height: 2em;
41
+ }
42
+
43
+ .results {
44
+ background-color: #4f4f4f;
45
+ padding: 1em;
46
+ border-radius: 10px;
47
+ text-align: left;
48
+ margin: 1em 0;
49
+ }
@@ -1,32 +1,53 @@
1
1
  import React, { useState } from 'react';
2
- import { useFinchConnect, SuccessEvent, ErrorEvent } from 'react-finch-connect';
2
+ import { useFinchConnect, SuccessEvent, ErrorEvent } from '@tryfinch/react-connect';
3
+
4
+ import Result, { ResultContainer } from './Result';
5
+
3
6
  import './App.css';
4
7
 
5
8
  const App = () => {
6
- const [code, setCode] = useState<string>();
9
+ const [sendState, setSendState] = useState<boolean>(false);
10
+ const [result, setResult] = useState<ResultContainer>();
7
11
 
8
- const onSuccess = ({ code }: SuccessEvent) => setCode(code);
9
- const onError = ({ errorMessage }: ErrorEvent) => console.error(errorMessage);
10
- const onClose = () => console.log('User exited Finch Connect');
12
+ const onSuccess = (value: SuccessEvent) => setResult({ kind: 'success', value });
13
+ const onError = (value: ErrorEvent) => setResult({ kind: 'error', value });
14
+ const onClose = () => setResult({ kind: 'closed' });
11
15
 
12
16
  const { open } = useFinchConnect({
13
- clientId: '<your-client-id>',
17
+ clientId: 'c90b78c6-2151-4ca3-8fea-ccb708ffc5d9',
14
18
  products: ['company', 'directory', 'individual', 'employment'],
15
- // sandbox: false,
19
+ sandbox: true,
16
20
  // payrollProvider: '<payroll-provider-id>',
17
21
  onSuccess,
18
22
  onError,
19
23
  onClose,
20
24
  });
21
25
 
26
+ const submissionHandler: React.FormEventHandler<HTMLFormElement> = (e) => {
27
+ e.preventDefault();
28
+ open({
29
+ ...(sendState ? { state: new Date().toISOString() } : undefined),
30
+ })
31
+ };
32
+
22
33
  return (
23
- <div className="App">
24
- <header className="App-header">
25
- <p>Code: {code}</p>
26
- <button type="button" onClick={() => open()}>
27
- Open Finch Connect
28
- </button>
29
- </header>
34
+ <div className="container">
35
+ <h2><a href="https://www.npmjs.com/package/@tryfinch/react-connect">@tryfinch/react-connect</a> Example App</h2>
36
+ <form className="actions" onSubmit={submissionHandler}>
37
+ <div className="row">
38
+ <label className="top-label">Include State:</label>
39
+ <input type="checkbox" checked={sendState} onChange={() => setSendState(prev => !prev)} />
40
+ </div>
41
+ <div className="row">
42
+ <button className="cta" type="submit">
43
+ Open Finch Connect
44
+ </button>
45
+ </div>
46
+ </form>
47
+ <div className="results">
48
+ { !result && <p>Complete a Finch Connect session and the success event will be displayed here</p> }
49
+ { result && <Result result={result} /> }
50
+ </div>
30
51
  </div>
31
52
  );
32
53
  };
@@ -0,0 +1,26 @@
1
+ import { SuccessEvent, ErrorEvent } from '@tryfinch/react-connect';
2
+
3
+ export type ResultContainer = {
4
+ kind: 'success';
5
+ value: SuccessEvent;
6
+ } | {
7
+ kind: 'error';
8
+ value: ErrorEvent;
9
+ } | {
10
+ kind: 'closed',
11
+ };
12
+
13
+ const Result = ({ result }: { result: ResultContainer }) => {
14
+ if (result.kind === 'closed') {
15
+ return <>
16
+ <p>Closed!</p>
17
+ </>;
18
+ }
19
+
20
+ return <>
21
+ <p>{ result.kind === 'error' ? 'Error' : 'Success' }:</p>
22
+ <pre>{ JSON.stringify(result.value, null, 2) }</pre>
23
+ </>;
24
+ };
25
+
26
+ export default Result
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryfinch/react-connect",
3
- "version": "3.0.0",
3
+ "version": "3.1.1",
4
4
  "description": "Finch SDK for embedding Finch Connect in API React Single Page Applications (SPA)",
5
5
  "keywords": [
6
6
  "finch",
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ import { useEffect } from 'react';
2
2
 
3
3
  export type SuccessEvent = {
4
4
  code: string;
5
+ state?: string;
5
6
  };
6
7
 
7
8
  export type ErrorEvent = {
@@ -12,6 +13,7 @@ export type ConnectOptions = {
12
13
  category: string | null;
13
14
  clientId: string;
14
15
  manual: boolean;
16
+ state: string | null;
15
17
  onSuccess: (e: SuccessEvent) => void;
16
18
  onError: (e: ErrorEvent) => void;
17
19
  onClose: () => void;
@@ -21,15 +23,27 @@ export type ConnectOptions = {
21
23
  zIndex: number;
22
24
  };
23
25
 
24
- type OpenFn = (overrides?: Partial<Pick<ConnectOptions, 'products'>>) => void;
26
+ type OpenFn = (overrides?: Partial<Pick<ConnectOptions, 'products' | 'state'>>) => void;
27
+
28
+ const POST_MESSAGE_NAME = 'finch-auth-message' as const;
29
+
30
+ type FinchConnectAuthMessage = { name: typeof POST_MESSAGE_NAME } & (
31
+ | {
32
+ kind: 'closed';
33
+ }
34
+ | {
35
+ kind: 'success';
36
+ code: string;
37
+ state?: string;
38
+ }
39
+ | {
40
+ kind: 'error';
41
+ error: string;
42
+ }
43
+ );
25
44
 
26
45
  interface FinchConnectPostMessage {
27
- data: {
28
- name: string;
29
- code?: string;
30
- error?: string;
31
- closed?: boolean;
32
- };
46
+ data: FinchConnectAuthMessage;
33
47
  origin: string;
34
48
  }
35
49
 
@@ -45,9 +59,9 @@ const constructAuthUrl = ({
45
59
  products,
46
60
  manual,
47
61
  sandbox,
62
+ state,
48
63
  }: Partial<ConnectOptions>) => {
49
64
  const authUrl = new URL(`${BASE_FINCH_CONNECT_URI}/authorize`);
50
-
51
65
  if (clientId) authUrl.searchParams.append('client_id', clientId);
52
66
  if (payrollProvider) authUrl.searchParams.append('payroll_provider', payrollProvider);
53
67
  if (category) authUrl.searchParams.append('category', category);
@@ -57,6 +71,7 @@ const constructAuthUrl = ({
57
71
  authUrl.searchParams.append('mode', 'employer');
58
72
  if (manual) authUrl.searchParams.append('manual', String(manual));
59
73
  if (sandbox) authUrl.searchParams.append('sandbox', String(sandbox));
74
+ if (state) authUrl.searchParams.append('state', state);
60
75
  // replace with actual SDK version by rollup
61
76
  authUrl.searchParams.append('sdk_version', 'react-SDK_VERSION');
62
77
 
@@ -76,6 +91,7 @@ const DEFAULT_OPTIONS: Omit<ConnectOptions, 'clientId'> = {
76
91
  payrollProvider: null,
77
92
  products: [],
78
93
  sandbox: false,
94
+ state: null,
79
95
  zIndex: 999,
80
96
  };
81
97
 
@@ -121,21 +137,34 @@ export const useFinchConnect = (options: Partial<ConnectOptions>): { open: OpenF
121
137
 
122
138
  useEffect(() => {
123
139
  function handleFinchAuth(event: FinchConnectPostMessage) {
124
- const handleFinchAuthSuccess = (code: string) => combinedOptions.onSuccess({ code });
125
- const handleFinchAuthError = (error: string) =>
126
- combinedOptions.onError({ errorMessage: error });
127
- const handleFinchAuthClose = () => combinedOptions.onClose();
128
-
129
140
  if (!event.data) return;
130
141
  if (event.data.name !== FINCH_AUTH_MESSAGE_NAME) return;
131
142
  if (!event.origin.startsWith(BASE_FINCH_CONNECT_URI)) return;
132
143
 
133
- const { code, error, closed } = event.data;
134
-
135
144
  close();
136
- if (code) handleFinchAuthSuccess(code);
137
- else if (error) handleFinchAuthError(error);
138
- else if (closed) handleFinchAuthClose();
145
+
146
+ switch (event.data.kind) {
147
+ case 'closed':
148
+ combinedOptions.onClose();
149
+ break;
150
+ case 'error':
151
+ combinedOptions.onError({ errorMessage: event.data.error });
152
+ break;
153
+ case 'success':
154
+ combinedOptions.onSuccess({
155
+ code: event.data.code,
156
+ state: event.data.state,
157
+ });
158
+ break;
159
+ default: {
160
+ // This case should never happen, if it does it should be reported to us
161
+ combinedOptions.onError({
162
+ errorMessage: `Report to developers@tryfinch.com: unable to handle window.postMessage for: ${JSON.stringify(
163
+ event.data
164
+ )}`,
165
+ });
166
+ }
167
+ }
139
168
  }
140
169
 
141
170
  window.addEventListener('message', handleFinchAuth);