@oneblink/apps-react 5.14.0 → 6.0.0-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.
- package/dist/hooks/useDrafts.d.ts +10 -11
- package/dist/hooks/useDrafts.js +28 -76
- package/dist/hooks/useDrafts.js.map +1 -1
- package/package.json +2 -2
@@ -1,27 +1,26 @@
|
|
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
|
-
/** `true` drafts are currently loading. */
|
5
|
+
/** `true` if drafts are currently loading for the first time. */
|
6
6
|
isLoading: boolean;
|
7
|
-
/** An Error object if loading drafts fails */
|
8
|
-
loadError: Error | null;
|
9
7
|
/** The incomplete submissions that were saved for later */
|
10
|
-
drafts:
|
11
|
-
/** A function to trigger loading of the drafts */
|
12
|
-
reloadDrafts: () => Promise<void>;
|
13
|
-
/** A function to clear Error object from loading drafts */
|
14
|
-
clearLoadError: () => void;
|
8
|
+
drafts: draftService.LocalFormSubmissionDraft[];
|
15
9
|
/** `true` drafts are syncing with other devices */
|
16
10
|
isSyncing: boolean;
|
11
|
+
/**
|
12
|
+
* The date when the sync process last completed successfully, will be `null`
|
13
|
+
* until it has completed the first time.
|
14
|
+
*/
|
15
|
+
lastSyncTime: Date | null;
|
17
16
|
/** A function to trigger syncing of the drafts */
|
18
|
-
syncDrafts: () => Promise<void>;
|
17
|
+
syncDrafts: (abortSignal: AbortSignal | undefined) => Promise<void>;
|
19
18
|
/** An Error object if syncing drafts fails */
|
20
19
|
syncError: Error | null;
|
21
20
|
/** A function to clear Error object from syncing drafts */
|
22
21
|
clearSyncError: () => void;
|
23
22
|
/** A function to remove a draft */
|
24
|
-
deleteDraft: (
|
23
|
+
deleteDraft: (formSubmissionDraftId: string) => Promise<void>;
|
25
24
|
};
|
26
25
|
/**
|
27
26
|
* React Component that provides the context for the `useDrafts()` hook to be
|
package/dist/hooks/useDrafts.js
CHANGED
@@ -3,15 +3,6 @@ import { draftService, submissionService } from '@oneblink/apps';
|
|
3
3
|
import useAuth from './useAuth';
|
4
4
|
import useIsMounted from './useIsMounted';
|
5
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
6
|
const DraftsContext = React.createContext(undefined);
|
16
7
|
/**
|
17
8
|
* React Component that provides the context for the `useDrafts()` hook to be
|
@@ -61,28 +52,34 @@ children, }) {
|
|
61
52
|
const isMounted = useIsMounted();
|
62
53
|
const isOffline = useIsOffline();
|
63
54
|
const { isLoggedIn, isUsingFormsKey } = useAuth();
|
64
|
-
const [syncState, setSyncState] = React.useState(
|
55
|
+
const [syncState, setSyncState] = React.useState({
|
56
|
+
lastSyncTime: null,
|
57
|
+
isSyncing: false,
|
58
|
+
syncError: null,
|
59
|
+
});
|
65
60
|
const clearSyncError = React.useCallback(() => {
|
66
61
|
setSyncState((currentState) => ({
|
67
62
|
...currentState,
|
68
63
|
syncError: null,
|
69
64
|
}));
|
70
65
|
}, []);
|
71
|
-
const syncDrafts = React.useCallback(async () => {
|
66
|
+
const syncDrafts = React.useCallback(async (abortSignal) => {
|
72
67
|
if (!isDraftsEnabled || !isLoggedIn || isUsingFormsKey) {
|
73
68
|
return;
|
74
69
|
}
|
75
70
|
if (isMounted.current) {
|
76
|
-
setSyncState({
|
71
|
+
setSyncState((currentState) => ({
|
72
|
+
...currentState,
|
77
73
|
isSyncing: true,
|
78
74
|
syncError: null,
|
79
|
-
});
|
75
|
+
}));
|
80
76
|
}
|
81
77
|
let newError = null;
|
82
78
|
try {
|
83
79
|
await draftService.syncDrafts({
|
84
80
|
formsAppId,
|
85
81
|
throwError: false,
|
82
|
+
abortSignal,
|
86
83
|
});
|
87
84
|
}
|
88
85
|
catch (error) {
|
@@ -90,52 +87,13 @@ children, }) {
|
|
90
87
|
}
|
91
88
|
if (isMounted.current) {
|
92
89
|
setSyncState({
|
90
|
+
lastSyncTime: new Date(),
|
93
91
|
isSyncing: false,
|
94
92
|
syncError: newError,
|
95
93
|
});
|
96
94
|
}
|
97
95
|
}, [formsAppId, isDraftsEnabled, isLoggedIn, isMounted, isUsingFormsKey]);
|
98
|
-
const [
|
99
|
-
const clearLoadError = React.useCallback(() => {
|
100
|
-
setLoadState((currentState) => ({
|
101
|
-
...currentState,
|
102
|
-
loadError: null,
|
103
|
-
}));
|
104
|
-
}, []);
|
105
|
-
const reloadDrafts = React.useCallback(async () => {
|
106
|
-
if (!isLoggedIn) {
|
107
|
-
if (isMounted.current) {
|
108
|
-
setLoadState({
|
109
|
-
isLoading: false,
|
110
|
-
loadError: null,
|
111
|
-
drafts: [],
|
112
|
-
});
|
113
|
-
}
|
114
|
-
return;
|
115
|
-
}
|
116
|
-
if (isMounted.current) {
|
117
|
-
setLoadState((currentState) => ({
|
118
|
-
isLoading: true,
|
119
|
-
loadError: null,
|
120
|
-
drafts: currentState.drafts,
|
121
|
-
}));
|
122
|
-
}
|
123
|
-
let newError = null;
|
124
|
-
let newDrafts = [];
|
125
|
-
try {
|
126
|
-
newDrafts = await draftService.getDrafts();
|
127
|
-
}
|
128
|
-
catch (error) {
|
129
|
-
newError = error;
|
130
|
-
}
|
131
|
-
if (isMounted.current) {
|
132
|
-
setLoadState({
|
133
|
-
isLoading: false,
|
134
|
-
loadError: newError,
|
135
|
-
drafts: newDrafts,
|
136
|
-
});
|
137
|
-
}
|
138
|
-
}, [isMounted, isLoggedIn]);
|
96
|
+
const [drafts, setDrafts] = React.useState(null);
|
139
97
|
const deleteDraft = React.useCallback((draftId) => {
|
140
98
|
return draftService.deleteDraft(draftId, formsAppId);
|
141
99
|
}, [formsAppId]);
|
@@ -144,45 +102,39 @@ children, }) {
|
|
144
102
|
syncDrafts,
|
145
103
|
isSyncing: syncState.isSyncing,
|
146
104
|
syncError: syncState.syncError,
|
105
|
+
lastSyncTime: syncState.lastSyncTime,
|
147
106
|
clearSyncError,
|
148
107
|
// Load
|
149
|
-
|
150
|
-
|
151
|
-
drafts: loadState.drafts,
|
152
|
-
loadError: loadState.loadError,
|
153
|
-
clearLoadError,
|
108
|
+
isLoading: !drafts,
|
109
|
+
drafts: drafts || [],
|
154
110
|
// Delete,
|
155
111
|
deleteDraft,
|
156
112
|
}), [
|
157
|
-
clearLoadError,
|
158
|
-
clearSyncError,
|
159
|
-
loadState.drafts,
|
160
|
-
loadState.isLoading,
|
161
|
-
loadState.loadError,
|
162
|
-
reloadDrafts,
|
163
113
|
syncDrafts,
|
164
114
|
syncState.isSyncing,
|
165
115
|
syncState.syncError,
|
116
|
+
syncState.lastSyncTime,
|
117
|
+
clearSyncError,
|
118
|
+
drafts,
|
166
119
|
deleteDraft,
|
167
120
|
]);
|
168
121
|
React.useEffect(() => {
|
169
|
-
|
170
|
-
const unregisterPendingQueueListener = submissionService.registerPendingQueueListener(
|
171
|
-
const unregisterDraftsListener = draftService.registerDraftsListener(
|
172
|
-
setLoadState({
|
173
|
-
isLoading: false,
|
174
|
-
drafts,
|
175
|
-
loadError: null,
|
176
|
-
});
|
177
|
-
});
|
122
|
+
const abortController = new AbortController();
|
123
|
+
const unregisterPendingQueueListener = submissionService.registerPendingQueueListener(() => syncDrafts(undefined));
|
124
|
+
const unregisterDraftsListener = draftService.registerDraftsListener(setDrafts);
|
178
125
|
return () => {
|
126
|
+
abortController.abort();
|
179
127
|
unregisterPendingQueueListener();
|
180
128
|
unregisterDraftsListener();
|
181
129
|
};
|
182
|
-
}, [
|
130
|
+
}, [syncDrafts]);
|
183
131
|
React.useEffect(() => {
|
184
132
|
if (!isOffline) {
|
185
|
-
|
133
|
+
const abortController = new AbortController();
|
134
|
+
syncDrafts(abortController.signal);
|
135
|
+
return () => {
|
136
|
+
abortController.abort();
|
137
|
+
};
|
186
138
|
}
|
187
139
|
}, [isOffline, syncDrafts]);
|
188
140
|
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,gBAAgB,GAAG;IACvB,SAAS,EAAE,KAAK;IAChB,SAAS,EAAE,IAAI;CAChB,CAAA;AAED,MAAM,aAAa,GAAG,KAAK,CAAC,aAAa,CACvC,SAAS,CACV,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,UAAU,qBAAqB,CAAC;AACpC,yEAAyE;AACzE,UAAU;AACV;;;GAGG;AACH,eAAe;AACf,kCAAkC;AAClC,QAAQ,GAKT;IACC,MAAM,SAAS,GAAG,YAAY,EAAE,CAAA;IAChC,MAAM,SAAS,GAAG,YAAY,EAAE,CAAA;IAChC,MAAM,EAAE,UAAU,EAAE,eAAe,EAAE,GAAG,OAAO,EAAE,CAAA;IAEjD,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,KAAK,CAAC,QAAQ,CAG7C,gBAAgB,CAAC,CAAA;IACpB,MAAM,cAAc,GAAG,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE;QAC5C,YAAY,CAAC,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;YAC9B,GAAG,YAAY;YACf,SAAS,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":["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;AAyBzC,MAAM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CAClD,UAAU,CAAC,SAAS,CAAC,CACtB,CAAA;QACH,MAAM,wBAAwB,GAC5B,YAAY,CAAC,sBAAsB,CAAC,SAAS,CAAC,CAAA;QAChD,OAAO,GAAG,EAAE;YACV,eAAe,CAAC,KAAK,EAAE,CAAA;YACvB,8BAA8B,EAAE,CAAA;YAChC,wBAAwB,EAAE,CAAA;QAC5B,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAA;IAEhB,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` if drafts are currently loading for the first time. */\n isLoading: boolean\n /** The incomplete submissions that were saved for later */\n drafts: draftService.LocalFormSubmissionDraft[]\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 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 }>({\n lastSyncTime: null,\n isSyncing: false,\n syncError: null,\n })\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 [drafts, setDrafts] = React.useState<\n draftService.LocalFormSubmissionDraft[] | null\n >(null)\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 isLoading: !drafts,\n drafts: drafts || [],\n // Delete,\n deleteDraft,\n }),\n [\n syncDrafts,\n syncState.isSyncing,\n syncState.syncError,\n syncState.lastSyncTime,\n clearSyncError,\n drafts,\n deleteDraft,\n ],\n )\n\n React.useEffect(() => {\n const abortController = new AbortController()\n const unregisterPendingQueueListener =\n submissionService.registerPendingQueueListener(() =>\n syncDrafts(undefined),\n )\n const unregisterDraftsListener =\n draftService.registerDraftsListener(setDrafts)\n return () => {\n abortController.abort()\n unregisterPendingQueueListener()\n unregisterDraftsListener()\n }\n }, [syncDrafts])\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.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": "^
|
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",
|