@oneblink/apps-react 3.2.0-beta.1 → 4.0.0-beta.10
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.
- package/README.md +2 -8
- package/dist/OneBlinkAutoSaveForm.d.ts +25 -5
- package/dist/OneBlinkAutoSaveForm.js +12 -0
- package/dist/OneBlinkAutoSaveForm.js.map +1 -1
- package/dist/OneBlinkForm.d.ts +558 -8
- package/dist/OneBlinkForm.js +546 -0
- package/dist/OneBlinkForm.js.map +1 -1
- package/dist/OneBlinkFormBase.d.ts +53 -8
- package/dist/OneBlinkFormBase.js +49 -25
- package/dist/OneBlinkFormBase.js.map +1 -1
- package/dist/OneBlinkReadOnlyForm.d.ts +83 -6
- package/dist/OneBlinkReadOnlyForm.js +80 -1
- package/dist/OneBlinkReadOnlyForm.js.map +1 -1
- package/dist/PaymentReceipt.d.ts +59 -0
- package/dist/PaymentReceipt.js +49 -0
- package/dist/PaymentReceipt.js.map +1 -1
- package/dist/components/formStore/FormStoreTableProvider.js +23 -1
- package/dist/components/formStore/FormStoreTableProvider.js.map +1 -1
- package/dist/components/formStore/OneBlinkFormStoreClearFiltersButton.d.ts +5 -0
- package/dist/components/formStore/OneBlinkFormStoreClearFiltersButton.js +5 -0
- package/dist/components/formStore/OneBlinkFormStoreClearFiltersButton.js.map +1 -1
- package/dist/components/formStore/OneBlinkFormStoreColumnsButton.d.ts +5 -0
- package/dist/components/formStore/OneBlinkFormStoreColumnsButton.js +5 -0
- package/dist/components/formStore/OneBlinkFormStoreColumnsButton.js.map +1 -1
- package/dist/components/formStore/OneBlinkFormStoreDownloadButton.d.ts +5 -0
- package/dist/components/formStore/OneBlinkFormStoreDownloadButton.js +5 -0
- package/dist/components/formStore/OneBlinkFormStoreDownloadButton.js.map +1 -1
- package/dist/components/formStore/OneBlinkFormStoreProvider.d.ts +5 -0
- package/dist/components/formStore/OneBlinkFormStoreProvider.js +5 -0
- package/dist/components/formStore/OneBlinkFormStoreProvider.js.map +1 -1
- package/dist/components/formStore/OneBlinkFormStoreRefreshButton.d.ts +5 -0
- package/dist/components/formStore/OneBlinkFormStoreRefreshButton.js +5 -0
- package/dist/components/formStore/OneBlinkFormStoreRefreshButton.js.map +1 -1
- package/dist/components/formStore/OneBlinkFormStoreTable.d.ts +4 -0
- package/dist/components/formStore/OneBlinkFormStoreTable.js +4 -0
- package/dist/components/formStore/OneBlinkFormStoreTable.js.map +1 -1
- package/dist/components/pickers/V4CompatibleDatePicker.d.ts +5 -0
- package/dist/components/pickers/V4CompatibleDatePicker.js +5 -0
- package/dist/components/pickers/V4CompatibleDatePicker.js.map +1 -1
- package/dist/components/pickers/V4CompatibleDateTimePicker.d.ts +5 -0
- package/dist/components/pickers/V4CompatibleDateTimePicker.js +5 -0
- package/dist/components/pickers/V4CompatibleDateTimePicker.js.map +1 -1
- package/dist/components/pickers/V4CompatibleTimePicker.d.ts +5 -0
- package/dist/components/pickers/V4CompatibleTimePicker.js +5 -0
- package/dist/components/pickers/V4CompatibleTimePicker.js.map +1 -1
- package/dist/components/renderer/LookupButton.js +2 -2
- package/dist/components/renderer/LookupButton.js.map +1 -1
- package/dist/components/renderer/LookupNotification.js +43 -25
- package/dist/components/renderer/LookupNotification.js.map +1 -1
- package/dist/components/renderer/OneBlinkFormElements.d.ts +3 -3
- package/dist/components/renderer/OneBlinkFormElements.js.map +1 -1
- package/dist/components/renderer/PageFormElements.d.ts +3 -3
- package/dist/components/renderer/PageFormElements.js.map +1 -1
- package/dist/components/renderer/ProgressBar.d.ts +7 -3
- package/dist/components/renderer/ProgressBar.js +5 -0
- package/dist/components/renderer/ProgressBar.js.map +1 -1
- package/dist/form-elements/FormElementBarcodeScanner.js +2 -2
- package/dist/form-elements/FormElementBarcodeScanner.js.map +1 -1
- package/dist/form-elements/FormElementCalculation.js.map +1 -1
- package/dist/form-elements/FormElementForm.d.ts +4 -4
- package/dist/form-elements/FormElementForm.js.map +1 -1
- package/dist/form-elements/FormElementRepeatableSet.d.ts +4 -4
- package/dist/form-elements/FormElementRepeatableSet.js.map +1 -1
- package/dist/form-elements/FormElementSummary.js.map +1 -1
- package/dist/hooks/useAuth.d.ts +81 -6
- package/dist/hooks/useAuth.js +54 -0
- package/dist/hooks/useAuth.js.map +1 -1
- package/dist/hooks/useBooleanState.d.ts +42 -5
- package/dist/hooks/useBooleanState.js +36 -0
- package/dist/hooks/useBooleanState.js.map +1 -1
- package/dist/hooks/useClickOutsideElement.d.ts +40 -0
- package/dist/hooks/useClickOutsideElement.js +40 -0
- package/dist/hooks/useClickOutsideElement.js.map +1 -1
- package/dist/hooks/useConditionalLogic.d.ts +2 -3
- package/dist/hooks/useConditionalLogic.js.map +1 -1
- package/dist/hooks/useDrafts.d.ts +82 -0
- package/dist/hooks/useDrafts.js +204 -0
- package/dist/hooks/useDrafts.js.map +1 -0
- package/dist/hooks/useFormSubmissionAutoSaveState.d.ts +18 -5
- package/dist/hooks/useFormSubmissionAutoSaveState.js +10 -0
- package/dist/hooks/useFormSubmissionAutoSaveState.js.map +1 -1
- package/dist/hooks/useFormSubmissionModelContext.d.ts +4 -4
- package/dist/hooks/useFormSubmissionModelContext.js.map +1 -1
- package/dist/hooks/useFormSubmissionState.d.ts +45 -5
- package/dist/hooks/useFormSubmissionState.js +37 -0
- package/dist/hooks/useFormSubmissionState.js.map +1 -1
- package/dist/hooks/useFormValidation.d.ts +3 -3
- package/dist/hooks/useFormValidation.js.map +1 -1
- package/dist/hooks/useInjectPages.d.ts +2 -3
- package/dist/hooks/useInjectPages.js.map +1 -1
- package/dist/hooks/useIsMounted.d.ts +20 -0
- package/dist/hooks/useIsMounted.js +19 -0
- package/dist/hooks/useIsMounted.js.map +1 -1
- package/dist/hooks/useIsOffline.d.ts +54 -0
- package/dist/hooks/useIsOffline.js +54 -0
- package/dist/hooks/useIsOffline.js.map +1 -1
- package/dist/hooks/useLoadDataState.d.ts +46 -3
- package/dist/hooks/useLoadDataState.js +41 -0
- package/dist/hooks/useLoadDataState.js.map +1 -1
- package/dist/hooks/useLogin.d.ts +400 -13
- package/dist/hooks/useLogin.js +300 -0
- package/dist/hooks/useLogin.js.map +1 -1
- package/dist/hooks/useLookupNotification.d.ts +6 -2
- package/dist/hooks/useLookupNotification.js +29 -3
- package/dist/hooks/useLookupNotification.js.map +1 -1
- package/dist/hooks/useLookups.d.ts +3 -3
- package/dist/hooks/useLookups.js.map +1 -1
- package/dist/hooks/useNullableState.d.ts +46 -1
- package/dist/hooks/useNullableState.js +42 -1
- package/dist/hooks/useNullableState.js.map +1 -1
- package/dist/hooks/usePendingSubmissions.d.ts +103 -0
- package/dist/hooks/usePendingSubmissions.js +195 -0
- package/dist/hooks/usePendingSubmissions.js.map +1 -0
- package/dist/hooks/useSignUp.d.ts +5 -0
- package/dist/hooks/useSignUp.js +5 -0
- package/dist/hooks/useSignUp.js.map +1 -1
- package/dist/index.d.ts +7 -4
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/services/checkBsbsAreInvalid.d.ts +2 -3
- package/dist/services/checkBsbsAreInvalid.js.map +1 -1
- package/dist/services/checkIfAttachmentsExist.d.ts +2 -3
- package/dist/services/checkIfAttachmentsExist.js.map +1 -1
- package/dist/services/checkIfBsbsAreValidating.d.ts +2 -3
- package/dist/services/checkIfBsbsAreValidating.js.map +1 -1
- package/dist/services/cleanFormSubmissionModel.d.ts +4 -4
- package/dist/services/cleanFormSubmissionModel.js +2 -1
- package/dist/services/cleanFormSubmissionModel.js.map +1 -1
- package/dist/services/form-validation.d.ts +3 -3
- package/dist/services/form-validation.js.map +1 -1
- package/dist/services/generate-default-data.d.ts +2 -3
- package/dist/services/generate-default-data.js +3 -1
- package/dist/services/generate-default-data.js.map +1 -1
- package/dist/services/generateFreshdeskDependentFieldElements.js.map +1 -1
- package/dist/services/getDateRangeConfiguration.d.ts +2 -3
- package/dist/services/getDateRangeConfiguration.js.map +1 -1
- package/dist/services/getRepeatableSetEntriesConfiguration.d.ts +2 -3
- package/dist/services/getRepeatableSetEntriesConfiguration.js.map +1 -1
- package/dist/styles/receipt.scss +1 -1
- package/dist/styles/repeatable-set.scss +2 -2
- package/dist/typedoc.d.ts +2 -0
- package/dist/typedoc.js +3 -0
- package/dist/typedoc.js.map +1 -0
- package/dist/types/form.d.ts +5 -6
- package/dist/types/form.js.map +1 -1
- package/package.json +10 -4
- package/dist/services/determineIsInfoPage.d.ts +0 -2
- package/dist/services/determineIsInfoPage.js +0 -17
- package/dist/services/determineIsInfoPage.js.map +0 -1
package/dist/hooks/useAuth.js
CHANGED
@@ -6,6 +6,41 @@ const AuthContext = React.createContext({
|
|
6
6
|
userFriendlyName: undefined,
|
7
7
|
isUsingFormsKey: false,
|
8
8
|
});
|
9
|
+
/**
|
10
|
+
* `<AuthContextProvider />` is a React Component that provides the context for
|
11
|
+
* the `useAuth()` hook to be used by components further down your component
|
12
|
+
* tree. **It should only be included in your component tree once and ideally at
|
13
|
+
* the root of the application.**
|
14
|
+
*
|
15
|
+
* #### Example
|
16
|
+
*
|
17
|
+
* ```jsx
|
18
|
+
* import * as React from 'react'
|
19
|
+
* import { AuthContextProvider, useAuth } from '@oneblink/apps-react'
|
20
|
+
*
|
21
|
+
* function Component() {
|
22
|
+
* const auth = useAuth()
|
23
|
+
* // use auth here
|
24
|
+
* }
|
25
|
+
*
|
26
|
+
* function App() {
|
27
|
+
* return (
|
28
|
+
* <AuthContextProvider>
|
29
|
+
* <Component />
|
30
|
+
* </AuthContextProvider>
|
31
|
+
* )
|
32
|
+
* }
|
33
|
+
*
|
34
|
+
* const root = document.getElementById('root')
|
35
|
+
* if (root) {
|
36
|
+
* ReactDOM.render(<App />, root)
|
37
|
+
* }
|
38
|
+
* ```
|
39
|
+
*
|
40
|
+
* @param props
|
41
|
+
* @returns
|
42
|
+
* @group Components
|
43
|
+
*/
|
9
44
|
export function AuthContextProvider({ children, formsKeyToken, userToken, }) {
|
10
45
|
const [value, setValue] = React.useState(() => {
|
11
46
|
authService.setFormsKeyToken(formsKeyToken);
|
@@ -37,6 +72,25 @@ export function AuthContextProvider({ children, formsKeyToken, userToken, }) {
|
|
37
72
|
}, []);
|
38
73
|
return React.createElement(AuthContext.Provider, { value: value }, children);
|
39
74
|
}
|
75
|
+
/**
|
76
|
+
* A React hook for containing state associated the current user. **This hook
|
77
|
+
* requires [`<AuthContextProvider />`](./AuthContextProvider.html) to be
|
78
|
+
* present in your component tree.**
|
79
|
+
*
|
80
|
+
* Example
|
81
|
+
*
|
82
|
+
* ```js
|
83
|
+
* import { useAuth } from '@oneblink/apps-react'
|
84
|
+
*
|
85
|
+
* function Component() {
|
86
|
+
* const { isLoggedIn, userProfile, userFriendlyName, isUsingFormsKey } =
|
87
|
+
* useAuth()
|
88
|
+
* }
|
89
|
+
* ```
|
90
|
+
*
|
91
|
+
* @returns
|
92
|
+
* @group Hooks
|
93
|
+
*/
|
40
94
|
export default function useAuth() {
|
41
95
|
return React.useContext(AuthContext);
|
42
96
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useAuth.js","sourceRoot":"","sources":["../../src/hooks/useAuth.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;
|
1
|
+
{"version":3,"file":"useAuth.js","sourceRoot":"","sources":["../../src/hooks/useAuth.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAuB5C,MAAM,WAAW,GAAG,KAAK,CAAC,aAAa,CAAmB;IACxD,UAAU,EAAE,KAAK;IACjB,WAAW,EAAE,IAAI;IACjB,gBAAgB,EAAE,SAAS;IAC3B,eAAe,EAAE,KAAK;CACvB,CAAC,CAAA;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,QAAQ,EACR,aAAa,EACb,SA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AK,EAAE,KAAK,IAAG,QAAQ,CAAwB,CAAA;AAC9E,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO;IAC7B,OAAO,KAAK,CAAC,UAAU,CAAC,WAAW,CAAC,CAAA;AACtC,CAAC","sourcesContent":["import * as React from 'react'\nimport { authService } from '@oneblink/apps'\nimport { UserProfile } from '@oneblink/types/typescript/misc'\n\nexport type AuthContextValue = {\n /** `true` if the current user is logged in */\n isLoggedIn: boolean\n /**\n * See\n * [auth-service.getUserProfile()](https://oneblink.github.io/apps/modules/authService.html#getUserProfile)\n */\n userProfile: UserProfile | null\n /**\n * See\n * [auth-service.getUserFriendlyName()](https://oneblink.github.io/apps/modules/authService.html#getUserFriendlyName)\n */\n userFriendlyName: string | undefined\n /**\n * `true` if [`<AuthContextProvider />`](#AuthContextProvider) was passed the\n * `formsKeyToken` prop\n */\n isUsingFormsKey: boolean\n}\n\nconst AuthContext = React.createContext<AuthContextValue>({\n isLoggedIn: false,\n userProfile: null,\n userFriendlyName: undefined,\n isUsingFormsKey: false,\n})\n/**\n * `<AuthContextProvider />` is a React Component that provides the context for\n * the `useAuth()` hook to be used by components further down your component\n * tree. **It should only be included in your component tree once and ideally at\n * the root of the application.**\n *\n * #### Example\n *\n * ```jsx\n * import * as React from 'react'\n * import { AuthContextProvider, useAuth } from '@oneblink/apps-react'\n *\n * function Component() {\n * const auth = useAuth()\n * // use auth here\n * }\n *\n * function App() {\n * return (\n * <AuthContextProvider>\n * <Component />\n * </AuthContextProvider>\n * )\n * }\n *\n * const root = document.getElementById('root')\n * if (root) {\n * ReactDOM.render(<App />, root)\n * }\n * ```\n *\n * @param props\n * @returns\n * @group Components\n */\nexport function AuthContextProvider({\n children,\n formsKeyToken,\n userToken,\n}: {\n /** Your application components */\n children: React.ReactNode\n /**\n * A Forms Key token being used to make requests to the OneBlink API on behalf\n * of the user\n */\n formsKeyToken?: string\n /**\n * An encrypted user token that will be used included in the submission on\n * behalf of the user\n */\n userToken?: string\n}) {\n const [value, setValue] = React.useState(() => {\n authService.setFormsKeyToken(formsKeyToken)\n authService.setUserToken(userToken)\n return {\n isLoggedIn: authService.isLoggedIn(),\n userProfile: authService.getUserProfile(),\n userFriendlyName: authService.getUserFriendlyName(),\n isUsingFormsKey: !!formsKeyToken,\n }\n })\n\n React.useEffect(() => {\n authService.setFormsKeyToken(formsKeyToken)\n setValue((currentValue) => ({\n ...currentValue,\n isUsingFormsKey: !!formsKeyToken,\n }))\n }, [formsKeyToken])\n\n React.useEffect(() => {\n authService.setUserToken(userToken)\n }, [userToken])\n\n React.useEffect(() => {\n return authService.registerAuthListener(() =>\n setValue((current) => ({\n ...current,\n isLoggedIn: authService.isLoggedIn(),\n userProfile: authService.getUserProfile(),\n userFriendlyName: authService.getUserFriendlyName(),\n })),\n )\n }, [])\n\n return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>\n}\n\n/**\n * A React hook for containing state associated the current user. **This hook\n * requires [`<AuthContextProvider />`](./AuthContextProvider.html) to be\n * present in your component tree.**\n *\n * Example\n *\n * ```js\n * import { useAuth } from '@oneblink/apps-react'\n *\n * function Component() {\n * const { isLoggedIn, userProfile, userFriendlyName, isUsingFormsKey } =\n * useAuth()\n * }\n * ```\n *\n * @returns\n * @group Hooks\n */\nexport default function useAuth() {\n return React.useContext(AuthContext)\n}\n"]}
|
@@ -1,5 +1,42 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
/**
|
2
|
+
* This function is a react hook for boolean state that comes with
|
3
|
+
* `useCallback`s for 'turning on', 'turning off' and toggling the state.
|
4
|
+
*
|
5
|
+
* ## Return
|
6
|
+
*
|
7
|
+
* The return type of `useBooleanState(true)` is an array where:
|
8
|
+
*
|
9
|
+
* - The first item is a `boolean` (the state).
|
10
|
+
* - The second item is `() => void` (a function that sets the state to true).
|
11
|
+
* - The third item is `() => void` (a function that sets the state to false).
|
12
|
+
* - The fourth item is `() => void` (a function that toggles the state to the
|
13
|
+
* opposite of what it currently is).
|
14
|
+
*
|
15
|
+
* As such, the items in the array can be destructured and named whatever you
|
16
|
+
* like:
|
17
|
+
*
|
18
|
+
* ```js
|
19
|
+
* import { useBooleanState } from '@oneblink/apps-react'
|
20
|
+
*
|
21
|
+
* const [dialogIsOpen, openDialog, closeDialog, toggleDialog] =
|
22
|
+
* useBooleanState(true)
|
23
|
+
* ```
|
24
|
+
*
|
25
|
+
* These properties can then be used like:
|
26
|
+
*
|
27
|
+
* ```js
|
28
|
+
* openDialog()
|
29
|
+
* closeDialog()
|
30
|
+
* toggleDialog()
|
31
|
+
* ```
|
32
|
+
*
|
33
|
+
* @param defaultValue
|
34
|
+
* @returns
|
35
|
+
* @group Hooks
|
36
|
+
*/
|
37
|
+
export default function useBooleanState(defaultValue: boolean): [
|
38
|
+
state: boolean,
|
39
|
+
setTrue: () => void,
|
40
|
+
setFalse: () => void,
|
41
|
+
toggle: () => void
|
42
|
+
];
|
@@ -1,4 +1,40 @@
|
|
1
1
|
import * as React from 'react';
|
2
|
+
/**
|
3
|
+
* This function is a react hook for boolean state that comes with
|
4
|
+
* `useCallback`s for 'turning on', 'turning off' and toggling the state.
|
5
|
+
*
|
6
|
+
* ## Return
|
7
|
+
*
|
8
|
+
* The return type of `useBooleanState(true)` is an array where:
|
9
|
+
*
|
10
|
+
* - The first item is a `boolean` (the state).
|
11
|
+
* - The second item is `() => void` (a function that sets the state to true).
|
12
|
+
* - The third item is `() => void` (a function that sets the state to false).
|
13
|
+
* - The fourth item is `() => void` (a function that toggles the state to the
|
14
|
+
* opposite of what it currently is).
|
15
|
+
*
|
16
|
+
* As such, the items in the array can be destructured and named whatever you
|
17
|
+
* like:
|
18
|
+
*
|
19
|
+
* ```js
|
20
|
+
* import { useBooleanState } from '@oneblink/apps-react'
|
21
|
+
*
|
22
|
+
* const [dialogIsOpen, openDialog, closeDialog, toggleDialog] =
|
23
|
+
* useBooleanState(true)
|
24
|
+
* ```
|
25
|
+
*
|
26
|
+
* These properties can then be used like:
|
27
|
+
*
|
28
|
+
* ```js
|
29
|
+
* openDialog()
|
30
|
+
* closeDialog()
|
31
|
+
* toggleDialog()
|
32
|
+
* ```
|
33
|
+
*
|
34
|
+
* @param defaultValue
|
35
|
+
* @returns
|
36
|
+
* @group Hooks
|
37
|
+
*/
|
2
38
|
export default function useBooleanState(defaultValue) {
|
3
39
|
const [state, setState] = React.useState(defaultValue);
|
4
40
|
const turnOn = React.useCallback(() => setState(true), []);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useBooleanState.js","sourceRoot":"","sources":["../../src/hooks/useBooleanState.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;
|
1
|
+
{"version":3,"file":"useBooleanState.js","sourceRoot":"","sources":["../../src/hooks/useBooleanState.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,CAAC,OAAO,UAAU,eAAe,CACrC,YAAqB;IAOrB,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAA;IACtD,MAAM,MAAM,GAAG,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAA;IAC1D,MAAM,OAAO,GAAG,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAA;IAC5D,MAAM,MAAM,GAAG,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,EAAE,EAAE,CAAC,CAAA;IAC3E,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;AACzC,CAAC","sourcesContent":["import * as React from 'react'\n\n/**\n * This function is a react hook for boolean state that comes with\n * `useCallback`s for 'turning on', 'turning off' and toggling the state.\n *\n * ## Return\n *\n * The return type of `useBooleanState(true)` is an array where:\n *\n * - The first item is a `boolean` (the state).\n * - The second item is `() => void` (a function that sets the state to true).\n * - The third item is `() => void` (a function that sets the state to false).\n * - The fourth item is `() => void` (a function that toggles the state to the\n * opposite of what it currently is).\n *\n * As such, the items in the array can be destructured and named whatever you\n * like:\n *\n * ```js\n * import { useBooleanState } from '@oneblink/apps-react'\n *\n * const [dialogIsOpen, openDialog, closeDialog, toggleDialog] =\n * useBooleanState(true)\n * ```\n *\n * These properties can then be used like:\n *\n * ```js\n * openDialog()\n * closeDialog()\n * toggleDialog()\n * ```\n *\n * @param defaultValue\n * @returns\n * @group Hooks\n */\nexport default function useBooleanState(\n defaultValue: boolean,\n): [\n state: boolean,\n setTrue: () => void,\n setFalse: () => void,\n toggle: () => void,\n] {\n const [state, setState] = React.useState(defaultValue)\n const turnOn = React.useCallback(() => setState(true), [])\n const turnOff = React.useCallback(() => setState(false), [])\n const toggle = React.useCallback(() => setState((current) => !current), [])\n return [state, turnOn, turnOff, toggle]\n}\n"]}
|
@@ -1,3 +1,43 @@
|
|
1
|
+
/**
|
2
|
+
* This function is a react hook for watching for click events outside of a
|
3
|
+
* particular element. The hook will add and remove its own `eventListener`.
|
4
|
+
*
|
5
|
+
* - For performance reasons, it is important to pass a memoised function as the
|
6
|
+
* callback argument, eg:
|
7
|
+
*
|
8
|
+
* ```js
|
9
|
+
* React.useCallback(() => {}, [])
|
10
|
+
* ```
|
11
|
+
*
|
12
|
+
* ## Example
|
13
|
+
*
|
14
|
+
* ```js
|
15
|
+
* import * as React from 'react'
|
16
|
+
* import { useClickOutsideElement } from '@oneblink/apps-react'
|
17
|
+
*
|
18
|
+
* const MyComponent = () => {
|
19
|
+
* const narrowDivRef = React.useRef(null)
|
20
|
+
* useClickOutsideElement(
|
21
|
+
* narrowDivRef,
|
22
|
+
* React.useCallback(() => {
|
23
|
+
* console.log('Wide Div was clicked outside of narrow div...')
|
24
|
+
* }, []),
|
25
|
+
* )
|
26
|
+
*
|
27
|
+
* return (
|
28
|
+
* <div className="wide-div">
|
29
|
+
* <div ref={narrowDivRef} className="narrow-div"></div>
|
30
|
+
* </div>
|
31
|
+
* )
|
32
|
+
* }
|
33
|
+
*
|
34
|
+
* export default MyComponent
|
35
|
+
* ```
|
36
|
+
*
|
37
|
+
* @param ref
|
38
|
+
* @param callback
|
39
|
+
* @group Hooks
|
40
|
+
*/
|
1
41
|
export default function useClickOutsideElement(ref: {
|
2
42
|
current: HTMLElement | null;
|
3
43
|
}, callback: () => void): void;
|
@@ -1,4 +1,44 @@
|
|
1
1
|
import * as React from 'react';
|
2
|
+
/**
|
3
|
+
* This function is a react hook for watching for click events outside of a
|
4
|
+
* particular element. The hook will add and remove its own `eventListener`.
|
5
|
+
*
|
6
|
+
* - For performance reasons, it is important to pass a memoised function as the
|
7
|
+
* callback argument, eg:
|
8
|
+
*
|
9
|
+
* ```js
|
10
|
+
* React.useCallback(() => {}, [])
|
11
|
+
* ```
|
12
|
+
*
|
13
|
+
* ## Example
|
14
|
+
*
|
15
|
+
* ```js
|
16
|
+
* import * as React from 'react'
|
17
|
+
* import { useClickOutsideElement } from '@oneblink/apps-react'
|
18
|
+
*
|
19
|
+
* const MyComponent = () => {
|
20
|
+
* const narrowDivRef = React.useRef(null)
|
21
|
+
* useClickOutsideElement(
|
22
|
+
* narrowDivRef,
|
23
|
+
* React.useCallback(() => {
|
24
|
+
* console.log('Wide Div was clicked outside of narrow div...')
|
25
|
+
* }, []),
|
26
|
+
* )
|
27
|
+
*
|
28
|
+
* return (
|
29
|
+
* <div className="wide-div">
|
30
|
+
* <div ref={narrowDivRef} className="narrow-div"></div>
|
31
|
+
* </div>
|
32
|
+
* )
|
33
|
+
* }
|
34
|
+
*
|
35
|
+
* export default MyComponent
|
36
|
+
* ```
|
37
|
+
*
|
38
|
+
* @param ref
|
39
|
+
* @param callback
|
40
|
+
* @group Hooks
|
41
|
+
*/
|
2
42
|
export default function useClickOutsideElement(ref, callback) {
|
3
43
|
const handleClickOutside = React.useCallback((event) => {
|
4
44
|
if (ref.current && !ref.current.contains(event.target)) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useClickOutsideElement.js","sourceRoot":"","sources":["../../src/hooks/useClickOutsideElement.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAC5C,GAAoC,EACpC,QAAoB;IAEpB,MAAM,kBAAkB,GAAG,KAAK,CAAC,WAAW,CAC1C,CAAC,KAAY,EAAE,EAAE;QACf,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAc,CAAC,EAAE;YAC9D,QAAQ,EAAE,CAAA;SACX;IACH,CAAC,EACD,CAAC,QAAQ,EAAE,GAAG,CAAC,CAChB,CAAA;IACD,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,QAAQ,CAAC,gBAAgB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAA;QAC1D,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,mBAAmB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAA;QAC/D,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,kBAAkB,CAAC,CAAC,CAAA;AAC1B,CAAC","sourcesContent":["import * as React from 'react'\n\nexport default function useClickOutsideElement(\n ref: { current: HTMLElement | null },\n callback: () => void,\n) {\n const handleClickOutside = React.useCallback(\n (event: Event) => {\n if (ref.current && !ref.current.contains(event.target as Node)) {\n callback()\n }\n },\n [callback, ref],\n )\n React.useEffect(() => {\n document.addEventListener('mousedown', handleClickOutside)\n return () => {\n document.removeEventListener('mousedown', handleClickOutside)\n }\n }, [handleClickOutside])\n}\n"]}
|
1
|
+
{"version":3,"file":"useClickOutsideElement.js","sourceRoot":"","sources":["../../src/hooks/useClickOutsideElement.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAC5C,GAAoC,EACpC,QAAoB;IAEpB,MAAM,kBAAkB,GAAG,KAAK,CAAC,WAAW,CAC1C,CAAC,KAAY,EAAE,EAAE;QACf,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAc,CAAC,EAAE;YAC9D,QAAQ,EAAE,CAAA;SACX;IACH,CAAC,EACD,CAAC,QAAQ,EAAE,GAAG,CAAC,CAChB,CAAA;IACD,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,QAAQ,CAAC,gBAAgB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAA;QAC1D,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,mBAAmB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAA;QAC/D,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,kBAAkB,CAAC,CAAC,CAAA;AAC1B,CAAC","sourcesContent":["import * as React from 'react'\n\n/**\n * This function is a react hook for watching for click events outside of a\n * particular element. The hook will add and remove its own `eventListener`.\n *\n * - For performance reasons, it is important to pass a memoised function as the\n * callback argument, eg:\n *\n * ```js\n * React.useCallback(() => {}, [])\n * ```\n *\n * ## Example\n *\n * ```js\n * import * as React from 'react'\n * import { useClickOutsideElement } from '@oneblink/apps-react'\n *\n * const MyComponent = () => {\n * const narrowDivRef = React.useRef(null)\n * useClickOutsideElement(\n * narrowDivRef,\n * React.useCallback(() => {\n * console.log('Wide Div was clicked outside of narrow div...')\n * }, []),\n * )\n *\n * return (\n * <div className=\"wide-div\">\n * <div ref={narrowDivRef} className=\"narrow-div\"></div>\n * </div>\n * )\n * }\n *\n * export default MyComponent\n * ```\n *\n * @param ref\n * @param callback\n * @group Hooks\n */\nexport default function useClickOutsideElement(\n ref: { current: HTMLElement | null },\n callback: () => void,\n) {\n const handleClickOutside = React.useCallback(\n (event: Event) => {\n if (ref.current && !ref.current.contains(event.target as Node)) {\n callback()\n }\n },\n [callback, ref],\n )\n React.useEffect(() => {\n document.addEventListener('mousedown', handleClickOutside)\n return () => {\n document.removeEventListener('mousedown', handleClickOutside)\n }\n }, [handleClickOutside])\n}\n"]}
|
@@ -1,7 +1,6 @@
|
|
1
|
-
import { FormTypes } from '@oneblink/types';
|
1
|
+
import { FormTypes, SubmissionTypes } from '@oneblink/types';
|
2
2
|
import { conditionalLogicService } from '@oneblink/sdk-core';
|
3
|
-
|
4
|
-
export default function useConditionalLogic(definition: FormTypes.Form, submission: FormSubmissionModel): {
|
3
|
+
export default function useConditionalLogic(definition: FormTypes.Form, submission: SubmissionTypes.S3SubmissionData['submission']): {
|
5
4
|
conditionalLogicError: Error | undefined;
|
6
5
|
formElementsConditionallyShown: conditionalLogicService.FormElementsConditionallyShown;
|
7
6
|
};
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useConditionalLogic.js","sourceRoot":"","sources":["../../src/hooks/useConditionalLogic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAEvC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAA;
|
1
|
+
{"version":3,"file":"useConditionalLogic.js","sourceRoot":"","sources":["../../src/hooks/useConditionalLogic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAEvC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAA;AAI5D,MAAM,CAAC,OAAO,UAAU,mBAAmB,CACzC,UAA0B,EAC1B,UAA0D;IAE1D,MAAM,CAAC,qBAAqB,EAAE,wBAAwB,CAAC,GAAG,KAAK,CAAC,QAAQ,EAErE,CAAA;IAEH,MAAM,aAAa,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC,KAAY,EAAE,EAAE;QACvD,OAAO,CAAC,IAAI,CAAC,wCAAwC,EAAE,KAAK,CAAC,CAAA;QAC7D,MAAM,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAA;QAC9B,wBAAwB,CAAC,KAAK,CAAC,CAAA;IACjC,CAAC,EAAE,EAAE,CAAC,CAAA;IAEN,MAAM,8BAA8B,GAClC,KAAK,CAAC,OAAO,CAAiC,GAAG,EAAE;QACjD,OAAO,uBAAuB,CAAC,sCAAsC,CAAC;YACpE,YAAY,EAAE,UAAU,CAAC,QAAQ;YACjC,UAAU;YACV,aAAa;SACd,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,UAAU,EAAE,aAAa,CAAC,CAAC,CAAA;IAEtD,OAAO;QACL,qBAAqB;QACrB,8BAA8B;KAC/B,CAAA;AACH,CAAC","sourcesContent":["import { Sentry } from '@oneblink/apps'\nimport { FormTypes, SubmissionTypes } from '@oneblink/types'\nimport * as React from 'react'\nimport { conditionalLogicService } from '@oneblink/sdk-core'\n\nimport { FormElementsConditionallyShown } from '../types/form'\n\nexport default function useConditionalLogic(\n definition: FormTypes.Form,\n submission: SubmissionTypes.S3SubmissionData['submission'],\n) {\n const [conditionalLogicError, setConditionalLogicError] = React.useState<\n Error | undefined\n >()\n\n const errorCallback = React.useCallback((error: Error) => {\n console.warn('Error while checking conditional logic', error)\n Sentry.captureException(error)\n setConditionalLogicError(error)\n }, [])\n\n const formElementsConditionallyShown =\n React.useMemo<FormElementsConditionallyShown>(() => {\n return conditionalLogicService.generateFormElementsConditionallyShown({\n formElements: definition.elements,\n submission,\n errorCallback,\n })\n }, [definition.elements, submission, errorCallback])\n\n return {\n conditionalLogicError,\n formElementsConditionallyShown,\n }\n}\n"]}
|
@@ -0,0 +1,82 @@
|
|
1
|
+
import * as React from 'react';
|
2
|
+
import { SubmissionTypes } from '@oneblink/types';
|
3
|
+
/** The value returned from `useDrafts()` hook */
|
4
|
+
export type DraftsContextValue = {
|
5
|
+
/** `true` drafts are currently loading. */
|
6
|
+
isLoading: boolean;
|
7
|
+
/** An Error object if loading drafts fails */
|
8
|
+
loadError: Error | null;
|
9
|
+
/** The incomplete submissions that were saved for later */
|
10
|
+
drafts: SubmissionTypes.FormsAppDraft[];
|
11
|
+
/** A function to trigger loading of the drafts */
|
12
|
+
reloadDrafts: () => unknown;
|
13
|
+
/** A function to clear Error object from loading drafts */
|
14
|
+
clearLoadError: () => void;
|
15
|
+
/** `true` drafts are syncing with other devices */
|
16
|
+
isSyncing: boolean;
|
17
|
+
/** A function to trigger syncing of the drafts */
|
18
|
+
syncDrafts: () => unknown;
|
19
|
+
/** An Error object if syncing drafts fails */
|
20
|
+
syncError: Error | null;
|
21
|
+
/** A function to clear Error object from syncing drafts */
|
22
|
+
clearSyncError: () => void;
|
23
|
+
/** A function to remove a draft */
|
24
|
+
deleteDraft: (draftId: string) => Promise<void>;
|
25
|
+
};
|
26
|
+
/**
|
27
|
+
* React Component that provides the context for the `useDrafts()` hook to be
|
28
|
+
* used by components further down your component tree. **It should only be
|
29
|
+
* included in your component tree once and ideally at the root of the
|
30
|
+
* application.**
|
31
|
+
*
|
32
|
+
* #### Example
|
33
|
+
*
|
34
|
+
* ```jsx
|
35
|
+
* import * as React from 'react'
|
36
|
+
* import { DraftsContextProvider, useDrafts } from '@oneblink/apps-react'
|
37
|
+
*
|
38
|
+
* function Component() {
|
39
|
+
* const draftsContext = useDrafts()
|
40
|
+
* // use drafts here
|
41
|
+
* }
|
42
|
+
*
|
43
|
+
* function App() {
|
44
|
+
* return (
|
45
|
+
* <DraftsContextProvider>
|
46
|
+
* <Component />
|
47
|
+
* </DraftsContextProvider>
|
48
|
+
* )
|
49
|
+
* }
|
50
|
+
*
|
51
|
+
* const root = document.getElementById('root')
|
52
|
+
* if (root) {
|
53
|
+
* ReactDOM.render(<App />, root)
|
54
|
+
* }
|
55
|
+
* ```
|
56
|
+
*
|
57
|
+
* @param props
|
58
|
+
* @returns
|
59
|
+
* @group Components
|
60
|
+
*/
|
61
|
+
export declare function DraftsContextProvider({
|
62
|
+
/** The identifier for the forms app associated with the user's drafts */
|
63
|
+
formsAppId,
|
64
|
+
/**
|
65
|
+
* `true` if drafts are enabled, otherwise `false`. Can be used for account
|
66
|
+
* tier validation.
|
67
|
+
*/
|
68
|
+
isDraftsEnabled,
|
69
|
+
/** Your application components */
|
70
|
+
children, }: {
|
71
|
+
formsAppId: number;
|
72
|
+
isDraftsEnabled: boolean;
|
73
|
+
children: React.ReactNode;
|
74
|
+
}): JSX.Element;
|
75
|
+
/**
|
76
|
+
* React hook to get the context value for Drafts. Will throw an Error if used
|
77
|
+
* outside of the `<DraftsContextProvider />` component.
|
78
|
+
*
|
79
|
+
* @returns
|
80
|
+
* @group Hooks
|
81
|
+
*/
|
82
|
+
export default function useDrafts(): DraftsContextValue;
|
@@ -0,0 +1,204 @@
|
|
1
|
+
import * as React from 'react';
|
2
|
+
import { draftService, submissionService } from '@oneblink/apps';
|
3
|
+
import useAuth from './useAuth';
|
4
|
+
import useIsMounted from './useIsMounted';
|
5
|
+
import useIsOffline from './useIsOffline';
|
6
|
+
const defaultLoadState = {
|
7
|
+
isLoading: false,
|
8
|
+
loadError: null,
|
9
|
+
drafts: [],
|
10
|
+
};
|
11
|
+
const defaultSyncState = {
|
12
|
+
isSyncing: false,
|
13
|
+
syncError: null,
|
14
|
+
};
|
15
|
+
const DraftsContext = React.createContext(undefined);
|
16
|
+
/**
|
17
|
+
* React Component that provides the context for the `useDrafts()` hook to be
|
18
|
+
* used by components further down your component tree. **It should only be
|
19
|
+
* included in your component tree once and ideally at the root of the
|
20
|
+
* application.**
|
21
|
+
*
|
22
|
+
* #### Example
|
23
|
+
*
|
24
|
+
* ```jsx
|
25
|
+
* import * as React from 'react'
|
26
|
+
* import { DraftsContextProvider, useDrafts } from '@oneblink/apps-react'
|
27
|
+
*
|
28
|
+
* function Component() {
|
29
|
+
* const draftsContext = useDrafts()
|
30
|
+
* // use drafts here
|
31
|
+
* }
|
32
|
+
*
|
33
|
+
* function App() {
|
34
|
+
* return (
|
35
|
+
* <DraftsContextProvider>
|
36
|
+
* <Component />
|
37
|
+
* </DraftsContextProvider>
|
38
|
+
* )
|
39
|
+
* }
|
40
|
+
*
|
41
|
+
* const root = document.getElementById('root')
|
42
|
+
* if (root) {
|
43
|
+
* ReactDOM.render(<App />, root)
|
44
|
+
* }
|
45
|
+
* ```
|
46
|
+
*
|
47
|
+
* @param props
|
48
|
+
* @returns
|
49
|
+
* @group Components
|
50
|
+
*/
|
51
|
+
export function DraftsContextProvider({
|
52
|
+
/** The identifier for the forms app associated with the user's drafts */
|
53
|
+
formsAppId,
|
54
|
+
/**
|
55
|
+
* `true` if drafts are enabled, otherwise `false`. Can be used for account
|
56
|
+
* tier validation.
|
57
|
+
*/
|
58
|
+
isDraftsEnabled,
|
59
|
+
/** Your application components */
|
60
|
+
children, }) {
|
61
|
+
const isMounted = useIsMounted();
|
62
|
+
const isOffline = useIsOffline();
|
63
|
+
const { isLoggedIn, isUsingFormsKey } = useAuth();
|
64
|
+
const [syncState, setSyncState] = React.useState(defaultSyncState);
|
65
|
+
const clearSyncError = React.useCallback(() => {
|
66
|
+
setSyncState((currentState) => ({
|
67
|
+
...currentState,
|
68
|
+
syncError: null,
|
69
|
+
}));
|
70
|
+
}, []);
|
71
|
+
const syncDrafts = React.useCallback(async () => {
|
72
|
+
if (!isDraftsEnabled || !isLoggedIn || isUsingFormsKey) {
|
73
|
+
return;
|
74
|
+
}
|
75
|
+
if (isMounted.current) {
|
76
|
+
setSyncState({
|
77
|
+
isSyncing: true,
|
78
|
+
syncError: null,
|
79
|
+
});
|
80
|
+
}
|
81
|
+
let newError = null;
|
82
|
+
try {
|
83
|
+
await draftService.syncDrafts({
|
84
|
+
formsAppId,
|
85
|
+
throwError: false,
|
86
|
+
});
|
87
|
+
}
|
88
|
+
catch (error) {
|
89
|
+
newError = error;
|
90
|
+
}
|
91
|
+
if (isMounted.current) {
|
92
|
+
setSyncState({
|
93
|
+
isSyncing: false,
|
94
|
+
syncError: newError,
|
95
|
+
});
|
96
|
+
}
|
97
|
+
}, [formsAppId, isDraftsEnabled, isLoggedIn, isMounted, isUsingFormsKey]);
|
98
|
+
const [loadState, setLoadState] = React.useState(defaultLoadState);
|
99
|
+
const clearLoadError = React.useCallback(() => {
|
100
|
+
setLoadState((currentState) => ({
|
101
|
+
...currentState,
|
102
|
+
loadError: null,
|
103
|
+
}));
|
104
|
+
}, []);
|
105
|
+
const reloadDrafts = React.useCallback(async () => {
|
106
|
+
if (!isLoggedIn) {
|
107
|
+
if (isMounted.current) {
|
108
|
+
setLoadState({
|
109
|
+
isLoading: false,
|
110
|
+
loadError: null,
|
111
|
+
drafts: [],
|
112
|
+
});
|
113
|
+
}
|
114
|
+
return;
|
115
|
+
}
|
116
|
+
if (isMounted.current) {
|
117
|
+
setLoadState((currentState) => ({
|
118
|
+
isLoading: true,
|
119
|
+
loadError: null,
|
120
|
+
drafts: currentState.drafts,
|
121
|
+
}));
|
122
|
+
}
|
123
|
+
let newError = null;
|
124
|
+
let newDrafts = [];
|
125
|
+
try {
|
126
|
+
newDrafts = await draftService.getDrafts();
|
127
|
+
}
|
128
|
+
catch (error) {
|
129
|
+
newError = error;
|
130
|
+
}
|
131
|
+
if (isMounted.current) {
|
132
|
+
setLoadState({
|
133
|
+
isLoading: false,
|
134
|
+
loadError: newError,
|
135
|
+
drafts: newDrafts,
|
136
|
+
});
|
137
|
+
}
|
138
|
+
}, [isMounted, isLoggedIn]);
|
139
|
+
const deleteDraft = React.useCallback((draftId) => {
|
140
|
+
return draftService.deleteDraft(draftId, formsAppId);
|
141
|
+
}, [formsAppId]);
|
142
|
+
const value = React.useMemo(() => ({
|
143
|
+
// Sync
|
144
|
+
syncDrafts,
|
145
|
+
isSyncing: syncState.isSyncing,
|
146
|
+
syncError: syncState.syncError,
|
147
|
+
clearSyncError,
|
148
|
+
// Load
|
149
|
+
reloadDrafts,
|
150
|
+
isLoading: loadState.isLoading,
|
151
|
+
drafts: loadState.drafts,
|
152
|
+
loadError: loadState.loadError,
|
153
|
+
clearLoadError,
|
154
|
+
// Delete,
|
155
|
+
deleteDraft,
|
156
|
+
}), [
|
157
|
+
clearLoadError,
|
158
|
+
clearSyncError,
|
159
|
+
loadState.drafts,
|
160
|
+
loadState.isLoading,
|
161
|
+
loadState.loadError,
|
162
|
+
reloadDrafts,
|
163
|
+
syncDrafts,
|
164
|
+
syncState.isSyncing,
|
165
|
+
syncState.syncError,
|
166
|
+
deleteDraft,
|
167
|
+
]);
|
168
|
+
React.useEffect(() => {
|
169
|
+
reloadDrafts();
|
170
|
+
const unregisterPendingQueueListener = submissionService.registerPendingQueueListener(reloadDrafts);
|
171
|
+
const unregisterDraftsListener = draftService.registerDraftsListener((drafts) => {
|
172
|
+
setLoadState({
|
173
|
+
isLoading: false,
|
174
|
+
drafts,
|
175
|
+
loadError: null,
|
176
|
+
});
|
177
|
+
});
|
178
|
+
return () => {
|
179
|
+
unregisterPendingQueueListener();
|
180
|
+
unregisterDraftsListener();
|
181
|
+
};
|
182
|
+
}, [reloadDrafts]);
|
183
|
+
React.useEffect(() => {
|
184
|
+
if (!isOffline) {
|
185
|
+
syncDrafts();
|
186
|
+
}
|
187
|
+
}, [isOffline, syncDrafts]);
|
188
|
+
return (React.createElement(DraftsContext.Provider, { value: value }, children));
|
189
|
+
}
|
190
|
+
/**
|
191
|
+
* React hook to get the context value for Drafts. Will throw an Error if used
|
192
|
+
* outside of the `<DraftsContextProvider />` component.
|
193
|
+
*
|
194
|
+
* @returns
|
195
|
+
* @group Hooks
|
196
|
+
*/
|
197
|
+
export default function useDrafts() {
|
198
|
+
const value = React.useContext(DraftsContext);
|
199
|
+
if (!value) {
|
200
|
+
throw new Error(`"useDrafts" hook was used outside of the "<DraftsContextProvider />" component's children.`);
|
201
|
+
}
|
202
|
+
return value;
|
203
|
+
}
|
204
|
+
//# sourceMappingURL=useDrafts.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useDrafts.js","sourceRoot":"","sources":["../../src/hooks/useDrafts.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAEhE,OAAO,OAAO,MAAM,WAAW,CAAA;AAC/B,OAAO,YAAY,MAAM,gBAAgB,CAAA;AACzC,OAAO,YAAY,MAAM,gBAAgB,CAAA;AA0BzC,MAAM,gBAAgB,GAAG;IACvB,SAAS,EAAE,KAAK;IAChB,SAAS,EAAE,IAAI;IACf,MAAM,EAAE,EAAE;CACX,CAAA;AAED,MAAM,gBAAgB,GAAG;IACvB,SAAS,EAAE,KAAK;IAChB,SAAS,EAAE,IAAI;CAChB,CAAA;AAED,MAAM,aAAa,GAAG,KAAK,CAAC,aAAa,CACvC,SAAS,CACV,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,UAAU,qBAAqB,CAAC;AACpC,yEAAyE;AACzE,UAAU;AACV;;;GAGG;AACH,eAAe;AACf,kCAAkC;AAClC,QAAQ,GAKT;IACC,MAAM,SAAS,GAAG,YAAY,EAAE,CAAA;IAChC,MAAM,SAAS,GAAG,YAAY,EAAE,CAAA;IAChC,MAAM,EAAE,UAAU,EAAE,eAAe,EAAE,GAAG,OAAO,EAAE,CAAA;IAEjD,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,KAAK,CAAC,QAAQ,CAG7C,gBAAgB,CAAC,CAAA;IACpB,MAAM,cAAc,GAAG,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE;QAC5C,YAA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K,CACb,4FAA4F,CAC7F,CAAA;KACF;IACD,OAAO,KAAK,CAAA;AACd,CAAC","sourcesContent":["import * as React from 'react'\nimport { draftService, submissionService } from '@oneblink/apps'\nimport { SubmissionTypes } from '@oneblink/types'\nimport useAuth from './useAuth'\nimport useIsMounted from './useIsMounted'\nimport useIsOffline from './useIsOffline'\n\n/** The value returned from `useDrafts()` hook */\nexport type DraftsContextValue = {\n /** `true` drafts are currently loading. */\n isLoading: boolean\n /** An Error object if loading drafts fails */\n loadError: Error | null\n /** The incomplete submissions that were saved for later */\n drafts: SubmissionTypes.FormsAppDraft[]\n /** A function to trigger loading of the drafts */\n reloadDrafts: () => unknown\n /** A function to clear Error object from loading drafts */\n clearLoadError: () => void\n /** `true` drafts are syncing with other devices */\n isSyncing: boolean\n /** A function to trigger syncing of the drafts */\n syncDrafts: () => unknown\n /** An Error object if syncing drafts fails */\n syncError: Error | null\n /** A function to clear Error object from syncing drafts */\n clearSyncError: () => void\n /** A function to remove a draft */\n deleteDraft: (draftId: string) => Promise<void>\n}\n\nconst defaultLoadState = {\n isLoading: false,\n loadError: null,\n drafts: [],\n}\n\nconst defaultSyncState = {\n isSyncing: false,\n syncError: null,\n}\n\nconst DraftsContext = React.createContext<DraftsContextValue | undefined>(\n undefined,\n)\n\n/**\n * React Component that provides the context for the `useDrafts()` hook to be\n * used by components further down your component tree. **It should only be\n * included in your component tree once and ideally at the root of the\n * application.**\n *\n * #### Example\n *\n * ```jsx\n * import * as React from 'react'\n * import { DraftsContextProvider, useDrafts } from '@oneblink/apps-react'\n *\n * function Component() {\n * const draftsContext = useDrafts()\n * // use drafts here\n * }\n *\n * function App() {\n * return (\n * <DraftsContextProvider>\n * <Component />\n * </DraftsContextProvider>\n * )\n * }\n *\n * const root = document.getElementById('root')\n * if (root) {\n * ReactDOM.render(<App />, root)\n * }\n * ```\n *\n * @param props\n * @returns\n * @group Components\n */\nexport function DraftsContextProvider({\n /** The identifier for the forms app associated with the user's drafts */\n formsAppId,\n /**\n * `true` if drafts are enabled, otherwise `false`. Can be used for account\n * tier validation.\n */\n isDraftsEnabled,\n /** Your application components */\n children,\n}: {\n formsAppId: number\n isDraftsEnabled: boolean\n children: React.ReactNode\n}) {\n const isMounted = useIsMounted()\n const isOffline = useIsOffline()\n const { isLoggedIn, isUsingFormsKey } = useAuth()\n\n const [syncState, setSyncState] = React.useState<{\n isSyncing: boolean\n syncError: Error | null\n }>(defaultSyncState)\n const clearSyncError = React.useCallback(() => {\n setSyncState((currentState) => ({\n ...currentState,\n syncError: null,\n }))\n }, [])\n const syncDrafts = React.useCallback(async () => {\n if (!isDraftsEnabled || !isLoggedIn || isUsingFormsKey) {\n return\n }\n\n if (isMounted.current) {\n setSyncState({\n isSyncing: true,\n syncError: null,\n })\n }\n\n let newError = null\n\n try {\n await draftService.syncDrafts({\n formsAppId,\n throwError: false,\n })\n } catch (error) {\n newError = error as Error\n }\n\n if (isMounted.current) {\n setSyncState({\n isSyncing: false,\n syncError: newError,\n })\n }\n }, [formsAppId, isDraftsEnabled, isLoggedIn, isMounted, isUsingFormsKey])\n\n const [loadState, setLoadState] = React.useState<{\n isLoading: boolean\n loadError: Error | null\n drafts: SubmissionTypes.FormsAppDraft[]\n }>(defaultLoadState)\n const clearLoadError = React.useCallback(() => {\n setLoadState((currentState) => ({\n ...currentState,\n loadError: null,\n }))\n }, [])\n const reloadDrafts = React.useCallback(async () => {\n if (!isLoggedIn) {\n if (isMounted.current) {\n setLoadState({\n isLoading: false,\n loadError: null,\n drafts: [],\n })\n }\n return\n }\n\n if (isMounted.current) {\n setLoadState((currentState) => ({\n isLoading: true,\n loadError: null,\n drafts: currentState.drafts,\n }))\n }\n\n let newError = null\n let newDrafts: SubmissionTypes.FormsAppDraft[] = []\n\n try {\n newDrafts = await draftService.getDrafts()\n } catch (error) {\n newError = error as Error\n }\n\n if (isMounted.current) {\n setLoadState({\n isLoading: false,\n loadError: newError,\n drafts: newDrafts,\n })\n }\n }, [isMounted, isLoggedIn])\n\n const deleteDraft = React.useCallback(\n (draftId) => {\n return draftService.deleteDraft(draftId, formsAppId)\n },\n [formsAppId],\n )\n\n const value = React.useMemo(\n () => ({\n // Sync\n syncDrafts,\n isSyncing: syncState.isSyncing,\n syncError: syncState.syncError,\n clearSyncError,\n // Load\n reloadDrafts,\n isLoading: loadState.isLoading,\n drafts: loadState.drafts,\n loadError: loadState.loadError,\n clearLoadError,\n // Delete,\n deleteDraft,\n }),\n [\n clearLoadError,\n clearSyncError,\n loadState.drafts,\n loadState.isLoading,\n loadState.loadError,\n reloadDrafts,\n syncDrafts,\n syncState.isSyncing,\n syncState.syncError,\n deleteDraft,\n ],\n )\n\n React.useEffect(() => {\n reloadDrafts()\n const unregisterPendingQueueListener =\n submissionService.registerPendingQueueListener(reloadDrafts)\n const unregisterDraftsListener = draftService.registerDraftsListener(\n (drafts) => {\n setLoadState({\n isLoading: false,\n drafts,\n loadError: null,\n })\n },\n )\n return () => {\n unregisterPendingQueueListener()\n unregisterDraftsListener()\n }\n }, [reloadDrafts])\n\n React.useEffect(() => {\n if (!isOffline) {\n syncDrafts()\n }\n }, [isOffline, syncDrafts])\n\n return (\n <DraftsContext.Provider value={value}>{children}</DraftsContext.Provider>\n )\n}\n\n/**\n * React hook to get the context value for Drafts. Will throw an Error if used\n * outside of the `<DraftsContextProvider />` component.\n *\n * @returns\n * @group Hooks\n */\nexport default function useDrafts(): DraftsContextValue {\n const value = React.useContext(DraftsContext)\n if (!value) {\n throw new Error(\n `\"useDrafts\" hook was used outside of the \"<DraftsContextProvider />\" component's children.`,\n )\n }\n return value\n}\n"]}
|