-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Ingest Pipelines] Add unsaved changes prompt (#183699)
- Loading branch information
1 parent
1ec9412
commit 1e197cf
Showing
23 changed files
with
421 additions
and
6 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# @kbn/unsaved-changes-prompt | ||
|
||
The useUnsavedChangesPrompt function is a custom React hook that prompts users with | ||
a confirmation dialog when they try to leave a page with unsaved changes. It blocks | ||
navigation and shows a dialog using the provided openConfirm function. If the user | ||
confirms, it navigates away; otherwise, it cancels the navigation, ensuring unsaved | ||
changes are not lost. | ||
|
||
|
||
```typescript | ||
import { useUnsavedChangesPrompt } from '@kbn/unsaved-changes-prompt'; | ||
|
||
export const SampleForm = ({ servicesForUnsavedChangesPrompt }) => { | ||
const { form } = useForm(); | ||
const isFormDirty = useFormIsModified({ form }); | ||
|
||
useUnsavedChangesPrompt({ | ||
hasUnsavedChanges: isFormDirty, | ||
...servicesForUnsavedChangesPrompt, | ||
}); | ||
|
||
return ( | ||
<> | ||
<Form form={form}> | ||
.... | ||
</Form> | ||
</> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { useUnsavedChangesPrompt } from './src/unsaved_changes_prompt'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../..', | ||
roots: ['<rootDir>/packages/kbn-unsaved-changes-prompt'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/unsaved-changes-prompt", | ||
"owner": "@elastic/kibana-management" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "@kbn/unsaved-changes-prompt", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "SSPL-1.0 OR Elastic License 2.0" | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { useUnsavedChangesPrompt } from './unsaved_changes_prompt'; |
65 changes: 65 additions & 0 deletions
65
...ges/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/unsaved_changes_prompt.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { createMemoryHistory } from 'history'; | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
|
||
import { coreMock } from '@kbn/core/public/mocks'; | ||
import { CoreScopedHistory } from '@kbn/core/public'; | ||
|
||
import { useUnsavedChangesPrompt } from './unsaved_changes_prompt'; | ||
|
||
const basePath = '/mock'; | ||
const memoryHistory = createMemoryHistory({ initialEntries: [basePath] }); | ||
const history = new CoreScopedHistory(memoryHistory, basePath); | ||
const coreStart = coreMock.createStart(); | ||
const navigateToUrl = jest.fn().mockImplementation(async (url) => { | ||
history.push(url); | ||
}); | ||
|
||
describe('useUnsavedChangesPrompt', () => { | ||
it('should not block if not edited', () => { | ||
renderHook(() => | ||
useUnsavedChangesPrompt({ | ||
hasUnsavedChanges: false, | ||
http: coreStart.http, | ||
openConfirm: coreStart.overlays.openConfirm, | ||
history, | ||
navigateToUrl, | ||
}) | ||
); | ||
|
||
act(() => history.push('/test')); | ||
|
||
expect(history.location.pathname).toBe('/test'); | ||
expect(history.location.search).toBe(''); | ||
expect(coreStart.overlays.openConfirm).not.toBeCalled(); | ||
}); | ||
|
||
it('should block if edited', async () => { | ||
coreStart.overlays.openConfirm.mockResolvedValue(true); | ||
|
||
renderHook(() => | ||
useUnsavedChangesPrompt({ | ||
hasUnsavedChanges: true, | ||
http: coreStart.http, | ||
openConfirm: coreStart.overlays.openConfirm, | ||
history, | ||
navigateToUrl, | ||
}) | ||
); | ||
|
||
act(() => history.push('/test')); | ||
|
||
// needed because we have an async useEffect | ||
await act(() => new Promise((resolve) => resolve())); | ||
|
||
expect(navigateToUrl).toBeCalledWith('/mock/test', expect.anything()); | ||
expect(coreStart.overlays.openConfirm).toBeCalled(); | ||
}); | ||
}); |
96 changes: 96 additions & 0 deletions
96
packages/kbn-unsaved-changes-prompt/src/unsaved_changes_prompt/unsaved_changes_prompt.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { useEffect } from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { ApplicationStart, ScopedHistory, OverlayStart, HttpStart } from '@kbn/core/public'; | ||
|
||
const DEFAULT_BODY_TEXT = i18n.translate('unsavedChangesPrompt.defaultModalText', { | ||
defaultMessage: `The data will be lost if you leave this page without saving the changes.`, | ||
}); | ||
|
||
const DEFAULT_TITLE_TEXT = i18n.translate('unsavedChangesPrompt.defaultModalTitle', { | ||
defaultMessage: 'Discard unsaved changes?', | ||
}); | ||
|
||
const DEFAULT_CANCEL_BUTTON = i18n.translate('unsavedChangesPrompt.defaultModalCancel', { | ||
defaultMessage: 'Keep editing', | ||
}); | ||
|
||
const DEFAULT_CONFIRM_BUTTON = i18n.translate('unsavedChangesPrompt.defaultModalConfirm', { | ||
defaultMessage: 'Leave page', | ||
}); | ||
|
||
interface Props { | ||
hasUnsavedChanges: boolean; | ||
http: HttpStart; | ||
openConfirm: OverlayStart['openConfirm']; | ||
history: ScopedHistory; | ||
navigateToUrl: ApplicationStart['navigateToUrl']; | ||
titleText?: string; | ||
messageText?: string; | ||
cancelButtonText?: string; | ||
confirmButtonText?: string; | ||
} | ||
|
||
export const useUnsavedChangesPrompt = ({ | ||
hasUnsavedChanges, | ||
openConfirm, | ||
history, | ||
http, | ||
navigateToUrl, | ||
// Provide overrides for confirm dialog | ||
messageText = DEFAULT_BODY_TEXT, | ||
titleText = DEFAULT_TITLE_TEXT, | ||
confirmButtonText = DEFAULT_CONFIRM_BUTTON, | ||
cancelButtonText = DEFAULT_CANCEL_BUTTON, | ||
}: Props) => { | ||
useEffect(() => { | ||
if (!hasUnsavedChanges) { | ||
return; | ||
} | ||
|
||
const unblock = history.block((state) => { | ||
async function confirmAsync() { | ||
const confirmResponse = await openConfirm(messageText, { | ||
title: titleText, | ||
cancelButtonText, | ||
confirmButtonText, | ||
'data-test-subj': 'navigationBlockConfirmModal', | ||
}); | ||
|
||
if (confirmResponse) { | ||
// Compute the URL we want to redirect to | ||
const url = http.basePath.prepend(state.pathname) + state.hash + state.search; | ||
// Unload history block | ||
unblock(); | ||
// Navigate away | ||
navigateToUrl(url, { | ||
state: state.state, | ||
}); | ||
} | ||
} | ||
|
||
confirmAsync(); | ||
return false; | ||
}); | ||
|
||
return unblock; | ||
}, [ | ||
history, | ||
hasUnsavedChanges, | ||
openConfirm, | ||
navigateToUrl, | ||
http.basePath, | ||
titleText, | ||
cancelButtonText, | ||
confirmButtonText, | ||
messageText, | ||
]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types" | ||
}, | ||
"include": ["*.ts", "src/**/*"], | ||
"exclude": [ | ||
"target/**/*" | ||
], | ||
"kbn_references": [ | ||
"@kbn/i18n", | ||
"@kbn/core" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.