@oneblink/apps-react 4.0.0-beta.4 → 4.0.0-beta.6

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.
Files changed (107) hide show
  1. package/README.md +2 -8
  2. package/dist/OneBlinkAutoSaveForm.d.ts +24 -5
  3. package/dist/OneBlinkAutoSaveForm.js +11 -0
  4. package/dist/OneBlinkAutoSaveForm.js.map +1 -1
  5. package/dist/OneBlinkForm.d.ts +556 -8
  6. package/dist/OneBlinkForm.js +544 -0
  7. package/dist/OneBlinkForm.js.map +1 -1
  8. package/dist/OneBlinkFormBase.d.ts +51 -6
  9. package/dist/OneBlinkFormBase.js.map +1 -1
  10. package/dist/OneBlinkReadOnlyForm.d.ts +82 -6
  11. package/dist/OneBlinkReadOnlyForm.js +78 -0
  12. package/dist/OneBlinkReadOnlyForm.js.map +1 -1
  13. package/dist/PaymentReceipt.d.ts +55 -0
  14. package/dist/PaymentReceipt.js +45 -0
  15. package/dist/PaymentReceipt.js.map +1 -1
  16. package/dist/components/formStore/FormStoreTableProvider.js +23 -1
  17. package/dist/components/formStore/FormStoreTableProvider.js.map +1 -1
  18. package/dist/components/renderer/LookupNotification.js +11 -2
  19. package/dist/components/renderer/LookupNotification.js.map +1 -1
  20. package/dist/components/renderer/OneBlinkFormElements.d.ts +3 -3
  21. package/dist/components/renderer/OneBlinkFormElements.js.map +1 -1
  22. package/dist/components/renderer/PageFormElements.d.ts +3 -3
  23. package/dist/components/renderer/PageFormElements.js.map +1 -1
  24. package/dist/components/renderer/ProgressBar.d.ts +2 -3
  25. package/dist/components/renderer/ProgressBar.js.map +1 -1
  26. package/dist/form-elements/FormElementCalculation.js.map +1 -1
  27. package/dist/form-elements/FormElementForm.d.ts +4 -4
  28. package/dist/form-elements/FormElementForm.js.map +1 -1
  29. package/dist/form-elements/FormElementRepeatableSet.d.ts +4 -4
  30. package/dist/form-elements/FormElementRepeatableSet.js.map +1 -1
  31. package/dist/form-elements/FormElementSummary.js.map +1 -1
  32. package/dist/hooks/useAuth.d.ts +79 -6
  33. package/dist/hooks/useAuth.js +52 -0
  34. package/dist/hooks/useAuth.js.map +1 -1
  35. package/dist/hooks/useBooleanState.d.ts +41 -5
  36. package/dist/hooks/useBooleanState.js +35 -0
  37. package/dist/hooks/useBooleanState.js.map +1 -1
  38. package/dist/hooks/useClickOutsideElement.d.ts +39 -0
  39. package/dist/hooks/useClickOutsideElement.js +39 -0
  40. package/dist/hooks/useClickOutsideElement.js.map +1 -1
  41. package/dist/hooks/useConditionalLogic.d.ts +2 -3
  42. package/dist/hooks/useConditionalLogic.js.map +1 -1
  43. package/dist/hooks/useDrafts.d.ts +80 -0
  44. package/dist/hooks/useDrafts.js +202 -0
  45. package/dist/hooks/useDrafts.js.map +1 -0
  46. package/dist/hooks/useFormSubmissionAutoSaveState.d.ts +17 -5
  47. package/dist/hooks/useFormSubmissionAutoSaveState.js +9 -0
  48. package/dist/hooks/useFormSubmissionAutoSaveState.js.map +1 -1
  49. package/dist/hooks/useFormSubmissionModelContext.d.ts +4 -4
  50. package/dist/hooks/useFormSubmissionModelContext.js.map +1 -1
  51. package/dist/hooks/useFormSubmissionState.d.ts +44 -5
  52. package/dist/hooks/useFormSubmissionState.js +36 -0
  53. package/dist/hooks/useFormSubmissionState.js.map +1 -1
  54. package/dist/hooks/useFormValidation.d.ts +3 -3
  55. package/dist/hooks/useFormValidation.js.map +1 -1
  56. package/dist/hooks/useInjectPages.d.ts +2 -3
  57. package/dist/hooks/useInjectPages.js.map +1 -1
  58. package/dist/hooks/useIsMounted.d.ts +19 -0
  59. package/dist/hooks/useIsMounted.js +18 -0
  60. package/dist/hooks/useIsMounted.js.map +1 -1
  61. package/dist/hooks/useIsOffline.d.ts +49 -0
  62. package/dist/hooks/useIsOffline.js +49 -0
  63. package/dist/hooks/useIsOffline.js.map +1 -1
  64. package/dist/hooks/useLoadDataState.d.ts +44 -3
  65. package/dist/hooks/useLoadDataState.js +39 -0
  66. package/dist/hooks/useLoadDataState.js.map +1 -1
  67. package/dist/hooks/useLogin.d.ts +396 -11
  68. package/dist/hooks/useLogin.js +299 -0
  69. package/dist/hooks/useLogin.js.map +1 -1
  70. package/dist/hooks/useLookups.d.ts +3 -3
  71. package/dist/hooks/useLookups.js.map +1 -1
  72. package/dist/hooks/useNullableState.d.ts +44 -1
  73. package/dist/hooks/useNullableState.js +40 -1
  74. package/dist/hooks/useNullableState.js.map +1 -1
  75. package/dist/hooks/usePendingSubmissions.d.ts +95 -0
  76. package/dist/hooks/usePendingSubmissions.js +192 -0
  77. package/dist/hooks/usePendingSubmissions.js.map +1 -0
  78. package/dist/index.d.ts +6 -3
  79. package/dist/index.js +5 -2
  80. package/dist/index.js.map +1 -1
  81. package/dist/services/checkBsbsAreInvalid.d.ts +2 -3
  82. package/dist/services/checkBsbsAreInvalid.js.map +1 -1
  83. package/dist/services/checkIfAttachmentsExist.d.ts +2 -3
  84. package/dist/services/checkIfAttachmentsExist.js.map +1 -1
  85. package/dist/services/checkIfBsbsAreValidating.d.ts +2 -3
  86. package/dist/services/checkIfBsbsAreValidating.js.map +1 -1
  87. package/dist/services/cleanFormSubmissionModel.d.ts +4 -4
  88. package/dist/services/cleanFormSubmissionModel.js +2 -1
  89. package/dist/services/cleanFormSubmissionModel.js.map +1 -1
  90. package/dist/services/form-validation.d.ts +3 -3
  91. package/dist/services/form-validation.js.map +1 -1
  92. package/dist/services/generate-default-data.d.ts +2 -3
  93. package/dist/services/generate-default-data.js +3 -1
  94. package/dist/services/generate-default-data.js.map +1 -1
  95. package/dist/services/generateFreshdeskDependentFieldElements.js.map +1 -1
  96. package/dist/services/getDateRangeConfiguration.d.ts +2 -3
  97. package/dist/services/getDateRangeConfiguration.js.map +1 -1
  98. package/dist/services/getRepeatableSetEntriesConfiguration.d.ts +2 -3
  99. package/dist/services/getRepeatableSetEntriesConfiguration.js.map +1 -1
  100. package/dist/styles/receipt.scss +1 -1
  101. package/dist/styles/repeatable-set.scss +2 -2
  102. package/dist/typedoc.d.ts +2 -0
  103. package/dist/typedoc.js +3 -0
  104. package/dist/typedoc.js.map +1 -0
  105. package/dist/types/form.d.ts +5 -6
  106. package/dist/types/form.js.map +1 -1
  107. package/package.json +9 -3
