feat: Begins a generic htmx form context and template, integrates user form, begins views for vendor and purchase orders.

This commit is contained in:
2025-01-08 14:02:50 -05:00
parent 3557227430
commit 2b6e92a5c6
18 changed files with 493 additions and 93 deletions

View File

@@ -8,29 +8,89 @@ struct UserViewController: RouteCollection {
func boot(routes: any RoutesBuilder) throws {
let users = routes.protected.grouped("users")
users.get(use: index(req:))
users.post(use: create(req:))
users.group(":userID") {
$0.delete(use: delete(req:))
}
}
@Sendable
func index(req: Request) async throws -> View {
let users = try await api.usersIndex(req: req)
return try await req.view.render("users", ["users": users])
try await req.view.render(
"users",
UsersCTX(users: api.getSortedUsers(req: req))
)
}
@Sendable
func create(req: Request) async throws -> View {
_ = try await api.createUser(req: req)
return try await req.view.render("user-table", ["users": api.getSortedUsers(req: req)])
}
@Sendable
func delete(req: Request) async throws -> View {
_ = try await api.deleteUser(req: req)
return try await req.view.render("user-table", ["users": api.getSortedUsers(req: req)])
}
}
struct UserFormCTX: Content {
let htmxTargetUrl: String
let htmxTarget: String
let htmxPushUrl: String
let showConfirmPassword: Bool
let buttonLabel: String
let htmxForm: HtmxFormCTX<Context>
static func signIn(route: String) -> Self {
struct Context: Content {
let showConfirmPassword: Bool
let showEmailInput: Bool
let buttonLabel: String
}
static func signIn(next: String?) -> Self {
.init(
htmxTargetUrl: route,
htmxTarget: "body",
htmxPushUrl: "true",
showConfirmPassword: false,
buttonLabel: "Sign In"
htmxForm: .init(
formClass: "user-form",
formId: "user-form",
htmxTargetUrl: .post("/login\(next != nil ? "?next=\(next!)" : "")"),
htmxTarget: "body",
htmxPushUrl: true,
htmxResetAfterRequest: true,
htmxSwapOob: nil,
htmxSwap: nil,
context: .init(showConfirmPassword: false, showEmailInput: false, buttonLabel: "Sign In")
)
)
}
static func create() -> Self {
.init(
htmxForm: .init(
formClass: "user-form",
formId: "user-form",
htmxTargetUrl: .post("/users"),
htmxTarget: "#user-table",
htmxPushUrl: false,
htmxResetAfterRequest: true,
htmxSwapOob: nil,
htmxSwap: nil,
context: .init(showConfirmPassword: true, showEmailInput: true, buttonLabel: "Create")
)
)
}
}
private struct UsersCTX: Content {
let users: [User.DTO]
let form: UserFormCTX
init(users: [User.DTO], form: UserFormCTX? = nil) {
self.users = users
self.form = form ?? .create()
}
}
private extension ApiController {
func getSortedUsers(req: Request) async throws -> [User.DTO] {
try await usersIndex(req: req)
.sorted { ($0.username ?? "") < ($1.username ?? "") }
}
}