Skip to content

Commit

Permalink
Allow determining whether beforeUnload() should block based on new …
Browse files Browse the repository at this point in the history
…URL (#27)

Co-authored-by: Marius Andra <[email protected]>
  • Loading branch information
Twixes and mariusandra authored Sep 18, 2022
1 parent 3f92b37 commit 65c6464
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
beforeUnmount,
setPluginContext,
} from 'kea'
import { combineUrl, decodeParams as decode, encodeParams as encode } from './utils'
import { CombinedLocation, combineUrl, decodeParams as decode, encodeParams as encode } from './utils'
import { routerType } from './routerType'
import { LocationChangedPayload, RouterPluginContext } from './types'

function preventUnload(): boolean {
function preventUnload(newLocation: CombinedLocation): boolean {
// We only check the last reference for unloading. Generally there should only be one loaded anyway.
const { beforeUnloadInterceptors } = getRouterContext()

Expand All @@ -25,7 +25,7 @@ function preventUnload(): boolean {
}

for (const beforeUnload of Array.from(beforeUnloadInterceptors)) {
if (!beforeUnload.enabled()) {
if (!beforeUnload.enabled(newLocation)) {
continue
}

Expand Down Expand Up @@ -113,7 +113,7 @@ export const router = kea<routerType>([
const { history } = routerContext
const response = combineUrl(url, searchInput, hashInput)

if (preventUnload()) {
if (preventUnload(response)) {
return
}

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logic } from 'kea'
import { MutableRefObject } from 'react'
import { CombinedLocation } from 'utils'

export interface RouterPluginOptions {
history?: {
Expand All @@ -20,7 +20,7 @@ export interface RouterPluginContext extends RouterPluginOptions {
}

export interface RouterBeforeUnloadInterceptor {
enabled: () => boolean
enabled: (newLocation: CombinedLocation) => boolean
message: string
onConfirm?: () => void
}
Expand Down
18 changes: 10 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,22 @@ export function parsePath(
const _e = encodeParams
const _d = decodeParams

export function combineUrl(
url: string,
searchInput?: string | Record<string, any>,
hashInput?: string | Record<string, any>,
encodeParams: (obj: Record<string, any>, symbol: string) => string = getPluginContext('router').encodeParams || _e,
decodeParams: (input: string, symbol: string) => Record<string, any> = getPluginContext('router').decodeParams || _d,
): {
export interface CombinedLocation {
pathname: string
search: string
searchParams: Record<string, any>
hash: string
hashParams: Record<string, any>
url: string
} {
}

export function combineUrl(
url: string,
searchInput?: string | Record<string, any>,
hashInput?: string | Record<string, any>,
encodeParams: (obj: Record<string, any>, symbol: string) => string = getPluginContext('router').encodeParams || _e,
decodeParams: (input: string, symbol: string) => Record<string, any> = getPluginContext('router').decodeParams || _d,
): CombinedLocation {
const parsedPath = parsePath(url)

const response = {
Expand Down

0 comments on commit 65c6464

Please sign in to comment.