Skip to content

Commit 806c22e

Browse files
authored
fix(next): properly closes leave-without-saving modal after navigating from Leave anyway button (#7661)
1 parent 39d7b71 commit 806c22e

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

packages/next/src/elements/LeaveWithoutSaving/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const Component: React.FC<{
5050
}
5151

5252
export const LeaveWithoutSaving: React.FC = () => {
53+
const { closeModal } = useModal()
5354
const modified = useFormModified()
5455
const { user } = useAuth()
5556
const [show, setShow] = React.useState(false)
@@ -61,7 +62,11 @@ export const LeaveWithoutSaving: React.FC = () => {
6162
setShow(true)
6263
}, [])
6364

64-
usePreventLeave({ hasAccepted, onPrevent, prevent })
65+
const handleAccept = useCallback(() => {
66+
closeModal(modalSlug)
67+
}, [closeModal])
68+
69+
usePreventLeave({ hasAccepted, onAccept: handleAccept, onPrevent, prevent })
6570

6671
return (
6772
<Component

packages/next/src/elements/LeaveWithoutSaving/usePreventLeave.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ export const useBeforeUnload = (enabled: (() => boolean) | boolean = true, messa
5757
export const usePreventLeave = ({
5858
hasAccepted = false,
5959
message = 'Are you sure want to leave this page?',
60+
onAccept,
6061
onPrevent,
6162
prevent = true,
6263
}: {
6364
hasAccepted: boolean
6465
// if no `onPrevent` is provided, the message will be displayed in a confirm dialog
6566
message?: string
67+
onAccept?: () => void
6668
// to use a custom confirmation dialog, provide a function that returns a boolean
6769
onPrevent?: () => void
6870
prevent: boolean
@@ -142,7 +144,8 @@ export const usePreventLeave = ({
142144

143145
useEffect(() => {
144146
if (hasAccepted && cancelledURL.current) {
147+
if (onAccept) onAccept()
145148
router.push(cancelledURL.current)
146149
}
147-
}, [hasAccepted, router])
150+
}, [hasAccepted, onAccept, router])
148151
}

test/admin/e2e/1/e2e.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,30 @@ describe('admin1', () => {
10231023
await page.locator('.doc-controls__popup >> .popup-button').click()
10241024
await expect(page.locator('#action-duplicate')).toBeHidden()
10251025
})
1026+
1027+
test('should properly close leave-without-saving modal after clicking leave-anyway button', async () => {
1028+
const { id } = await createPost()
1029+
await page.goto(postsUrl.edit(id))
1030+
const title = 'title'
1031+
await page.locator('#field-title').fill(title)
1032+
await saveDocHotkeyAndAssert(page)
1033+
await expect(page.locator('#field-title')).toHaveValue(title)
1034+
1035+
const newTitle = 'new title'
1036+
await page.locator('#field-title').fill(newTitle)
1037+
1038+
await page.locator('header.app-header a[href="/admin/collections/posts"]').click()
1039+
1040+
// Locate the modal container
1041+
const modalContainer = page.locator('.payload__modal-container')
1042+
await expect(modalContainer).toBeVisible()
1043+
1044+
// Click the "Leave anyway" button
1045+
await page.locator('.leave-without-saving__controls .btn--style-primary').click()
1046+
1047+
// Assert that the class on the modal container changes to 'payload__modal-container--exitDone'
1048+
await expect(modalContainer).toHaveClass(/payload__modal-container--exitDone/)
1049+
})
10261050
})
10271051

10281052
describe('custom IDs', () => {

0 commit comments

Comments
 (0)