feat: Working on adding updates to project form, it's currently not loading an existing project.

This commit is contained in:
2026-01-05 17:04:25 -05:00
parent 4c8a23be94
commit fc12e47b5c
9 changed files with 178 additions and 35 deletions

View File

@@ -85,8 +85,16 @@ extension SiteRoute.View.ProjectRoute {
let projects = try await database.projects.fetch(user.id, page)
return ProjectsTable(userID: user.id, projects: projects)
case .form(let dismiss):
return ProjectForm(dismiss: dismiss)
case .form(let id, let dismiss):
request.logger.debug("Project form: \(id != nil ? "Fetching project for: \(id!)" : "N/A")")
var project: Project? = nil
if let id, dismiss == false {
project = try await database.projects.get(id)
}
request.logger.debug(
project == nil ? "No project found" : "Showing form for existing project"
)
return ProjectForm(dismiss: dismiss, project: project)
case .create(let form):
let project = try await database.projects.create(user.id, form)
@@ -97,11 +105,16 @@ extension SiteRoute.View.ProjectRoute {
try await database.projects.delete(id)
return EmptyHTML()
case .update(let form):
let project = try await database.projects.update(form)
return ProjectView(projectID: project.id, activeTab: .project)
case .detail(let projectID, let route):
switch route {
case .index(let tab):
return ProjectView(projectID: projectID, activeTab: tab)
// return try await defaultDetailView(projectID: projectID, activeTab: tab)
return request.view {
ProjectView(projectID: projectID, activeTab: tab)
}
case .equipment(let route):
return try await route.renderView(on: request, projectID: projectID)
case .frictionRate(let route):

View File

@@ -18,7 +18,7 @@ struct ProjectDetail: HTML, Sendable {
h1(.class("text-2xl font-bold")) { "Project" }
EditButton()
.attributes(
.hx.get(route: .project(.form(dismiss: false))),
.hx.get(route: .project(.form(id: project.id, dismiss: false))),
.hx.target("#projectForm"),
.hx.swap(.outerHTML)
)
@@ -54,6 +54,6 @@ struct ProjectDetail: HTML, Sendable {
}
}
div(.id("projectForm")) {}
ProjectForm(dismiss: true)
}
}

View File

@@ -21,9 +21,16 @@ struct ProjectForm: HTML, Sendable {
h1(.class("text-3xl font-bold pb-6 ps-2")) { "Project" }
form(
.class("space-y-4 p-4"),
.method(.post),
.action(route: .project(.index))
project == nil
? .hx.post(route: .project(.index))
: .hx.patch(route: .project(.index)),
.hx.target("body"),
.hx.swap(.outerHTML)
) {
if let project {
input(.class("hidden"), .name("id"), .value("\(project.id)"))
}
div {
label(.for("name")) { "Name" }
Input(id: "name", placeholder: "Name")
@@ -32,35 +39,32 @@ struct ProjectForm: HTML, Sendable {
div {
label(.for("streetAddress")) { "Address" }
Input(id: "streetAddress", placeholder: "Street Address")
.attributes(.type(.text), .required)
.attributes(.type(.text), .required, .value(project?.streetAddress))
}
div {
label(.for("city")) { "City" }
Input(id: "city", placeholder: "City")
.attributes(.type(.text), .required)
.attributes(.type(.text), .required, .value(project?.city))
}
div {
label(.for("state")) { "State" }
Input(id: "state", placeholder: "State")
.attributes(.type(.text), .required)
.attributes(.type(.text), .required, .value(project?.state))
}
div {
label(.for("zipCode")) { "Zip" }
Input(id: "zipCode", placeholder: "Zip code")
.attributes(.type(.text), .required)
.attributes(.type(.text), .required, .value(project?.zipCode))
}
Row {
div {}
div(.class("space-x-4")) {
CancelButton()
.attributes(
.hx.get(route: .project(.form(dismiss: true))),
.hx.target("#projectForm"),
.hx.swap(.outerHTML)
)
SubmitButton()
}
div(.class("flex justify-end space-x-6")) {
CancelButton()
.attributes(
.hx.get(route: .project(.form(dismiss: true))),
.hx.target("#projectForm"),
.hx.swap(.outerHTML)
)
SubmitButton()
}
}
}

View File

@@ -61,6 +61,7 @@ struct RoomsView: HTML, Sendable {
.class("badge badge-outline badge-success badge-xl"))
}
td {}
td {}
}
}
}
@@ -87,10 +88,11 @@ struct RoomsView: HTML, Sendable {
Number(room.registerCount)
}
td {
Row {
div(.class("flex justify-end space-x-6")) {
TrashButton()
.attributes(
.hx.delete(route: .project(.detail(room.projectID, .rooms(.delete(id: room.id))))),
.hx.delete(
route: .project(.detail(room.projectID, .rooms(.delete(id: room.id))))),
.hx.target("closest tr"),
.hx.confirm("Are you sure?")
)