Skip to content

Commit

Permalink
Add patch support
Browse files Browse the repository at this point in the history
  • Loading branch information
carson-katri committed Dec 11, 2024
1 parent 0451c9f commit 975edb5
Showing 1 changed file with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ struct NavigationLink<Root: RootRegistry>: View {
@_documentation(visibility: public)
private var destination: String?

@LiveAttribute("data-phx-link")
private var link: Link = .redirect

enum Link: String, AttributeDecodable {
case redirect
case patch
}

@LiveAttribute("data-phx-link-state")
private var linkState: LinkState = .push

Expand Down Expand Up @@ -87,15 +95,57 @@ struct NavigationLink<Root: RootRegistry>: View {
} else {
nil
}
switch linkState {
case .replace:
switch link {
case .redirect:
switch linkState {
case .replace:
SwiftUI.Button {
Task { @MainActor in
try await $liveElement.context.coordinator.session.redirect(
.init(
kind: .replace,
to: url,
mode: .replaceTop
),
navigationTransition: anyNavigationTransition,
pendingView: pendingView
)
}
} label: {
$liveElement.children()
}
case .push:
SwiftUI.NavigationLink(
value: LiveNavigationEntry(
url: url,
coordinator: LiveViewCoordinator(session: $liveElement.context.coordinator.session, url: url),
navigationTransition: anyNavigationTransition,
pendingView: pendingView
)
) {
$liveElement.children()
}
}
case .patch:
SwiftUI.Button {
Task { @MainActor in
// send the `live_patch` event
try await $liveElement.context.coordinator.doPushEvent("live_patch", payload: [
"url": url.absoluteString
])
// update the navigation path
let kind: LiveRedirect.Kind = switch linkState {
case .push:
.push
case .replace:
.replace
}
print(kind)
try await $liveElement.context.coordinator.session.redirect(
.init(
kind: .replace,
kind: kind,
to: url,
mode: .replaceTop
mode: .patch
),
navigationTransition: anyNavigationTransition,
pendingView: pendingView
Expand All @@ -104,17 +154,6 @@ struct NavigationLink<Root: RootRegistry>: View {
} label: {
$liveElement.children()
}
case .push:
SwiftUI.NavigationLink(
value: LiveNavigationEntry(
url: url,
coordinator: LiveViewCoordinator(session: $liveElement.context.coordinator.session, url: url),
navigationTransition: anyNavigationTransition,
pendingView: pendingView
)
) {
$liveElement.children()
}
}
} else {
$liveElement.children()
Expand Down

0 comments on commit 975edb5

Please sign in to comment.