@oneblink/apps-react 5.14.0 → 6.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
|
|
1
1
|
import * as React from 'react';
|
2
|
-
import {
|
2
|
+
import { draftService } from '@oneblink/apps';
|
3
3
|
/** The value returned from `useDrafts()` hook */
|
4
4
|
export type DraftsContextValue = {
|
5
5
|
/** `true` drafts are currently loading. */
|
@@ -7,21 +7,26 @@ export type DraftsContextValue = {
|
|
7
7
|
/** An Error object if loading drafts fails */
|
8
8
|
loadError: Error | null;
|
9
9
|
/** The incomplete submissions that were saved for later */
|
10
|
-
drafts:
|
10
|
+
drafts: draftService.LocalFormSubmissionDraft[];
|
11
11
|
/** A function to trigger loading of the drafts */
|
12
12
|
reloadDrafts: () => Promise<void>;
|
13
13
|
/** A function to clear Error object from loading drafts */
|
14
14
|
clearLoadError: () => void;
|
15
15
|
/** `true` drafts are syncing with other devices */
|
16
16
|
isSyncing: boolean;
|
17
|
+
/**
|
18
|
+
* The date when the sync process last completed successfully, will be `null`
|
19
|
+
* until it has completed the first time.
|
20
|
+
*/
|
21
|
+
lastSyncTime: Date | null;
|
17
22
|
/** A function to trigger syncing of the drafts */
|
18
|
-
syncDrafts: () => Promise<void>;
|
23
|
+
syncDrafts: (abortSignal: AbortSignal | undefined) => Promise<void>;
|
19
24
|
/** An Error object if syncing drafts fails */
|
20
25
|
syncError: Error | null;
|
21
26
|
/** A function to clear Error object from syncing drafts */
|
22
27
|
clearSyncError: () => void;
|
23
28
|
/** A function to remove a draft */
|
24
|
-
deleteDraft: (
|
29
|
+
deleteDraft: (formSubmissionDraftId: string) => Promise<void>;
|
25
30
|
};
|
26
31
|
/**
|
27
32
|
* React Component that provides the context for the `useDrafts()` hook to be
|
package/dist/hooks/useDrafts.js
CHANGED
@@ -9,6 +9,7 @@ const defaultLoadState = {
|
|
9
9
|
drafts: [],
|
10
10
|
};
|
11
11
|
const defaultSyncState = {
|
12
|
+
lastSyncTime: null,
|
12
13
|
isSyncing: false,
|
13
14
|
syncError: null,
|
14
15
|
};
|
@@ -68,21 +69,23 @@ children, }) {
|
|
68
69
|
syncError: null,
|
69
70
|
}));
|
70
71
|
}, []);
|
71
|
-
const syncDrafts = React.useCallback(async () => {
|
72
|
+
const syncDrafts = React.useCallback(async (abortSignal) => {
|
72
73
|
if (!isDraftsEnabled || !isLoggedIn || isUsingFormsKey) {
|
73
74
|
return;
|
74
75
|
}
|
75
76
|
if (isMounted.current) {
|
76
|
-
setSyncState({
|
77
|
+
setSyncState((currentState) => ({
|
78
|
+
...currentState,
|
77
79
|
isSyncing: true,
|
78
80
|
syncError: null,
|
79
|
-
});
|
81
|
+
}));
|
80
82
|
}
|
81
83
|
let newError = null;
|
82
84
|
try {
|
83
85
|
await draftService.syncDrafts({
|
84
86
|
formsAppId,
|
85
87
|
throwError: false,
|
88
|
+
abortSignal,
|
86
89
|
});
|
87
90
|
}
|
88
91
|
catch (error) {
|
@@ -90,6 +93,7 @@ children, }) {
|
|
90
93
|
}
|
91
94
|
if (isMounted.current) {
|
92
95
|
setSyncState({
|
96
|
+
lastSyncTime: new Date(),
|
93
97
|
isSyncing: false,
|
94
98
|
syncError: newError,
|
95
99
|
});
|
@@ -144,6 +148,7 @@ children, }) {
|
|
144
148
|
syncDrafts,
|
145
149
|
isSyncing: syncState.isSyncing,
|
146
150
|
syncError: syncState.syncError,
|
151
|
+
lastSyncTime: syncState.lastSyncTime,
|
147
152
|
clearSyncError,
|
148
153
|
// Load
|
149
154
|
reloadDrafts,
|
@@ -154,18 +159,20 @@ children, }) {
|
|
154
159
|
// Delete,
|
155
160
|
deleteDraft,
|
156
161
|
}), [
|
157
|
-
clearLoadError,
|
158
|
-
clearSyncError,
|
159
|
-
loadState.drafts,
|
160
|
-
loadState.isLoading,
|
161
|
-
loadState.loadError,
|
162
|
-
reloadDrafts,
|
163
162
|
syncDrafts,
|
164
163
|
syncState.isSyncing,
|
165
164
|
syncState.syncError,
|
165
|
+
syncState.lastSyncTime,
|
166
|
+
clearSyncError,
|
167
|
+
reloadDrafts,
|
168
|
+
loadState.isLoading,
|
169
|
+
loadState.drafts,
|
170
|
+
loadState.loadError,
|
171
|
+
clearLoadError,
|
166
172
|
deleteDraft,
|
167
173
|
]);
|
168
174
|
React.useEffect(() => {
|
175
|
+
const abortController = new AbortController();
|
169
176
|
reloadDrafts();
|
170
177
|
const unregisterPendingQueueListener = submissionService.registerPendingQueueListener(reloadDrafts);
|
171
178
|
const unregisterDraftsListener = draftService.registerDraftsListener((drafts) => {
|
@@ -176,13 +183,18 @@ children, }) {
|
|
176
183
|
});
|
177
184
|
});
|
178
185
|
return () => {
|
186
|
+
abortController.abort();
|
179
187
|
unregisterPendingQueueListener();
|
180
188
|
unregisterDraftsListener();
|
181
189
|
};
|
182
190
|
}, [reloadDrafts]);
|
183
191
|
React.useEffect(() => {
|
184
192
|
if (!isOffline) {
|
185
|
-
|
193
|
+
const abortController = new AbortController();
|
194
|
+
syncDrafts(abortController.signal);
|
195
|
+
return () => {
|
196
|
+
abortController.abort();
|
197
|
+
};
|
186
198
|
}
|
187
199
|
}, [isOffline, syncDrafts]);
|
188
200
|
return (React.createElement(DraftsContext.Provider, { value: value }, children));
|
@@ -1 +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,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B,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;;;;;;GAMG;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: () => Promise<void>\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: () => Promise<void>\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"]}
|
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;AAChE,OAAO,OAAO,MAAM,WAAW,CAAA;AAC/B,OAAO,YAAY,MAAM,gBAAgB,CAAA;AACzC,OAAO,YAAY,MAAM,gBAAgB,CAAA;AA+BzC,MAAM,gBAAgB,GAAG;IACvB,SAAS,EAAE,KAAK;IAChB,SAAS,EAAE,IAAI;IACf,MAAM,EAAE,EAAE;CACX,CAAA;AAED,MAAM,gBAAgB,GAAG;IACvB,YAAY,EAAE,IAAI;IAClB,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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,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAA;IAElB,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,IAAI,CAAC,SAAS,EAAE;YACd,MAAM,eAAe,GAAG,IAAI,eAAe,EAAE,CAAA;YAC7C,UAAU,CAAC,eAAe,CAAC,MAAM,CAAC,CAAA;YAClC,OAAO,GAAG,EAAE;gBACV,eAAe,CAAC,KAAK,EAAE,CAAA;YACzB,CAAC,CAAA;SACF;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;;;;;;GAMG;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 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: draftService.LocalFormSubmissionDraft[]\n /** A function to trigger loading of the drafts */\n reloadDrafts: () => Promise<void>\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 /**\n * The date when the sync process last completed successfully, will be `null`\n * until it has completed the first time.\n */\n lastSyncTime: Date | null\n /** A function to trigger syncing of the drafts */\n syncDrafts: (abortSignal: AbortSignal | undefined) => Promise<void>\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: (formSubmissionDraftId: string) => Promise<void>\n}\n\nconst defaultLoadState = {\n isLoading: false,\n loadError: null,\n drafts: [],\n}\n\nconst defaultSyncState = {\n lastSyncTime: null,\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 lastSyncTime: Date | null\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(\n async (abortSignal: AbortSignal | undefined) => {\n if (!isDraftsEnabled || !isLoggedIn || isUsingFormsKey) {\n return\n }\n\n if (isMounted.current) {\n setSyncState((currentState) => ({\n ...currentState,\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 abortSignal,\n })\n } catch (error) {\n newError = error as Error\n }\n\n if (isMounted.current) {\n setSyncState({\n lastSyncTime: new Date(),\n isSyncing: false,\n syncError: newError,\n })\n }\n },\n [formsAppId, isDraftsEnabled, isLoggedIn, isMounted, isUsingFormsKey],\n )\n\n const [loadState, setLoadState] = React.useState<{\n isLoading: boolean\n loadError: Error | null\n drafts: draftService.LocalFormSubmissionDraft[]\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: draftService.LocalFormSubmissionDraft[] = []\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<DraftsContextValue>(\n () => ({\n // Sync\n syncDrafts,\n isSyncing: syncState.isSyncing,\n syncError: syncState.syncError,\n lastSyncTime: syncState.lastSyncTime,\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 syncDrafts,\n syncState.isSyncing,\n syncState.syncError,\n syncState.lastSyncTime,\n clearSyncError,\n reloadDrafts,\n loadState.isLoading,\n loadState.drafts,\n loadState.loadError,\n clearLoadError,\n deleteDraft,\n ],\n )\n\n React.useEffect(() => {\n const abortController = new AbortController()\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 abortController.abort()\n unregisterPendingQueueListener()\n unregisterDraftsListener()\n }\n }, [reloadDrafts])\n\n React.useEffect(() => {\n if (!isOffline) {\n const abortController = new AbortController()\n syncDrafts(abortController.signal)\n return () => {\n abortController.abort()\n }\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"]}
|
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": "
|
4
|
+
"version": "6.0.0-beta.1",
|
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": "^
|
49
|
+
"@oneblink/apps": "^12.0.0-beta.1",
|
50
50
|
"@oneblink/release-cli": "^3.1.0",
|
51
51
|
"@oneblink/types": "github:oneblink/types",
|
52
52
|
"@types/blueimp-load-image": "^5.16.6",
|