@oneblink/apps-react 6.2.4-beta.1 → 6.2.4-beta.2

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.
@@ -118,7 +118,10 @@ export function PendingSubmissionsContextProvider({ isPendingQueueEnabled, succe
118
118
  hideFailedNotification();
119
119
  break;
120
120
  }
121
- case 'ADDITION': {
121
+ // EDIT_CANCELLED is here for when beginnning edit offline and then hitting cancel while online
122
+ // among other use cases
123
+ case 'ADDITION':
124
+ case 'EDIT_CANCELLED': {
122
125
  processPendingQueue();
123
126
  break;
124
127
  }
@@ -1 +1 @@
1
- {"version":3,"file":"usePendingSubmissions.js","sourceRoot":"","sources":["../../src/hooks/usePendingSubmissions.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAClD,OAAO,eAAe,MAAM,mBAAmB,CAAA;AAC/C,OAAO,YAAY,MAAM,gBAAgB,CAAA;AACzC,OAAO,YAAY,MAAM,gBAAgB,CAAA;AAqCzC,MAAM,YAAY,GAAG;IACnB,SAAS,EAAE,KAAK;IAChB,SAAS,EAAE,IAAI;IACf,kBAAkB,EAAE,EAAE;CACvB,CAAA;AAED,MAAM,yBAAyB,GAAG,KAAK,CAAC,aAAa,CAEnD,SAAS,CAAC,CAAA;AAEZ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,UAAU,iCAAiC,CAAC,EAChD,qBAAqB,EACrB,4BAA4B,GAAG,IAAI,EACnC,QAAQ,GAiBT;IACC,MAAM,SAAS,GAAG,YAAY,EAAE,CAAA;IAChC,MAAM,SAAS,GAAG,YAAY,EAAE,CAAA;IAEhC,kEAAkE;IAClE,4DAA4D;IAC5D,MAAM,CAAC,0BAA0B,EAAE,6BAA6B,CAAC,GAC/D,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAA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,EAAE,CAAC,SAAS,EAAE,mBAAmB,CAAC,CAAC,CAAA;IAEpC,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,IAAI,0BAA0B,GAAG,CAAC,EAAE;YAClC,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE;gBAChC,uBAAuB,EAAE,CAAA;YAC3B,CAAC,EAAE,4BAA4B,CAAC,CAAA;YAChC,OAAO,GAAG,EAAE;gBACV,YAAY,CAAC,SAAS,CAAC,CAAA;YACzB,CAAC,CAAA;SACF;IACH,CAAC,EAAE;QACD,uBAAuB;QACvB,0BAA0B;QAC1B,4BAA4B;KAC7B,CAAC,CAAA;IAEF,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CACzB,GAAG,EAAE,CAAC,CAAC;QACL,GAAG,KAAK;QACR,2BAA2B;QAC3B,sBAAsB;QACtB,wBAAwB;QACxB,mBAAmB;QACnB,wBAAwB;QACxB,uBAAuB,EAAE,iBAAiB,CAAC,4BAA4B;QACvE,4BAA4B,EAAE,0BAA0B,GAAG,CAAC;QAC5D,uBAAuB;KACxB,CAAC,EACF;QACE,KAAK;QACL,2BAA2B;QAC3B,sBAAsB;QACtB,wBAAwB;QACxB,mBAAmB;QACnB,wBAAwB;QACxB,0BAA0B;QAC1B,uBAAuB;KACxB,CACF,CAAA;IAED,OAAO,CACL,oBAAC,yBAAyB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAC7C,QAAQ,CAC0B,CACtC,CAAA;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,qBAAqB;IAC3C,MAAM,KAAK,GAAG,KAAK,CAAC,UAAU,CAAC,yBAAyB,CAAC,CAAA;IACzD,IAAI,CAAC,KAAK,EAAE;QACV,MAAM,IAAI,KAAK,CACb,oHAAoH,CACrH,CAAA;KACF;IACD,OAAO,KAAK,CAAA;AACd,CAAC","sourcesContent":["import * as React from 'react'\nimport { submissionService } from '@oneblink/apps'\nimport useBooleanState from './useBooleanState'\nimport useIsMounted from './useIsMounted'\nimport useIsOffline from './useIsOffline'\n\n/** The value returned from `usePendingSubmissions()` hook */\nexport type PendingSubmissionsContextValue = {\n /** `true` if the pending submissions are currently loading */\n isLoading: boolean\n /** An Error object if loading pending submissions fails */\n loadError: Error | null\n /**\n * The submissions that were submitted offline and can be processed when back\n * online\n */\n pendingSubmissions: submissionService.PendingFormSubmission[]\n /** `true` submissions that where submitted offline are being processed */\n isProcessingPendingQueue: boolean\n /** A function to trigger processing of the pending queue */\n processPendingQueue: () => unknown\n /**\n * A function to trigger loading of the submissions that were submitted\n * offline\n */\n reloadPendingSubmissions: () => unknown\n /** A function to remove a submission from the pending submissions */\n deletePendingSubmission: (pendingTimestamp: string) => unknown\n /** `true` if a submission being processed fails */\n isShowingFailedNotification: boolean\n /** A function to hide the notification when a submission fails to process */\n hideFailedNotification: () => unknown\n /** `true` if a submission being processed completes successfully. */\n isShowingSuccessNotification: boolean\n /**\n * A function to hide the notification after a submission completed\n * successfully\n */\n hideSuccessNotification: () => unknown\n}\n\nconst defaultState = {\n isLoading: false,\n loadError: null,\n pendingSubmissions: [],\n}\n\nconst PendingSubmissionsContext = React.createContext<\n PendingSubmissionsContextValue | undefined\n>(undefined)\n\n/**\n * React Component that provides the context for the `usePendingSubmissions()`\n * hook to be used by components further down your component tree. **It should\n * only be 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 {\n * PendingSubmissionsContextProvider,\n * usePendingSubmissions,\n * } from '@oneblink/apps-react'\n *\n * function Component() {\n * const pendingSubmissionsContext = usePendingSubmissions()\n * // use pending submissions here\n * }\n *\n * function App() {\n * return (\n * <PendingSubmissionsContextProvider\n * isPendingQueueEnabled\n * successNotificationTimeoutMs={3000}\n * >\n * <Component />\n * </PendingSubmissionsContextProvider>\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 PendingSubmissionsContextProvider({\n isPendingQueueEnabled,\n successNotificationTimeoutMs = 5000,\n children,\n}: {\n /**\n * `true` if pending queue is enabled, otherwise `false`. Can be used prevent\n * offline submissions from being processed.\n */\n isPendingQueueEnabled: boolean\n /**\n * When a submission is processed successfully the\n * `isShowingSuccessNotification` will be temporarily set to `true`, it will\n * be set back to `false` after 5 seconds. This prop will allow you to\n * customise how long to wait before hiding the notification with a\n * milliseconds value.\n */\n successNotificationTimeoutMs?: number\n /** Your application components */\n children: React.ReactNode\n}) {\n const isMounted = useIsMounted()\n const isOffline = useIsOffline()\n\n // Using a number as state so we can reset the timer used to close\n // the snack bar each time a pending queue item is processed\n const [submittedNotificationCount, setSubmittedNotificationCount] =\n React.useState(0)\n const hideSuccessNotification = React.useCallback(() => {\n setSubmittedNotificationCount(0)\n }, [])\n const [\n isShowingFailedNotification,\n showFailedNotification,\n hideFailedNotification,\n ] = useBooleanState(false)\n const [isProcessingPendingQueue, setIsProcessingPendingQueue] =\n React.useState(false)\n const [state, setState] = React.useState<{\n isLoading: boolean\n loadError: Error | null\n pendingSubmissions: submissionService.PendingFormSubmission[]\n }>(defaultState)\n\n const processPendingQueue = React.useCallback(async () => {\n if (isMounted.current) {\n setIsProcessingPendingQueue(true)\n }\n\n await submissionService.processPendingQueue({\n shouldRunExternalIdGeneration: true,\n shouldRunServerValidation: true,\n })\n\n if (isMounted.current) {\n setIsProcessingPendingQueue(false)\n }\n }, [isMounted])\n\n const reloadPendingSubmissions = React.useCallback(async () => {\n if (isMounted.current) {\n setState((currentState) => ({\n isLoading: true,\n loadError: null,\n pendingSubmissions: currentState.pendingSubmissions,\n }))\n }\n let newError = null\n let newPendingSubmissions: submissionService.PendingFormSubmission[] = []\n\n try {\n newPendingSubmissions =\n await submissionService.getPendingQueueSubmissions()\n } catch (error) {\n newError = error as Error\n }\n\n if (isMounted.current) {\n setState({\n isLoading: false,\n loadError: newError,\n pendingSubmissions: newPendingSubmissions,\n })\n }\n }, [isMounted])\n\n React.useEffect(() => {\n if (!isPendingQueueEnabled) {\n return\n }\n reloadPendingSubmissions()\n return submissionService.registerPendingQueueListener(\n (pendingSubmissions, action) => {\n switch (action) {\n case 'SUBMIT_SUCCEEDED': {\n setSubmittedNotificationCount((current) => current + 1)\n break\n }\n case 'SUBMIT_FAILED': {\n showFailedNotification()\n break\n }\n case 'SUBMIT_STARTED': {\n hideFailedNotification()\n break\n }\n case 'ADDITION': {\n processPendingQueue()\n break\n }\n }\n setState({\n isLoading: false,\n pendingSubmissions,\n loadError: null,\n })\n },\n )\n }, [\n hideFailedNotification,\n isPendingQueueEnabled,\n processPendingQueue,\n reloadPendingSubmissions,\n showFailedNotification,\n ])\n\n React.useEffect(() => {\n if (!isOffline) {\n processPendingQueue()\n }\n }, [isOffline, processPendingQueue])\n\n React.useEffect(() => {\n if (submittedNotificationCount > 0) {\n const timeoutId = setTimeout(() => {\n hideSuccessNotification()\n }, successNotificationTimeoutMs)\n return () => {\n clearTimeout(timeoutId)\n }\n }\n }, [\n hideSuccessNotification,\n submittedNotificationCount,\n successNotificationTimeoutMs,\n ])\n\n const value = React.useMemo<PendingSubmissionsContextValue>(\n () => ({\n ...state,\n isShowingFailedNotification,\n hideFailedNotification,\n isProcessingPendingQueue,\n processPendingQueue,\n reloadPendingSubmissions,\n deletePendingSubmission: submissionService.deletePendingQueueSubmission,\n isShowingSuccessNotification: submittedNotificationCount > 0,\n hideSuccessNotification,\n }),\n [\n state,\n isShowingFailedNotification,\n hideFailedNotification,\n isProcessingPendingQueue,\n processPendingQueue,\n reloadPendingSubmissions,\n submittedNotificationCount,\n hideSuccessNotification,\n ],\n )\n\n return (\n <PendingSubmissionsContext.Provider value={value}>\n {children}\n </PendingSubmissionsContext.Provider>\n )\n}\n\n/**\n * React hook to get the context value for Pending Submissions. Will throw an\n * Error if used outside of the `<PendingSubmissionsContextProvider />`\n * component.\n *\n * @returns\n * @group Hooks\n */\nexport default function usePendingSubmissions(): PendingSubmissionsContextValue {\n const value = React.useContext(PendingSubmissionsContext)\n if (!value) {\n throw new Error(\n `\"usePendingSubmissions\" hook was used outside of the \"<PendingSubmissionsContextProvider />\" component's children.`,\n )\n }\n return value\n}\n"]}
1
+ {"version":3,"file":"usePendingSubmissions.js","sourceRoot":"","sources":["../../src/hooks/usePendingSubmissions.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAClD,OAAO,eAAe,MAAM,mBAAmB,CAAA;AAC/C,OAAO,YAAY,MAAM,gBAAgB,CAAA;AACzC,OAAO,YAAY,MAAM,gBAAgB,CAAA;AAqCzC,MAAM,YAAY,GAAG;IACnB,SAAS,EAAE,KAAK;IAChB,SAAS,EAAE,IAAI;IACf,kBAAkB,EAAE,EAAE;CACvB,CAAA;AAED,MAAM,yBAAyB,GAAG,KAAK,CAAC,aAAa,CAEnD,SAAS,CAAC,CAAA;AAEZ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,UAAU,iCAAiC,CAAC,EAChD,qBAAqB,EACrB,4BAA4B,GAAG,IAAI,EACnC,QAAQ,GAiBT;IACC,MAAM,SAAS,GAAG,YAAY,EAAE,CAAA;IAChC,MAAM,SAAS,GAAG,YAAY,EAAE,CAAA;IAEhC,kEAAkE;IAClE,4DAA4D;IAC5D,MAAM,CAAC,0BAA0B,EAAE,6BAA6B,CAAC,GAC/D,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;IACnB,MAAM,uBAAuB,GAAG,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE;QACrD,6BAA6B,CAAC,CAAC,CAAC,CAAA;IAClC,CAAC,EAAE,EAAE,CAAC,CAAA;IACN,MAAM,CACJ,2BAA2B,EAC3B,sBAAsB,EACtB,sBAAsB,EACvB,GAAG,eAAe,CAAC,KAAK,CAAC,CAAA;IAC1B,MAA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C,uBAAuB,EAAE,CAAA;YAC3B,CAAC,EAAE,4BAA4B,CAAC,CAAA;YAChC,OAAO,GAAG,EAAE;gBACV,YAAY,CAAC,SAAS,CAAC,CAAA;YACzB,CAAC,CAAA;SACF;IACH,CAAC,EAAE;QACD,uBAAuB;QACvB,0BAA0B;QAC1B,4BAA4B;KAC7B,CAAC,CAAA;IAEF,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CACzB,GAAG,EAAE,CAAC,CAAC;QACL,GAAG,KAAK;QACR,2BAA2B;QAC3B,sBAAsB;QACtB,wBAAwB;QACxB,mBAAmB;QACnB,wBAAwB;QACxB,uBAAuB,EAAE,iBAAiB,CAAC,4BAA4B;QACvE,4BAA4B,EAAE,0BAA0B,GAAG,CAAC;QAC5D,uBAAuB;KACxB,CAAC,EACF;QACE,KAAK;QACL,2BAA2B;QAC3B,sBAAsB;QACtB,wBAAwB;QACxB,mBAAmB;QACnB,wBAAwB;QACxB,0BAA0B;QAC1B,uBAAuB;KACxB,CACF,CAAA;IAED,OAAO,CACL,oBAAC,yBAAyB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAC7C,QAAQ,CAC0B,CACtC,CAAA;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,qBAAqB;IAC3C,MAAM,KAAK,GAAG,KAAK,CAAC,UAAU,CAAC,yBAAyB,CAAC,CAAA;IACzD,IAAI,CAAC,KAAK,EAAE;QACV,MAAM,IAAI,KAAK,CACb,oHAAoH,CACrH,CAAA;KACF;IACD,OAAO,KAAK,CAAA;AACd,CAAC","sourcesContent":["import * as React from 'react'\nimport { submissionService } from '@oneblink/apps'\nimport useBooleanState from './useBooleanState'\nimport useIsMounted from './useIsMounted'\nimport useIsOffline from './useIsOffline'\n\n/** The value returned from `usePendingSubmissions()` hook */\nexport type PendingSubmissionsContextValue = {\n /** `true` if the pending submissions are currently loading */\n isLoading: boolean\n /** An Error object if loading pending submissions fails */\n loadError: Error | null\n /**\n * The submissions that were submitted offline and can be processed when back\n * online\n */\n pendingSubmissions: submissionService.PendingFormSubmission[]\n /** `true` submissions that where submitted offline are being processed */\n isProcessingPendingQueue: boolean\n /** A function to trigger processing of the pending queue */\n processPendingQueue: () => unknown\n /**\n * A function to trigger loading of the submissions that were submitted\n * offline\n */\n reloadPendingSubmissions: () => unknown\n /** A function to remove a submission from the pending submissions */\n deletePendingSubmission: (pendingTimestamp: string) => unknown\n /** `true` if a submission being processed fails */\n isShowingFailedNotification: boolean\n /** A function to hide the notification when a submission fails to process */\n hideFailedNotification: () => unknown\n /** `true` if a submission being processed completes successfully. */\n isShowingSuccessNotification: boolean\n /**\n * A function to hide the notification after a submission completed\n * successfully\n */\n hideSuccessNotification: () => unknown\n}\n\nconst defaultState = {\n isLoading: false,\n loadError: null,\n pendingSubmissions: [],\n}\n\nconst PendingSubmissionsContext = React.createContext<\n PendingSubmissionsContextValue | undefined\n>(undefined)\n\n/**\n * React Component that provides the context for the `usePendingSubmissions()`\n * hook to be used by components further down your component tree. **It should\n * only be 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 {\n * PendingSubmissionsContextProvider,\n * usePendingSubmissions,\n * } from '@oneblink/apps-react'\n *\n * function Component() {\n * const pendingSubmissionsContext = usePendingSubmissions()\n * // use pending submissions here\n * }\n *\n * function App() {\n * return (\n * <PendingSubmissionsContextProvider\n * isPendingQueueEnabled\n * successNotificationTimeoutMs={3000}\n * >\n * <Component />\n * </PendingSubmissionsContextProvider>\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 PendingSubmissionsContextProvider({\n isPendingQueueEnabled,\n successNotificationTimeoutMs = 5000,\n children,\n}: {\n /**\n * `true` if pending queue is enabled, otherwise `false`. Can be used prevent\n * offline submissions from being processed.\n */\n isPendingQueueEnabled: boolean\n /**\n * When a submission is processed successfully the\n * `isShowingSuccessNotification` will be temporarily set to `true`, it will\n * be set back to `false` after 5 seconds. This prop will allow you to\n * customise how long to wait before hiding the notification with a\n * milliseconds value.\n */\n successNotificationTimeoutMs?: number\n /** Your application components */\n children: React.ReactNode\n}) {\n const isMounted = useIsMounted()\n const isOffline = useIsOffline()\n\n // Using a number as state so we can reset the timer used to close\n // the snack bar each time a pending queue item is processed\n const [submittedNotificationCount, setSubmittedNotificationCount] =\n React.useState(0)\n const hideSuccessNotification = React.useCallback(() => {\n setSubmittedNotificationCount(0)\n }, [])\n const [\n isShowingFailedNotification,\n showFailedNotification,\n hideFailedNotification,\n ] = useBooleanState(false)\n const [isProcessingPendingQueue, setIsProcessingPendingQueue] =\n React.useState(false)\n const [state, setState] = React.useState<{\n isLoading: boolean\n loadError: Error | null\n pendingSubmissions: submissionService.PendingFormSubmission[]\n }>(defaultState)\n\n const processPendingQueue = React.useCallback(async () => {\n if (isMounted.current) {\n setIsProcessingPendingQueue(true)\n }\n\n await submissionService.processPendingQueue({\n shouldRunExternalIdGeneration: true,\n shouldRunServerValidation: true,\n })\n\n if (isMounted.current) {\n setIsProcessingPendingQueue(false)\n }\n }, [isMounted])\n\n const reloadPendingSubmissions = React.useCallback(async () => {\n if (isMounted.current) {\n setState((currentState) => ({\n isLoading: true,\n loadError: null,\n pendingSubmissions: currentState.pendingSubmissions,\n }))\n }\n let newError = null\n let newPendingSubmissions: submissionService.PendingFormSubmission[] = []\n\n try {\n newPendingSubmissions =\n await submissionService.getPendingQueueSubmissions()\n } catch (error) {\n newError = error as Error\n }\n\n if (isMounted.current) {\n setState({\n isLoading: false,\n loadError: newError,\n pendingSubmissions: newPendingSubmissions,\n })\n }\n }, [isMounted])\n\n React.useEffect(() => {\n if (!isPendingQueueEnabled) {\n return\n }\n reloadPendingSubmissions()\n return submissionService.registerPendingQueueListener(\n (pendingSubmissions, action) => {\n switch (action) {\n case 'SUBMIT_SUCCEEDED': {\n setSubmittedNotificationCount((current) => current + 1)\n break\n }\n case 'SUBMIT_FAILED': {\n showFailedNotification()\n break\n }\n case 'SUBMIT_STARTED': {\n hideFailedNotification()\n break\n }\n // EDIT_CANCELLED is here for when beginnning edit offline and then hitting cancel while online\n // among other use cases\n case 'ADDITION':\n case 'EDIT_CANCELLED': {\n processPendingQueue()\n break\n }\n }\n setState({\n isLoading: false,\n pendingSubmissions,\n loadError: null,\n })\n },\n )\n }, [\n hideFailedNotification,\n isPendingQueueEnabled,\n processPendingQueue,\n reloadPendingSubmissions,\n showFailedNotification,\n ])\n\n React.useEffect(() => {\n if (!isOffline) {\n processPendingQueue()\n }\n }, [isOffline, processPendingQueue])\n\n React.useEffect(() => {\n if (submittedNotificationCount > 0) {\n const timeoutId = setTimeout(() => {\n hideSuccessNotification()\n }, successNotificationTimeoutMs)\n return () => {\n clearTimeout(timeoutId)\n }\n }\n }, [\n hideSuccessNotification,\n submittedNotificationCount,\n successNotificationTimeoutMs,\n ])\n\n const value = React.useMemo<PendingSubmissionsContextValue>(\n () => ({\n ...state,\n isShowingFailedNotification,\n hideFailedNotification,\n isProcessingPendingQueue,\n processPendingQueue,\n reloadPendingSubmissions,\n deletePendingSubmission: submissionService.deletePendingQueueSubmission,\n isShowingSuccessNotification: submittedNotificationCount > 0,\n hideSuccessNotification,\n }),\n [\n state,\n isShowingFailedNotification,\n hideFailedNotification,\n isProcessingPendingQueue,\n processPendingQueue,\n reloadPendingSubmissions,\n submittedNotificationCount,\n hideSuccessNotification,\n ],\n )\n\n return (\n <PendingSubmissionsContext.Provider value={value}>\n {children}\n </PendingSubmissionsContext.Provider>\n )\n}\n\n/**\n * React hook to get the context value for Pending Submissions. Will throw an\n * Error if used outside of the `<PendingSubmissionsContextProvider />`\n * component.\n *\n * @returns\n * @group Hooks\n */\nexport default function usePendingSubmissions(): PendingSubmissionsContextValue {\n const value = React.useContext(PendingSubmissionsContext)\n if (!value) {\n throw new Error(\n `\"usePendingSubmissions\" hook was used outside of the \"<PendingSubmissionsContextProvider />\" component's children.`,\n )\n }\n return value\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oneblink/apps-react",
3
3
  "description": "Helper functions for OneBlink apps in ReactJS.",
4
- "version": "6.2.4-beta.1",
4
+ "version": "6.2.4-beta.2",
5
5
  "author": "OneBlink <developers@oneblink.io> (https://oneblink.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/oneblink/apps-react/issues"
@@ -46,7 +46,7 @@
46
46
  "@mui/lab": "^5.0.0-alpha.152",
47
47
  "@mui/material": "^5.15.6",
48
48
  "@mui/x-date-pickers": "^6.19.2",
49
- "@oneblink/apps": "^13.0.0-beta.5",
49
+ "@oneblink/apps": "^14.1.0-beta.1",
50
50
  "@oneblink/release-cli": "^3.2.1",
51
51
  "@oneblink/types": "github:oneblink/types",
52
52
  "@types/blueimp-load-image": "^5.16.6",