@@ -1,4 +1,39 @@
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
+ */
2
37
  export default function useBooleanState(defaultValue) {
3
38
  const [state, setState] = React.useState(defaultValue);
4
39
  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;AAM9B,MAAM,CAAC,OAAO,UAAU,eAAe,CACrC,YAAqB;IAErB,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\ntype setTrue = () => void\ntype setFalse = () => void\ntype toggle = () => void\n\nexport default function useBooleanState(\n defaultValue: boolean,\n): [boolean, setTrue, setFalse, toggle] {\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
+ {"version":3,"file":"useBooleanState.js","sourceRoot":"","sources":["../../src/hooks/useBooleanState.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;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 */\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,42 @@
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
+ */
1
40
  export default function useClickOutsideElement(ref: {
2
41
  current: HTMLElement | null;
3
42
  }, callback: () => void): void;
@@ -1,4 +1,43 @@
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
+ */
2
41
  export default function useClickOutsideElement(ref, callback) {
3
42
  const handleClickOutside = React.useCallback((event) => {
4
43
  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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;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 */\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
- import { FormSubmissionModel } from '../types/form';
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;AAO5D,MAAM,CAAC,OAAO,UAAU,mBAAmB,CACzC,UAA0B,EAC1B,UAA+B;IAE/B,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 } from '@oneblink/types'\nimport * as React from 'react'\nimport { conditionalLogicService } from '@oneblink/sdk-core'\n\nimport {\n FormElementsConditionallyShown,\n FormSubmissionModel,\n} from '../types/form'\n\nexport default function useConditionalLogic(\n definition: FormTypes.Form,\n submission: FormSubmissionModel,\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"]}
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,80 @@
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 } 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
+ */
60
+ export declare function DraftsContextProvider({
61
+ /** The identifier for the forms app associated with the user's drafts */
62
+ formsAppId,
63
+ /**
64
+ * `true` if drafts are enabled, otherwise `false`. Can be used for account
65
+ * tier validation.
66
+ */
67
+ isDraftsEnabled,
68
+ /** Your application components */
69
+ children, }: {
70
+ formsAppId: number;
71
+ isDraftsEnabled: boolean;
72
+ children: React.ReactNode;
73
+ }): JSX.Element;
74
+ /**
75
+ * React hook to get the context value for Drafts. Will throw an Error if used
76
+ * outside of the `<DraftsContextProvider />` component.
77
+ *
78
+ * @returns
79
+ */
80
+ export default function useDrafts(): DraftsContextValue;
@@ -0,0 +1,202 @@
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 } 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
+ */
50
+ export function DraftsContextProvider({
51
+ /** The identifier for the forms app associated with the user's drafts */
52
+ formsAppId,
53
+ /**
54
+ * `true` if drafts are enabled, otherwise `false`. Can be used for account
55
+ * tier validation.
56
+ */
57
+ isDraftsEnabled,
58
+ /** Your application components */
59
+ children, }) {
60
+ const isMounted = useIsMounted();
61
+ const isOffline = useIsOffline();
62
+ const { isLoggedIn, isUsingFormsKey } = useAuth();
63
+ const [syncState, setSyncState] = React.useState(defaultSyncState);
64
+ const clearSyncError = React.useCallback(() => {
65
+ setSyncState((currentState) => ({
66
+ ...currentState,
67
+ syncError: null,
68
+ }));
69
+ }, []);
70
+ const syncDrafts = React.useCallback(async () => {
71
+ if (!isDraftsEnabled || !isLoggedIn || isUsingFormsKey) {
72
+ return;
73
+ }
74
+ if (isMounted.current) {
75
+ setSyncState({
76
+ isSyncing: true,
77
+ syncError: null,
78
+ });
79
+ }
80
+ let newError = null;
81
+ try {
82
+ await draftService.syncDrafts({
83
+ formsAppId,
84
+ throwError: false,
85
+ });
86
+ }
87
+ catch (error) {
88
+ newError = error;
89
+ }
90
+ if (isMounted.current) {
91
+ setSyncState({
92
+ isSyncing: false,
93
+ syncError: newError,
94
+ });
95
+ }
96
+ }, [formsAppId, isDraftsEnabled, isLoggedIn, isMounted, isUsingFormsKey]);
97
+ const [loadState, setLoadState] = React.useState(defaultLoadState);
98
+ const clearLoadError = React.useCallback(() => {
99
+ setLoadState((currentState) => ({
100
+ ...currentState,
101
+ loadError: null,
102
+ }));
103
+ }, []);
104
+ const reloadDrafts = React.useCallback(async () => {
105
+ if (!isLoggedIn) {
106
+ if (isMounted.current) {
107
+ setLoadState({
108
+ isLoading: false,
109
+ loadError: null,
110
+ drafts: [],
111
+ });
112
+ }
113
+ return;
114
+ }
115
+ if (isMounted.current) {
116
+ setLoadState((currentState) => ({
117
+ isLoading: true,
118
+ loadError: null,
119
+ drafts: currentState.drafts,
120
+ }));
121
+ }
122
+ let newError = null;
123
+ let newDrafts = [];
124
+ try {
125
+ newDrafts = await draftService.getDrafts();
126
+ }
127
+ catch (error) {
128
+ newError = error;
129
+ }
130
+ if (isMounted.current) {
131
+ setLoadState({
132
+ isLoading: false,
133
+ loadError: newError,
134
+ drafts: newDrafts,
135
+ });
136
+ }
137
+ }, [isMounted, isLoggedIn]);
138
+ const deleteDraft = React.useCallback((draftId) => {
139
+ return draftService.deleteDraft(draftId, formsAppId);
140
+ }, [formsAppId]);
141
+ const value = React.useMemo(() => ({
142
+ // Sync
143
+ syncDrafts,
144
+ isSyncing: syncState.isSyncing,
145
+ syncError: syncState.syncError,
146
+ clearSyncError,
147
+ // Load
148
+ reloadDrafts,
149
+ isLoading: loadState.isLoading,
150
+ drafts: loadState.drafts,
151
+ loadError: loadState.loadError,
152
+ clearLoadError,
153
+ // Delete,
154
+ deleteDraft,
155
+ }), [
156
+ clearLoadError,
157
+ clearSyncError,
158
+ loadState.drafts,
159
+ loadState.isLoading,
160
+ loadState.loadError,
161
+ reloadDrafts,
162
+ syncDrafts,
163
+ syncState.isSyncing,
164
+ syncState.syncError,
165
+ deleteDraft,
166
+ ]);
167
+ React.useEffect(() => {
168
+ reloadDrafts();
169
+ const unregisterPendingQueueListener = submissionService.registerPendingQueueListener(reloadDrafts);
170
+ const unregisterDraftsListener = draftService.registerDraftsListener((drafts) => {
171
+ setLoadState({
172
+ isLoading: false,
173
+ drafts,
174
+ loadError: null,
175
+ });
176
+ });
177
+ return () => {
178
+ unregisterPendingQueueListener();
179
+ unregisterDraftsListener();
180
+ };
181
+ }, [reloadDrafts]);
182
+ React.useEffect(() => {
183
+ if (!isOffline) {
184
+ syncDrafts();
185
+ }
186
+ }, [isOffline, syncDrafts]);
187
+ return (React.createElement(DraftsContext.Provider, { value: value }, children));
188
+ }
189
+ /**
190
+ * React hook to get the context value for Drafts. Will throw an Error if used
191
+ * outside of the `<DraftsContextProvider />` component.
192
+ *
193
+ * @returns
194
+ */
195
+ export default function useDrafts() {
196
+ const value = React.useContext(DraftsContext);
197
+ if (!value) {
198
+ throw new Error(`"useDrafts" hook was used outside of the "<DraftsContextProvider />" component's children.`);
199
+ }
200
+ return value;
201
+ }
202
+ //# 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,g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AAC,MAAM,EAAE,EAAE;YACT,YAAY,CAAC;gBACX,SAAS,EAAE,KAAK;gBAChB,MAAM;gBACN,SAAS,EAAE,IAAI;aAChB,CAAC,CAAA;QACJ,CAAC,CACF,CAAA;QACD,OAAO,GAAG,EAAE;YACV,8BAA8B,EAAE,CAAA;YAChC,wBAAwB,EAAE,CAAA;QAC5B,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAA;IAElB,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,IAAI,CAAC,SAAS,EAAE;YACd,UAAU,EAAE,CAAA;SACb;IACH,CAAC,EAAE,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC,CAAA;IAE3B,OAAO,CACL,oBAAC,aAAa,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAAG,QAAQ,CAA0B,CAC1E,CAAA;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,SAAS;IAC/B,MAAM,KAAK,GAAG,KAAK,CAAC,UAAU,CAAC,aAAa,CAAC,CAAA;IAC7C,IAAI,CAAC,KAAK,EAAE;QACV,MAAM,IAAI,KAAK,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 } 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 */\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 */\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"]}
@@ -1,7 +1,15 @@
1
1
  import * as React from 'react';
2
2
  import { submissionService } from '@oneblink/apps';
3
- import { FormTypes } from '@oneblink/types';
4
- import { FormSubmissionModel } from '../types/form';
3
+ import { FormTypes, SubmissionTypes } from '@oneblink/types';
4
+ /**
5
+ * Use this if you want to implement a controlled auto saving form. See
6
+ * {@link OneBlinkFormControlled} for a full example. If you do not need to
7
+ * control the `submission` or `definition` properties, you can use the
8
+ * {@link OneBlinkAutoSaveForm} component.
9
+ *
10
+ * @param options
11
+ * @returns
12
+ */
5
13
  export default function useFormSubmissionAutoSaveState({ form, initialSubmission, resumeAtElement, removeAutoSaveDataBeforeSubmit, removeAutoSaveDataBeforeSaveDraft, autoSaveKey, onCancel, onSubmit, onSaveDraft, }: {
6
14
  form: FormTypes.Form;
7
15
  removeAutoSaveDataBeforeSubmit?: boolean;
@@ -9,12 +17,14 @@ export default function useFormSubmissionAutoSaveState({ form, initialSubmission
9
17
  autoSaveKey: string;
10
18
  onCancel: () => unknown;
11
19
  onSubmit: (newFormSubmission: submissionService.NewFormSubmission) => unknown;
12
- initialSubmission?: FormSubmissionModel;
20
+ initialSubmission?: SubmissionTypes.S3SubmissionData['submission'];
13
21
  resumeAtElement?: FormTypes.FormElement;
14
22
  onSaveDraft?: (newDraftSubmission: submissionService.NewDraftSubmission) => unknown;
15
23
  }): {
16
24
  definition: FormTypes.Form;
17
- submission: FormSubmissionModel;
25
+ submission: {
26
+ [name: string]: unknown;
27
+ };
18
28
  lastElementUpdated: FormTypes.FormElement | undefined;
19
29
  isLoadingAutoSaveSubmission: boolean;
20
30
  isAutoSaveSubmissionAvailable: boolean;
@@ -26,7 +36,9 @@ export default function useFormSubmissionAutoSaveState({ form, initialSubmission
26
36
  handleNavigateAway: () => void;
27
37
  setFormSubmission: React.Dispatch<React.SetStateAction<{
28
38
  definition: FormTypes.Form;
29
- submission: FormSubmissionModel;
39
+ submission: {
40
+ [name: string]: unknown;
41
+ };
30
42
  lastElementUpdated: FormTypes.FormElement | undefined;
31
43
  }>>;
32
44
  };
@@ -2,6 +2,15 @@ import * as React from 'react';
2
2
  import _throttle from 'lodash.throttle';
3
3
  import { autoSaveService, Sentry } from '@oneblink/apps';
4
4
  import useFormSubmissionState from './useFormSubmissionState';
5
+ /**
6
+ * Use this if you want to implement a controlled auto saving form. See
7
+ * {@link OneBlinkFormControlled} for a full example. If you do not need to
8
+ * control the `submission` or `definition` properties, you can use the
9
+ * {@link OneBlinkAutoSaveForm} component.
10
+ *
11
+ * @param options
12
+ * @returns
13
+ */
5
14
  export default function useFormSubmissionAutoSaveState({ form, initialSubmission, resumeAtElement, removeAutoSaveDataBeforeSubmit, removeAutoSaveDataBeforeSaveDraft, autoSaveKey, onCancel, onSubmit, onSaveDraft, }) {
6
15
  const [{ definition, submission, lastElementUpdated }, setFormSubmission] = useFormSubmissionState(form, initialSubmission, resumeAtElement);
7
16
  const [{ isLoadingAutoSaveSubmission, autoSaveSubmission, autoSaveElement }, setAutoSaveState,] = React.useState({
@@ -1 +1 @@
1
- {"version":3,"file":"useFormSubmissionAutoSaveState.js","sourceRoot":"","sources":["../../src/hooks/useFormSubmissionAutoSaveState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,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,EAAE,CAAC,wBAAwB,CAAC,CAAC,CAAA;IAE9B,MAAM,0BAA0B,GAAG,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE;QACxD,IAAI,kBAAkB,EAAE;YACtB,iBAAiB,CAAC,CAAC,qBAAqB,EAAE,EAAE,CAAC,CAAC;gBAC5C,GAAG,qBAAqB;gBACxB,UAAU,EAAE,kBAAkB;gBAC9B,kBAAkB,EAAE,eAAe,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,SAAS;aAClE,CAAC,CAAC,CAAA;SACJ;QACD,gBAAgB,CAAC;YACf,2BAA2B,EAAE,KAAK;YAClC,kBAAkB,EAAE,IAAI;YACxB,eAAe,EAAE,IAAI;SACtB,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,kBAAkB,EAAE,iBAAiB,EAAE,eAAe,CAAC,CAAC,CAAA;IAE5D,OAAO;QACL,UAAU;QACV,UAAU;QACV,kBAAkB;QAClB,2BAA2B;QAC3B,6BAA6B,EAAE,kBAAkB,KAAK,IAAI;QAC1D,kBAAkB;QAClB,0BAA0B;QAC1B,YAAY;QACZ,YAAY;QACZ,eAAe;QACf,kBAAkB;QAClB,iBAAiB,EAAE,yBAAyB;KAC7C,CAAA;AACH,CAAC","sourcesContent":["import * as React from 'react'\nimport _throttle from 'lodash.throttle'\nimport { autoSaveService, submissionService, Sentry } from '@oneblink/apps'\nimport { FormTypes } from '@oneblink/types'\nimport useFormSubmissionState from './useFormSubmissionState'\nimport { FormSubmissionModel } from '../types/form'\nimport { FormElement } from '@oneblink/types/typescript/forms'\n\nexport default function useFormSubmissionAutoSaveState({\n form,\n initialSubmission,\n resumeAtElement,\n removeAutoSaveDataBeforeSubmit,\n removeAutoSaveDataBeforeSaveDraft,\n autoSaveKey,\n onCancel,\n onSubmit,\n onSaveDraft,\n}: {\n form: FormTypes.Form\n removeAutoSaveDataBeforeSubmit?: boolean\n removeAutoSaveDataBeforeSaveDraft?: boolean\n autoSaveKey: string\n onCancel: () => unknown\n onSubmit: (newFormSubmission: submissionService.NewFormSubmission) => unknown\n initialSubmission?: FormSubmissionModel\n resumeAtElement?: FormTypes.FormElement\n onSaveDraft?: (\n newDraftSubmission: submissionService.NewDraftSubmission,\n ) => unknown\n}) {\n const [{ definition, submission, lastElementUpdated }, setFormSubmission] =\n useFormSubmissionState(form, initialSubmission, resumeAtElement)\n\n const [\n { isLoadingAutoSaveSubmission, autoSaveSubmission, autoSaveElement },\n setAutoSaveState,\n ] = React.useState<{\n isLoadingAutoSaveSubmission: boolean\n autoSaveSubmission: FormSubmissionModel | null\n autoSaveElement: FormElement | null\n }>({\n isLoadingAutoSaveSubmission: true,\n autoSaveSubmission: null,\n autoSaveElement: null,\n })\n\n const throttledAutoSave = React.useMemo(() => {\n return _throttle(\n (model: FormSubmissionModel, lastElementUpdated?: FormElement) => {\n console.log('Auto saving...')\n autoSaveService\n .upsertAutoSaveData(definition.id, autoSaveKey, model)\n .then(() => {\n if (lastElementUpdated) {\n return autoSaveService.upsertAutoSaveData<FormElement>(\n definition.id,\n `LAST_ELEMENT_UPDATED_${autoSaveKey}`,\n lastElementUpdated,\n )\n }\n })\n .catch((error) => {\n console.warn('Error while auto saving', error)\n Sentry.captureException(error)\n })\n },\n 9580, // https://en.wikipedia.org/wiki/100_metres\n { trailing: true, leading: false },\n )\n }, [autoSaveKey, definition.id])\n\n const cancelAutoSave = React.useCallback(() => {\n if (throttledAutoSave) {\n throttledAutoSave.cancel()\n }\n }, [throttledAutoSave])\n\n const deleteAutoSaveSubmission = React.useCallback(() => {\n return autoSaveService\n .deleteAutoSaveData(definition.id, autoSaveKey)\n .catch((error) => {\n console.warn('Error removing auto save data: ', error)\n Sentry.captureException(error)\n })\n }, [autoSaveKey, definition.id])\n\n const handleSubmit = React.useCallback(\n (submissionResult: submissionService.NewFormSubmission) => {\n cancelAutoSave()\n if (removeAutoSaveDataBeforeSubmit !== false) {\n deleteAutoSaveSubmission()\n }\n onSubmit(submissionResult)\n },\n [\n cancelAutoSave,\n deleteAutoSaveSubmission,\n onSubmit,\n removeAutoSaveDataBeforeSubmit,\n ],\n )\n\n const handleSaveDraft = React.useMemo(() => {\n if (onSaveDraft) {\n return (newDraftSubmission: submissionService.NewDraftSubmission) => {\n cancelAutoSave()\n if (removeAutoSaveDataBeforeSaveDraft !== false) {\n deleteAutoSaveSubmission()\n }\n if (onSaveDraft) {\n onSaveDraft(newDraftSubmission)\n }\n }\n }\n }, [\n cancelAutoSave,\n deleteAutoSaveSubmission,\n onSaveDraft,\n removeAutoSaveDataBeforeSaveDraft,\n ])\n\n const handleNavigateAway = React.useCallback(() => {\n cancelAutoSave()\n deleteAutoSaveSubmission()\n }, [cancelAutoSave, deleteAutoSaveSubmission])\n\n const handleCancel = React.useCallback(() => {\n cancelAutoSave()\n deleteAutoSaveSubmission()\n onCancel()\n }, [cancelAutoSave, deleteAutoSaveSubmission, onCancel])\n\n React.useEffect(() => {\n let ignore = false\n const loadAutoSaveData = async () => {\n try {\n const autoSaveSubmission =\n await autoSaveService.getAutoSaveData<FormSubmissionModel>(\n definition.id,\n autoSaveKey,\n )\n const autoSaveElement =\n await autoSaveService.getAutoSaveData<FormElement>(\n definition.id,\n `LAST_ELEMENT_UPDATED_${autoSaveKey}`,\n )\n if (!ignore) {\n setAutoSaveState({\n isLoadingAutoSaveSubmission: false,\n autoSaveSubmission,\n autoSaveElement,\n })\n }\n } catch (error) {\n console.warn('Error loading auto save data', error)\n Sentry.captureException(error)\n if (!ignore) {\n setAutoSaveState({\n isLoadingAutoSaveSubmission: false,\n autoSaveSubmission: null,\n autoSaveElement: null,\n })\n }\n }\n }\n loadAutoSaveData()\n return () => {\n ignore = true\n }\n }, [autoSaveKey, definition.id])\n\n // Clean up throttle function on unmount\n React.useEffect(() => {\n return () => {\n cancelAutoSave()\n }\n }, [cancelAutoSave])\n\n const setFormSubmissionAutoSave: typeof setFormSubmission = React.useCallback(\n (formSubmission) => {\n setFormSubmission((currentFormSubmission) => {\n const newFormSubmission =\n typeof formSubmission === 'function'\n ? formSubmission(currentFormSubmission)\n : formSubmission\n\n throttledAutoSave(\n newFormSubmission.submission,\n newFormSubmission.lastElementUpdated,\n )\n\n return newFormSubmission\n })\n },\n [setFormSubmission, throttledAutoSave],\n )\n\n const startNewSubmission = React.useCallback(() => {\n deleteAutoSaveSubmission()\n setAutoSaveState({\n isLoadingAutoSaveSubmission: false,\n autoSaveSubmission: null,\n autoSaveElement: null,\n })\n }, [deleteAutoSaveSubmission])\n\n const continueAutoSaveSubmission = React.useCallback(() => {\n if (autoSaveSubmission) {\n setFormSubmission((currentFormSubmission) => ({\n ...currentFormSubmission,\n submission: autoSaveSubmission,\n lastElementUpdated: autoSaveElement ? autoSaveElement : undefined,\n }))\n }\n setAutoSaveState({\n isLoadingAutoSaveSubmission: false,\n autoSaveSubmission: null,\n autoSaveElement: null,\n })\n }, [autoSaveSubmission, setFormSubmission, autoSaveElement])\n\n return {\n definition,\n submission,\n lastElementUpdated,\n isLoadingAutoSaveSubmission,\n isAutoSaveSubmissionAvailable: autoSaveSubmission !== null,\n startNewSubmission,\n continueAutoSaveSubmission,\n handleSubmit,\n handleCancel,\n handleSaveDraft,\n handleNavigateAway,\n setFormSubmission: setFormSubmissionAutoSave,\n }\n}\n"]}
1
+ {"version":3,"file":"useFormSubmissionAutoSaveState.js","sourceRoot":"","sources":["../../src/hooks/useFormSubmissionAutoSaveState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,SAAS,MAAM,iBAAiB,CAAA;AACvC,OAAO,EAAE,eAAe,EAAqB,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAE3E,OAAO,sBAAsB,MAAM,0BAA0B,CAAA;AAG7D;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,UAAU,8BAA8B,CAAC,EACrD,IAAI,EACJ,iBAAiB,EACjB,eAAe,EACf,8BAA8B,EAC9B,iCAAiC,EACjC,WAAW,EACX,QAAQ,EACR,QAAQ,E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AAC,eAAe,CAAC,CAAC,CAAC,SAAS;aAClE,CAAC,CAAC,CAAA;SACJ;QACD,gBAAgB,CAAC;YACf,2BAA2B,EAAE,KAAK;YAClC,kBAAkB,EAAE,IAAI;YACxB,eAAe,EAAE,IAAI;SACtB,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,kBAAkB,EAAE,iBAAiB,EAAE,eAAe,CAAC,CAAC,CAAA;IAE5D,OAAO;QACL,UAAU;QACV,UAAU;QACV,kBAAkB;QAClB,2BAA2B;QAC3B,6BAA6B,EAAE,kBAAkB,KAAK,IAAI;QAC1D,kBAAkB;QAClB,0BAA0B;QAC1B,YAAY;QACZ,YAAY;QACZ,eAAe;QACf,kBAAkB;QAClB,iBAAiB,EAAE,yBAAyB;KAC7C,CAAA;AACH,CAAC","sourcesContent":["import * as React from 'react'\nimport _throttle from 'lodash.throttle'\nimport { autoSaveService, submissionService, Sentry } from '@oneblink/apps'\nimport { FormTypes, SubmissionTypes } from '@oneblink/types'\nimport useFormSubmissionState from './useFormSubmissionState'\nimport { FormElement } from '@oneblink/types/typescript/forms'\n\n/**\n * Use this if you want to implement a controlled auto saving form. See\n * {@link OneBlinkFormControlled} for a full example. If you do not need to\n * control the `submission` or `definition` properties, you can use the\n * {@link OneBlinkAutoSaveForm} component.\n *\n * @param options\n * @returns\n */\nexport default function useFormSubmissionAutoSaveState({\n form,\n initialSubmission,\n resumeAtElement,\n removeAutoSaveDataBeforeSubmit,\n removeAutoSaveDataBeforeSaveDraft,\n autoSaveKey,\n onCancel,\n onSubmit,\n onSaveDraft,\n}: {\n form: FormTypes.Form\n removeAutoSaveDataBeforeSubmit?: boolean\n removeAutoSaveDataBeforeSaveDraft?: boolean\n autoSaveKey: string\n onCancel: () => unknown\n onSubmit: (newFormSubmission: submissionService.NewFormSubmission) => unknown\n initialSubmission?: SubmissionTypes.S3SubmissionData['submission']\n resumeAtElement?: FormTypes.FormElement\n onSaveDraft?: (\n newDraftSubmission: submissionService.NewDraftSubmission,\n ) => unknown\n}) {\n const [{ definition, submission, lastElementUpdated }, setFormSubmission] =\n useFormSubmissionState(form, initialSubmission, resumeAtElement)\n\n const [\n { isLoadingAutoSaveSubmission, autoSaveSubmission, autoSaveElement },\n setAutoSaveState,\n ] = React.useState<{\n isLoadingAutoSaveSubmission: boolean\n autoSaveSubmission: SubmissionTypes.S3SubmissionData['submission'] | null\n autoSaveElement: FormElement | null\n }>({\n isLoadingAutoSaveSubmission: true,\n autoSaveSubmission: null,\n autoSaveElement: null,\n })\n\n const throttledAutoSave = React.useMemo(() => {\n return _throttle(\n (\n model: SubmissionTypes.S3SubmissionData['submission'],\n lastElementUpdated?: FormElement,\n ) => {\n console.log('Auto saving...')\n autoSaveService\n .upsertAutoSaveData(definition.id, autoSaveKey, model)\n .then(() => {\n if (lastElementUpdated) {\n return autoSaveService.upsertAutoSaveData<FormElement>(\n definition.id,\n `LAST_ELEMENT_UPDATED_${autoSaveKey}`,\n lastElementUpdated,\n )\n }\n })\n .catch((error) => {\n console.warn('Error while auto saving', error)\n Sentry.captureException(error)\n })\n },\n 9580, // https://en.wikipedia.org/wiki/100_metres\n { trailing: true, leading: false },\n )\n }, [autoSaveKey, definition.id])\n\n const cancelAutoSave = React.useCallback(() => {\n if (throttledAutoSave) {\n throttledAutoSave.cancel()\n }\n }, [throttledAutoSave])\n\n const deleteAutoSaveSubmission = React.useCallback(() => {\n return autoSaveService\n .deleteAutoSaveData(definition.id, autoSaveKey)\n .catch((error) => {\n console.warn('Error removing auto save data: ', error)\n Sentry.captureException(error)\n })\n }, [autoSaveKey, definition.id])\n\n const handleSubmit = React.useCallback(\n (submissionResult: submissionService.NewFormSubmission) => {\n cancelAutoSave()\n if (removeAutoSaveDataBeforeSubmit !== false) {\n deleteAutoSaveSubmission()\n }\n onSubmit(submissionResult)\n },\n [\n cancelAutoSave,\n deleteAutoSaveSubmission,\n onSubmit,\n removeAutoSaveDataBeforeSubmit,\n ],\n )\n\n const handleSaveDraft = React.useMemo(() => {\n if (onSaveDraft) {\n return (newDraftSubmission: submissionService.NewDraftSubmission) => {\n cancelAutoSave()\n if (removeAutoSaveDataBeforeSaveDraft !== false) {\n deleteAutoSaveSubmission()\n }\n if (onSaveDraft) {\n onSaveDraft(newDraftSubmission)\n }\n }\n }\n }, [\n cancelAutoSave,\n deleteAutoSaveSubmission,\n onSaveDraft,\n removeAutoSaveDataBeforeSaveDraft,\n ])\n\n const handleNavigateAway = React.useCallback(() => {\n cancelAutoSave()\n deleteAutoSaveSubmission()\n }, [cancelAutoSave, deleteAutoSaveSubmission])\n\n const handleCancel = React.useCallback(() => {\n cancelAutoSave()\n deleteAutoSaveSubmission()\n onCancel()\n }, [cancelAutoSave, deleteAutoSaveSubmission, onCancel])\n\n React.useEffect(() => {\n let ignore = false\n const loadAutoSaveData = async () => {\n try {\n const autoSaveSubmission = await autoSaveService.getAutoSaveData<\n SubmissionTypes.S3SubmissionData['submission']\n >(definition.id, autoSaveKey)\n const autoSaveElement =\n await autoSaveService.getAutoSaveData<FormElement>(\n definition.id,\n `LAST_ELEMENT_UPDATED_${autoSaveKey}`,\n )\n if (!ignore) {\n setAutoSaveState({\n isLoadingAutoSaveSubmission: false,\n autoSaveSubmission,\n autoSaveElement,\n })\n }\n } catch (error) {\n console.warn('Error loading auto save data', error)\n Sentry.captureException(error)\n if (!ignore) {\n setAutoSaveState({\n isLoadingAutoSaveSubmission: false,\n autoSaveSubmission: null,\n autoSaveElement: null,\n })\n }\n }\n }\n loadAutoSaveData()\n return () => {\n ignore = true\n }\n }, [autoSaveKey, definition.id])\n\n // Clean up throttle function on unmount\n React.useEffect(() => {\n return () => {\n cancelAutoSave()\n }\n }, [cancelAutoSave])\n\n const setFormSubmissionAutoSave: typeof setFormSubmission = React.useCallback(\n (formSubmission) => {\n setFormSubmission((currentFormSubmission) => {\n const newFormSubmission =\n typeof formSubmission === 'function'\n ? formSubmission(currentFormSubmission)\n : formSubmission\n\n throttledAutoSave(\n newFormSubmission.submission,\n newFormSubmission.lastElementUpdated,\n )\n\n return newFormSubmission\n })\n },\n [setFormSubmission, throttledAutoSave],\n )\n\n const startNewSubmission = React.useCallback(() => {\n deleteAutoSaveSubmission()\n setAutoSaveState({\n isLoadingAutoSaveSubmission: false,\n autoSaveSubmission: null,\n autoSaveElement: null,\n })\n }, [deleteAutoSaveSubmission])\n\n const continueAutoSaveSubmission = React.useCallback(() => {\n if (autoSaveSubmission) {\n setFormSubmission((currentFormSubmission) => ({\n ...currentFormSubmission,\n submission: autoSaveSubmission,\n lastElementUpdated: autoSaveElement ? autoSaveElement : undefined,\n }))\n }\n setAutoSaveState({\n isLoadingAutoSaveSubmission: false,\n autoSaveSubmission: null,\n autoSaveElement: null,\n })\n }, [autoSaveSubmission, setFormSubmission, autoSaveElement])\n\n return {\n definition,\n submission,\n lastElementUpdated,\n isLoadingAutoSaveSubmission,\n isAutoSaveSubmissionAvailable: autoSaveSubmission !== null,\n startNewSubmission,\n continueAutoSaveSubmission,\n handleSubmit,\n handleCancel,\n handleSaveDraft,\n handleNavigateAway,\n setFormSubmission: setFormSubmissionAutoSave,\n }\n}\n"]}
@@ -1,14 +1,14 @@
1
- import { FormTypes } from '@oneblink/types';
1
+ import { FormTypes, SubmissionTypes } from '@oneblink/types';
2
2
  import { FormElement } from '@oneblink/types/typescript/forms';
3
3
  import * as React from 'react';
4
- import { FormElementsConditionallyShown, FormSubmissionModel } from '../types/form';
4
+ import { FormElementsConditionallyShown } from '../types/form';
5
5
  export type FormSubmissionModelContextValue = {
6
- formSubmissionModel: FormSubmissionModel;
6
+ formSubmissionModel: SubmissionTypes.S3SubmissionData['submission'];
7
7
  parent?: FormSubmissionModelContextValue;
8
8
  elements: FormElement[];
9
9
  };
10
10
  export declare function FormSubmissionModelContextProvider({ children, model, elements, formElementsConditionallyShown, }: {
11
- model: FormSubmissionModel;
11
+ model: SubmissionTypes.S3SubmissionData['submission'];
12
12
  elements: FormTypes.FormElement[];
13
13
  formElementsConditionallyShown: FormElementsConditionallyShown | undefined;
14
14
  children: React.ReactNode;
@@ -1 +1 @@
1
- {"version":3,"file":"useFormSubmissionModelContext.js","sourceRoot":"","sources":["../../src/hooks/useFormSubmissionModelContext.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,wBAAwB,MAAM,sCAAsC,CAAA;AAY3E,MAAM,0BAA0B,GAAG,KAAK,CAAC,aAAa,CAEpD,SAAS,CAAC,CAAA;AAEZ,MAAM,UAAU,kCAAkC,CAAC,EACjD,QAAQ,EACR,KAAK,EACL,QAAQ,EACR,8BAA8B,GAM/B;IACC,MAAM,0BAA0B,GAAG,KAAK,CAAC,UAAU,CACjD,0BAA0B,CAC3B,CAAA;IACD,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE;QAC/B,OAAO;YACL,mBAAmB,EAAE,wBAAwB,CAC3C,KAAK,EACL,QAAQ,EACR,8BAA8B,EAC9B,IAAI,CACL,CAAC,KAAK;YACP,MAAM,EAAE,0BAA0B;YAClC,QAAQ;SACT,CAAA;IACH,CAAC,EAAE;QACD,KAAK;QACL,QAAQ;QACR,8BAA8B;QAC9B,0BAA0B;KAC3B,CAAC,CAAA;IACF,OAAO,CACL,oBAAC,0BAA0B,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAC9C,QAAQ,CAC2B,CACvC,CAAA;AACH,CAAC;AAED,MAAM,CAAC,OAAO,UAAU,sBAAsB;IAC5C,MAAM,0BAA0B,GAAG,KAAK,CAAC,UAAU,CACjD,0BAA0B,CAC3B,CAAA;IACD,IAAI,CAAC,0BAA0B,EAAE;QAC/B,MAAM,IAAI,KAAK,CACb,oEAAoE,CACrE,CAAA;KACF;IACD,OAAO,0BAA0B,CAAA;AACnC,CAAC;AAED,MAAM,UAAU,4BAA4B;IAC1C,IAAI,0BAA0B,GAAG,sBAAsB,EAAE,CAAA;IACzD,OAAO,0BAA0B,CAAC,MAAM,EAAE;QACxC,0BAA0B,GAAG,0BAA0B,CAAC,MAAM,CAAA;KAC/D;IACD,OAAO,0BAA0B,CAAA;AACnC,CAAC","sourcesContent":["import { FormTypes } from '@oneblink/types'\nimport { FormElement } from '@oneblink/types/typescript/forms'\nimport * as React from 'react'\nimport cleanFormSubmissionModel from '../services/cleanFormSubmissionModel'\nimport {\n FormElementsConditionallyShown,\n FormSubmissionModel,\n} from '../types/form'\n\nexport type FormSubmissionModelContextValue = {\n formSubmissionModel: FormSubmissionModel\n parent?: FormSubmissionModelContextValue\n elements: FormElement[]\n}\n\nconst FormSubmissionModelContext = React.createContext<\n FormSubmissionModelContextValue | undefined\n>(undefined)\n\nexport function FormSubmissionModelContextProvider({\n children,\n model,\n elements,\n formElementsConditionallyShown,\n}: {\n model: FormSubmissionModel\n elements: FormTypes.FormElement[]\n formElementsConditionallyShown: FormElementsConditionallyShown | undefined\n children: React.ReactNode\n}) {\n const formSubmissionModelContext = React.useContext(\n FormSubmissionModelContext,\n )\n const value = React.useMemo(() => {\n return {\n formSubmissionModel: cleanFormSubmissionModel(\n model,\n elements,\n formElementsConditionallyShown,\n true,\n ).model,\n parent: formSubmissionModelContext,\n elements,\n }\n }, [\n model,\n elements,\n formElementsConditionallyShown,\n formSubmissionModelContext,\n ])\n return (\n <FormSubmissionModelContext.Provider value={value}>\n {children}\n </FormSubmissionModelContext.Provider>\n )\n}\n\nexport default function useFormSubmissionModel() {\n const formSubmissionModelContext = React.useContext(\n FormSubmissionModelContext,\n )\n if (!formSubmissionModelContext) {\n throw new Error(\n '\"FormSubmissionModelContext\" does not have a value in this context',\n )\n }\n return formSubmissionModelContext\n}\n\nexport function useFormSubmissionModelParent() {\n let formSubmissionModelContext = useFormSubmissionModel()\n while (formSubmissionModelContext.parent) {\n formSubmissionModelContext = formSubmissionModelContext.parent\n }\n return formSubmissionModelContext\n}\n"]}
1
+ {"version":3,"file":"useFormSubmissionModelContext.js","sourceRoot":"","sources":["../../src/hooks/useFormSubmissionModelContext.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,wBAAwB,MAAM,sCAAsC,CAAA;AAS3E,MAAM,0BAA0B,GAAG,KAAK,CAAC,aAAa,CAEpD,SAAS,CAAC,CAAA;AAEZ,MAAM,UAAU,kCAAkC,CAAC,EACjD,QAAQ,EACR,KAAK,EACL,QAAQ,EACR,8BAA8B,GAM/B;IACC,MAAM,0BAA0B,GAAG,KAAK,CAAC,UAAU,CACjD,0BAA0B,CAC3B,CAAA;IACD,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE;QAC/B,OAAO;YACL,mBAAmB,EAAE,wBAAwB,CAC3C,KAAK,EACL,QAAQ,EACR,8BAA8B,EAC9B,IAAI,CACL,CAAC,KAAK;YACP,MAAM,EAAE,0BAA0B;YAClC,QAAQ;SACT,CAAA;IACH,CAAC,EAAE;QACD,KAAK;QACL,QAAQ;QACR,8BAA8B;QAC9B,0BAA0B;KAC3B,CAAC,CAAA;IACF,OAAO,CACL,oBAAC,0BAA0B,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAC9C,QAAQ,CAC2B,CACvC,CAAA;AACH,CAAC;AAED,MAAM,CAAC,OAAO,UAAU,sBAAsB;IAC5C,MAAM,0BAA0B,GAAG,KAAK,CAAC,UAAU,CACjD,0BAA0B,CAC3B,CAAA;IACD,IAAI,CAAC,0BAA0B,EAAE;QAC/B,MAAM,IAAI,KAAK,CACb,oEAAoE,CACrE,CAAA;KACF;IACD,OAAO,0BAA0B,CAAA;AACnC,CAAC;AAED,MAAM,UAAU,4BAA4B;IAC1C,IAAI,0BAA0B,GAAG,sBAAsB,EAAE,CAAA;IACzD,OAAO,0BAA0B,CAAC,MAAM,EAAE;QACxC,0BAA0B,GAAG,0BAA0B,CAAC,MAAM,CAAA;KAC/D;IACD,OAAO,0BAA0B,CAAA;AACnC,CAAC","sourcesContent":["import { FormTypes, SubmissionTypes } from '@oneblink/types'\nimport { FormElement } from '@oneblink/types/typescript/forms'\nimport * as React from 'react'\nimport cleanFormSubmissionModel from '../services/cleanFormSubmissionModel'\nimport { FormElementsConditionallyShown } from '../types/form'\n\nexport type FormSubmissionModelContextValue = {\n formSubmissionModel: SubmissionTypes.S3SubmissionData['submission']\n parent?: FormSubmissionModelContextValue\n elements: FormElement[]\n}\n\nconst FormSubmissionModelContext = React.createContext<\n FormSubmissionModelContextValue | undefined\n>(undefined)\n\nexport function FormSubmissionModelContextProvider({\n children,\n model,\n elements,\n formElementsConditionallyShown,\n}: {\n model: SubmissionTypes.S3SubmissionData['submission']\n elements: FormTypes.FormElement[]\n formElementsConditionallyShown: FormElementsConditionallyShown | undefined\n children: React.ReactNode\n}) {\n const formSubmissionModelContext = React.useContext(\n FormSubmissionModelContext,\n )\n const value = React.useMemo(() => {\n return {\n formSubmissionModel: cleanFormSubmissionModel(\n model,\n elements,\n formElementsConditionallyShown,\n true,\n ).model,\n parent: formSubmissionModelContext,\n elements,\n }\n }, [\n model,\n elements,\n formElementsConditionallyShown,\n formSubmissionModelContext,\n ])\n return (\n <FormSubmissionModelContext.Provider value={value}>\n {children}\n </FormSubmissionModelContext.Provider>\n )\n}\n\nexport default function useFormSubmissionModel() {\n const formSubmissionModelContext = React.useContext(\n FormSubmissionModelContext,\n )\n if (!formSubmissionModelContext) {\n throw new Error(\n '\"FormSubmissionModelContext\" does not have a value in this context',\n )\n }\n return formSubmissionModelContext\n}\n\nexport function useFormSubmissionModelParent() {\n let formSubmissionModelContext = useFormSubmissionModel()\n while (formSubmissionModelContext.parent) {\n formSubmissionModelContext = formSubmissionModelContext.parent\n }\n return formSubmissionModelContext\n}\n"]}