Skip to content

Commit

Permalink
refactor: expose URLSearchParams directly
Browse files Browse the repository at this point in the history
  • Loading branch information
TylorS committed May 19, 2024
1 parent b78d7c1 commit b0a0429
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/realworld/src/ui/components/Pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { html, Link, many, RefSubject } from "@typed/core"
import { CurrentSearchParams } from "@typed/router"

export function Pagination<E, R>(pageSize: number, count: RefSubject.Computed<number, E, R>) {
const currentPage = RefSubject.map(CurrentSearchParams, (params) => Number(params.page ?? 1))
const currentPage = RefSubject.map(CurrentSearchParams, (params) => Number(params.get("page") ?? 1))
const pages = RefSubject.map(count, (count) => Array.from({ length: Math.ceil(count / pageSize) }, (_, i) => i + 1))

return html`<ul class="pagination">
Expand Down
2 changes: 1 addition & 1 deletion examples/realworld/src/ui/pages/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const main = (params: RefSubject.RefSubject<Params>) =>
const profile = RefSubject.proxy(ref)
const profileImage = RefSubject.map(profile.image, Option.getOrElse(() => ""))
const profileBio = RefSubject.map(profile.bio, Option.getOrElse(() => ""))
const currentPage = RefSubject.map(CurrentSearchParams, (params) => Number(params.page ?? 1))
const currentPage = RefSubject.map(CurrentSearchParams, (params) => Number(params.get("page") ?? 1))
const articlesAndCount = RefSubject.mapEffect(
RefSubject.tuple([Router.isActive(favoritesRoute), profile.username, currentPage]),
([favorites, username, page]) =>
Expand Down
5 changes: 2 additions & 3 deletions packages/router/src/CurrentRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,12 @@ function getBasePathname(base: string): string {
export const server = (base: string = "/"): Layer.Layer<CurrentRoute> =>
CurrentRoute.layer({ route: Route.parse(base), parent: Option.none() })

const getSearchParams = (destination: Destination): Readonly<Record<string, string>> =>
Object.fromEntries(destination.url.searchParams)
const getSearchParams = (destination: Destination) => destination.url.searchParams

/**
* @since 1.0.0
*/
export const CurrentSearchParams: RefSubject.Computed<Readonly<Record<string, string>>, never, Navigation> = RefSubject
export const CurrentSearchParams: RefSubject.Computed<URLSearchParams, never, Navigation> = RefSubject
.map(CurrentEntry, getSearchParams)

/**
Expand Down

0 comments on commit b0a0429

Please sign in to comment.