@contember/interface 2.0.0-alpha.14 → 2.0.0-alpha.15
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/development/index.cjs +2 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +2 -0
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/bootstrap/ApplicationEntrypoint.cjs.map +1 -1
- package/dist/development/src/bootstrap/ApplicationEntrypoint.js.map +1 -1
- package/dist/development/src/hooks/useBlockNavigationOnDirtyState.cjs +36 -0
- package/dist/development/src/hooks/useBlockNavigationOnDirtyState.cjs.map +1 -0
- package/dist/development/src/hooks/useBlockNavigationOnDirtyState.js +36 -0
- package/dist/development/src/hooks/useBlockNavigationOnDirtyState.js.map +1 -0
- package/dist/production/index.cjs +2 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +2 -0
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/bootstrap/ApplicationEntrypoint.cjs.map +1 -1
- package/dist/production/src/bootstrap/ApplicationEntrypoint.js.map +1 -1
- package/dist/production/src/hooks/useBlockNavigationOnDirtyState.cjs +36 -0
- package/dist/production/src/hooks/useBlockNavigationOnDirtyState.cjs.map +1 -0
- package/dist/production/src/hooks/useBlockNavigationOnDirtyState.js +36 -0
- package/dist/production/src/hooks/useBlockNavigationOnDirtyState.js.map +1 -0
- package/dist/types/bootstrap/ApplicationEntrypoint.d.ts.map +1 -1
- package/dist/types/hooks/index.d.ts +1 -0
- package/dist/types/hooks/index.d.ts.map +1 -1
- package/dist/types/hooks/useBlockNavigationOnDirtyState.d.ts +7 -0
- package/dist/types/hooks/useBlockNavigationOnDirtyState.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -8
- package/src/bootstrap/ApplicationEntrypoint.tsx +1 -1
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useBlockNavigationOnDirtyState.ts +40 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/interface",
|
|
3
3
|
"license": "Apache-2.0",
|
|
4
|
-
"version": "2.0.0-alpha.
|
|
4
|
+
"version": "2.0.0-alpha.15",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "./dist/production/index.js",
|
|
@@ -28,13 +28,13 @@
|
|
|
28
28
|
"ae:test": "api-extractor run"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@contember/react-binding": "2.0.0-alpha.
|
|
32
|
-
"@contember/react-client": "2.0.0-alpha.
|
|
33
|
-
"@contember/react-dataview": "2.0.0-alpha.
|
|
34
|
-
"@contember/react-devbar": "2.0.0-alpha.
|
|
35
|
-
"@contember/react-identity": "2.0.0-alpha.
|
|
36
|
-
"@contember/react-routing": "2.0.0-alpha.
|
|
37
|
-
"@contember/utilities": "2.0.0-alpha.
|
|
31
|
+
"@contember/react-binding": "2.0.0-alpha.15",
|
|
32
|
+
"@contember/react-client": "2.0.0-alpha.15",
|
|
33
|
+
"@contember/react-dataview": "2.0.0-alpha.15",
|
|
34
|
+
"@contember/react-devbar": "2.0.0-alpha.15",
|
|
35
|
+
"@contember/react-identity": "2.0.0-alpha.15",
|
|
36
|
+
"@contember/react-routing": "2.0.0-alpha.15",
|
|
37
|
+
"@contember/utilities": "2.0.0-alpha.15",
|
|
38
38
|
"@radix-ui/react-slot": "^1.0.2",
|
|
39
39
|
"stacktracey": "^2.1.8"
|
|
40
40
|
},
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Environment, EnvironmentContext, EnvironmentExtensionProvider } from '@contember/react-binding'
|
|
2
2
|
import { ContemberClient, ContemberClientProps } from '@contember/react-client'
|
|
3
3
|
import { ReactNode } from 'react'
|
|
4
|
-
import {
|
|
4
|
+
import { RouteMap, RoutingContextValue, RoutingProvider } from '@contember/react-routing'
|
|
5
5
|
import { DataViewPageNameKeyProvider } from './DataViewPageNameKeyProvider'
|
|
6
6
|
import { IdentityEnvironmentProvider, IdentityProvider, projectEnvironmentExtension } from '@contember/react-identity'
|
|
7
7
|
|
package/src/hooks/index.ts
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ErrorPersistResult, SuccessfulPersistResult, useDirtinessState, useMutationState, usePersist } from '@contember/react-binding'
|
|
2
|
+
import { useCallback, useRef } from 'react'
|
|
3
|
+
import { useRegisterRequestChangeListener } from '@contember/react-routing'
|
|
4
|
+
|
|
5
|
+
export type BlockNavigationOnDirtyStateResult = 'save' | 'discard' | 'cancel'
|
|
6
|
+
|
|
7
|
+
export const useBlockNavigationOnDirtyState = (handler: () => Promise<BlockNavigationOnDirtyStateResult>, options?: {
|
|
8
|
+
onPersistSuccess?: (result: SuccessfulPersistResult) => void
|
|
9
|
+
onPersistError?: (result: ErrorPersistResult) => void
|
|
10
|
+
}) => {
|
|
11
|
+
const isDirty = useDirtinessState()
|
|
12
|
+
const isDirtyRef = useRef(isDirty)
|
|
13
|
+
isDirtyRef.current = isDirty
|
|
14
|
+
const isMutating = useMutationState()
|
|
15
|
+
const isMutatingRef = useRef(isMutating)
|
|
16
|
+
isMutatingRef.current = isMutating
|
|
17
|
+
const persist = usePersist()
|
|
18
|
+
|
|
19
|
+
useRegisterRequestChangeListener(useCallback(async event => {
|
|
20
|
+
if (!isDirtyRef.current || isMutatingRef.current) {
|
|
21
|
+
return
|
|
22
|
+
}
|
|
23
|
+
const result = await handler()
|
|
24
|
+
if (result === 'cancel') {
|
|
25
|
+
event.abortNavigation()
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (result === 'save') {
|
|
30
|
+
try {
|
|
31
|
+
const result = await persist()
|
|
32
|
+
options?.onPersistSuccess?.(result)
|
|
33
|
+
} catch (e: any) {
|
|
34
|
+
options?.onPersistError?.(e)
|
|
35
|
+
event.abortNavigation()
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}, [handler, options, persist]))
|
|
40
|
+
}
|