Skip to content

Commit

Permalink
refactor: Routes shouldn't know directly about auth
Browse files Browse the repository at this point in the history
  • Loading branch information
TylorS committed May 22, 2024
1 parent 419179f commit 0bc5bdb
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 13 deletions.
9 changes: 4 additions & 5 deletions examples/realworld/src/ui/common/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Route, Router } from "@typed/core"
import { Route } from "@typed/core"
import { ArticleSlug, Username } from "@typed/realworld/model"
import { isAuthenticatedGuard } from "@typed/realworld/services"

export const home = Route.home.concat(Route.queryParams({
tag: Route.param("tag").optional(),
Expand All @@ -12,14 +11,14 @@ export const article = Route.literal("article").concat(
Route.paramWithSchema("slug", ArticleSlug)
)

export const editor = Route.literal("editor").pipe(isAuthenticatedGuard)
export const editor = Route.literal("editor", { end: true })

export const editArticle = Router.concat(editor, Route.paramWithSchema("slug", ArticleSlug))
export const editArticle = Route.literal("editor").concat(Route.paramWithSchema("slug", ArticleSlug, { end: true }))

export const login = Route.literal("login")

export const profile = Route.literal("profile").concat(Route.paramWithSchema("username", Username))

export const register = Route.literal("register")

export const settings = Route.literal("settings").pipe(isAuthenticatedGuard)
export const settings = Route.literal("settings")
2 changes: 1 addition & 1 deletion examples/realworld/src/ui/pages/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function ArticleMeta<E, R>(ref: RefSubject.RefSubject<Article, E, R>) {
</button>`
})

const editArticleHref = RefSubject.map(article.slug, (slug) => Routes.editArticle.route.interpolate({ slug }))
const editArticleHref = RefSubject.map(article.slug, (slug) => Routes.editArticle.interpolate({ slug }))

const deleteArticle = Effect.gen(function*() {
const slug = yield* article.slug
Expand Down
4 changes: 2 additions & 2 deletions examples/realworld/src/ui/pages/edit-article.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Fx, RefSubject } from "@typed/core"
import { Articles } from "@typed/realworld/services"
import { Articles, isAuthenticatedGuard } from "@typed/realworld/services"
import * as Routes from "@typed/realworld/ui/common/routes"
import type { RouteGuard } from "@typed/router"
import { Effect } from "effect"
import { EditArticle } from "../components/EditArticle"

export const route = Routes.editArticle
export const route = Routes.editArticle.pipe(isAuthenticatedGuard)

export type Params = RouteGuard.RouteGuard.Success<typeof route>

Expand Down
4 changes: 2 additions & 2 deletions examples/realworld/src/ui/pages/editor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Fx, Router } from "@typed/core"
import { RefSubject } from "@typed/fx"
import { ArticleBody, ArticleDescription, ArticleTitle } from "@typed/realworld/model"
import { Articles } from "@typed/realworld/services"
import { Articles, isAuthenticatedGuard } from "@typed/realworld/services"
import * as Routes from "@typed/realworld/ui/common/routes"
import { Effect } from "effect"
import { EditArticle, type EditArticleFields } from "../components/EditArticle"

export const route = Routes.editor
export const route = Routes.editor.pipe(isAuthenticatedGuard)

export const main = Fx.gen(function*() {
const initial = yield* RefSubject.of<EditArticleFields>({
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 @@ -64,7 +64,7 @@ export const main = (params: RefSubject.RefSubject<Params>) =>
${
Link(
{
to: Routes.settings.route.interpolate({}),
to: Routes.settings.interpolate({}),
relative: false
},
html`<button
Expand Down
4 changes: 2 additions & 2 deletions examples/realworld/src/ui/pages/settings.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ArrayFormatter } from "@effect/schema"
import { AsyncData, Fx, RefAsyncData, RefSubject, Router } from "@typed/core"
import { parseFormData } from "@typed/realworld/lib/Schema"
import { CurrentUser, Users } from "@typed/realworld/services"
import { CurrentUser, isAuthenticatedGuard, Users } from "@typed/realworld/services"
import { Unprocessable } from "@typed/realworld/services/errors"
import { UpdateUserInput } from "@typed/realworld/services/UpdateUser"
import * as Routes from "@typed/realworld/ui/common/routes"
import { CurrentUserErrors } from "@typed/realworld/ui/services/CurrentUser"
import { EventHandler, html } from "@typed/template"
import { Effect, Option } from "effect"

export const route = Routes.settings
export const route = Routes.settings.pipe(isAuthenticatedGuard)

type SubmitEvent = Event & { target: HTMLFormElement }

Expand Down

0 comments on commit 0bc5bdb

Please sign in to comment.