@tryfinch/react-connect 3.1.0 → 3.3.0

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,13 +1,17 @@
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
17
  clientId: '<your-client-id>',
@@ -19,14 +23,31 @@ const App = () => {
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.1.0",
3
+ "version": "3.3.0",
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
@@ -19,11 +19,19 @@ export type ConnectOptions = {
19
19
  onClose: () => void;
20
20
  payrollProvider: string | null;
21
21
  products: string[];
22
- sandbox: boolean;
22
+ sandbox:
23
+ | 'provider' /** This is to enable the new Provider Sandbox */
24
+ /**
25
+ * The old sandbox flag retained for backwards compatibility.
26
+ * It's defaults to the Provider Sandbox
27
+ */
28
+ | boolean;
23
29
  zIndex: number;
24
30
  };
25
31
 
26
- type OpenFn = (overrides?: Partial<Pick<ConnectOptions, 'products'>>) => void;
32
+ type OpenFn = (
33
+ overrides?: Partial<Pick<ConnectOptions, 'products' | 'state' | 'payrollProvider'>>
34
+ ) => void;
27
35
 
28
36
  const POST_MESSAGE_NAME = 'finch-auth-message' as const;
29
37
 
@@ -95,16 +103,26 @@ const DEFAULT_OPTIONS: Omit<ConnectOptions, 'clientId'> = {
95
103
  zIndex: 999,
96
104
  };
97
105
 
106
+ let isUseFinchConnectInitialized = false;
107
+
98
108
  export const useFinchConnect = (options: Partial<ConnectOptions>): { open: OpenFn } => {
99
109
  if (!options.clientId) throw new Error('must specify clientId in options for useFinchConnect');
100
110
 
111
+ if (isUseFinchConnectInitialized) {
112
+ console.error(
113
+ 'One useFinchConnect hook has already been registered. Please ensure to only call useFinchConnect once to avoid your event callbacks getting called more than once. You can pass in override options to the open function if you so require.'
114
+ );
115
+ } else {
116
+ isUseFinchConnectInitialized = true;
117
+ }
118
+
101
119
  const combinedOptions: ConnectOptions = {
102
120
  clientId: '',
103
121
  ...DEFAULT_OPTIONS,
104
122
  ...options,
105
123
  };
106
124
 
107
- const open: OpenFn = (overrides?: Partial<Pick<ConnectOptions, 'products'>>) => {
125
+ const open: OpenFn = (overrides) => {
108
126
  const openOptions: ConnectOptions = {
109
127
  ...combinedOptions,
110
128
  ...overrides,
@@ -168,7 +186,10 @@ export const useFinchConnect = (options: Partial<ConnectOptions>): { open: OpenF
168
186
  }
169
187
 
170
188
  window.addEventListener('message', handleFinchAuth);
171
- return () => window.removeEventListener('message', handleFinchAuth);
189
+ return () => {
190
+ window.removeEventListener('message', handleFinchAuth);
191
+ isUseFinchConnectInitialized = false;
192
+ };
172
193
  }, [combinedOptions.onClose, combinedOptions.onError, combinedOptions.onSuccess]);
173
194
 
174
195
  return {