WIP: Cleans up ResultView, fixes creating a new trunk size after changes to the database client, pdf button shows html now.
This commit is contained in:
@@ -41,8 +41,9 @@ extension DatabaseClient.TrunkSizes: TestDependencyKey {
|
|||||||
type: request.type
|
type: request.type
|
||||||
)
|
)
|
||||||
try await model.save(on: database)
|
try await model.save(on: database)
|
||||||
// TODO: This `model.toDTO()` may not work now, need to check.
|
roomProxies.append(
|
||||||
try await roomProxies.append(model.toDTO())
|
.init(room: try room.toDTO(), registers: registers)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return try .init(
|
return try .init(
|
||||||
@@ -77,7 +78,7 @@ extension DatabaseClient.TrunkSizes: TestDependencyKey {
|
|||||||
else {
|
else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return try await model.toDTO()
|
return try model.toDTO()
|
||||||
},
|
},
|
||||||
update: { id, updates in
|
update: { id, updates in
|
||||||
guard
|
guard
|
||||||
|
|||||||
@@ -1,102 +1,76 @@
|
|||||||
import Elementary
|
import Elementary
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public struct ResultView<
|
public struct ResultView<ValueView, ErrorView>: HTML where ValueView: HTML, ErrorView: HTML {
|
||||||
V: Sendable,
|
|
||||||
E: Error,
|
|
||||||
ValueView: HTML,
|
|
||||||
ErrorView: HTML
|
|
||||||
>: HTML {
|
|
||||||
|
|
||||||
let onSuccess: @Sendable (V) -> ValueView
|
let result: Result<ValueView, any Error>
|
||||||
let onError: @Sendable (E) -> ErrorView
|
let errorView: @Sendable (any Error) -> ErrorView
|
||||||
let result: Result<V, E>
|
|
||||||
|
|
||||||
public init(
|
public init(
|
||||||
result: Result<V, E>,
|
_ content: @escaping @Sendable () async throws -> ValueView,
|
||||||
@HTMLBuilder onSuccess: @escaping @Sendable (V) -> ValueView,
|
onError: @escaping @Sendable (any Error) -> ErrorView
|
||||||
@HTMLBuilder onError: @escaping @Sendable (E) -> ErrorView
|
) async {
|
||||||
) {
|
self.result = await Result(catching: content)
|
||||||
self.result = result
|
self.errorView = onError
|
||||||
self.onError = onError
|
|
||||||
self.onSuccess = onSuccess
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public var body: some HTML {
|
public var body: some HTML {
|
||||||
switch result {
|
switch result {
|
||||||
case .success(let value):
|
case .success(let view):
|
||||||
onSuccess(value)
|
view
|
||||||
case .failure(let error):
|
case .failure(let error):
|
||||||
onError(error)
|
errorView(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ResultView {
|
extension ResultView where ErrorView == Styleguide.ErrorView {
|
||||||
|
|
||||||
public init(
|
public init(
|
||||||
result: Result<V, E>,
|
_ content: @escaping @Sendable () async throws -> ValueView
|
||||||
@HTMLBuilder onSuccess: @escaping @Sendable (V) -> ValueView
|
) async {
|
||||||
) where ErrorView == Styleguide.ErrorView<E> {
|
await self.init(
|
||||||
self.init(result: result, onSuccess: onSuccess) { error in
|
content,
|
||||||
Styleguide.ErrorView(error: error)
|
onError: { Styleguide.ErrorView(error: $0) }
|
||||||
}
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
public init<V: Sendable>(
|
||||||
|
catching: @escaping @Sendable () async throws -> V,
|
||||||
|
onSuccess content: @escaping @Sendable (V) -> ValueView
|
||||||
|
) async where ValueView: Sendable {
|
||||||
|
await self.init(
|
||||||
|
{
|
||||||
|
try await content(catching())
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(
|
public init(
|
||||||
catching: @escaping @Sendable () async throws(E) -> V,
|
catching: @escaping @Sendable () async throws -> Void
|
||||||
@HTMLBuilder onSuccess: @escaping @Sendable (V) -> ValueView
|
) async where ValueView == EmptyHTML {
|
||||||
) async where ErrorView == Styleguide.ErrorView<E> {
|
|
||||||
await self.init(
|
await self.init(
|
||||||
result: .init(catching: catching),
|
catching: catching,
|
||||||
onSuccess: onSuccess
|
|
||||||
) { error in
|
|
||||||
Styleguide.ErrorView(error: error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public init(
|
|
||||||
catching: @escaping @Sendable () async throws(E) -> V,
|
|
||||||
) async where ErrorView == Styleguide.ErrorView<E>, V == Void, ValueView == EmptyHTML {
|
|
||||||
await self.init(
|
|
||||||
result: .init(catching: catching),
|
|
||||||
onSuccess: { EmptyHTML() }
|
onSuccess: { EmptyHTML() }
|
||||||
) { error in
|
)
|
||||||
Styleguide.ErrorView(error: error)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ResultView where V == ValueView {
|
extension ResultView: Sendable where ValueView: Sendable, ErrorView: Sendable {}
|
||||||
|
|
||||||
public init(
|
public struct ErrorView: HTML, Sendable {
|
||||||
catching: @escaping @Sendable () async throws(E) -> V
|
let error: any Error
|
||||||
) async where ErrorView == Styleguide.ErrorView<E> {
|
|
||||||
await self.init(result: .init(catching: catching)) {
|
|
||||||
$0
|
|
||||||
} onError: { error in
|
|
||||||
Styleguide.ErrorView(error: error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension ResultView: Sendable where Error: Sendable, ValueView: Sendable, ErrorView: Sendable {}
|
public init(error: any Error) {
|
||||||
|
|
||||||
public struct ErrorView<E: Error>: HTML, Sendable where Error: Sendable {
|
|
||||||
|
|
||||||
let error: E
|
|
||||||
|
|
||||||
public init(error: E) {
|
|
||||||
self.error = error
|
self.error = error
|
||||||
}
|
}
|
||||||
|
|
||||||
public var body: some HTML<HTMLTag.div> {
|
public var body: some HTML<HTMLTag.div> {
|
||||||
div {
|
div {
|
||||||
h1(.class("text-2xl font-bold text-error")) { "Oops: Error" }
|
h1(.class("text-xl font-bold text-error")) { "Oops: Error" }
|
||||||
p {
|
p {
|
||||||
"\(error)"
|
"\(error)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -193,9 +193,14 @@ extension SiteRoute.View.ProjectRoute {
|
|||||||
case .frictionRate(let route):
|
case .frictionRate(let route):
|
||||||
return await route.renderView(on: request, projectID: projectID)
|
return await route.renderView(on: request, projectID: projectID)
|
||||||
case .pdf:
|
case .pdf:
|
||||||
return await ResultView {
|
// return await ResultView2 {
|
||||||
try await projectClient.toHTML(projectID)
|
// try await projectClient.toHTML(projectID)
|
||||||
}
|
// } onError: {
|
||||||
|
// ErrorView2(error: $0)
|
||||||
|
// }
|
||||||
|
// return await ResultView {
|
||||||
|
return try! await projectClient.toHTML(projectID)
|
||||||
|
// }
|
||||||
// fatalError()
|
// fatalError()
|
||||||
case .rooms(let route):
|
case .rooms(let route):
|
||||||
return await route.renderView(on: request, projectID: projectID)
|
return await route.renderView(on: request, projectID: projectID)
|
||||||
|
|||||||
Reference in New Issue
Block a user