feat: Adds update and delete routes to room.

This commit is contained in:
2026-01-05 15:59:23 -05:00
parent fb7cf9905c
commit 4c8a23be94
17 changed files with 366 additions and 125 deletions

View File

@@ -1,23 +1,24 @@
import DatabaseClient
import Dependencies
import Elementary
import ElementaryHTMX
import ManualDCore
import Styleguide
// TODO: Need a back button to navigate to all projects table.
// TODO: Make view async and load based on the active tab.
struct ProjectView: HTML, Sendable {
@Dependency(\.database) var database
struct ProjectView<Inner: HTML>: HTML, Sendable where Inner: Sendable {
let projectID: Project.ID
let activeTab: Sidebar.ActiveTab
let inner: Inner
let activeTab: SiteRoute.View.ProjectRoute.DetailRoute.Tab
init(
projectID: Project.ID,
activeTab: Sidebar.ActiveTab,
@HTMLBuilder inner: () -> Inner
activeTab: SiteRoute.View.ProjectRoute.DetailRoute.Tab
) {
self.projectID = projectID
self.activeTab = activeTab
self.inner = inner()
}
var body: some HTML {
@@ -25,7 +26,30 @@ struct ProjectView<Inner: HTML>: HTML, Sendable where Inner: Sendable {
div(.class("flex flex-row")) {
Sidebar(active: activeTab, projectID: projectID)
main(.class("flex flex-col h-screen w-full px-6 py-10")) {
inner
switch self.activeTab {
case .project:
if let project = try await database.projects.get(projectID) {
ProjectDetail(project: project)
} else {
div {
"FIX ME!"
}
}
case .rooms:
try await RoomsView(projectID: projectID, rooms: database.rooms.fetch(projectID))
case .effectiveLength:
try await EffectiveLengthsView(
effectiveLengths: database.effectiveLength.fetch(projectID)
)
case .frictionRate:
try await FrictionRateView(
equipmentInfo: database.equipment.fetch(projectID),
componentLosses: database.componentLoss.fetch(projectID), projectID: projectID)
case .ductSizing:
div { "FIX ME!" }
}
}
}
}
@@ -35,7 +59,7 @@ struct ProjectView<Inner: HTML>: HTML, Sendable where Inner: Sendable {
// TODO: Update to use DaisyUI drawer.
struct Sidebar: HTML {
let active: ActiveTab
let active: SiteRoute.View.ProjectRoute.DetailRoute.Tab
let projectID: Project.ID
var body: some HTML {
@@ -49,15 +73,31 @@ struct Sidebar: HTML {
)
) {
// TODO: Move somewhere outside of the sidebar.
div(.class("flex")) {
// TODO: Move somewhere outside of the sidebar.
button(
.class("w-full btn btn-secondary"),
.hx.get(route: .project(.index)),
.hx.target("body"),
.hx.pushURL(true),
.hx.swap(.outerHTML),
) {
"< All Projects"
}
}
Row {
Label("Theme")
input(.type(.checkbox), .class("toggle theme-controller"), .value("light"))
}
.attributes(.class("p-4"))
row(title: "Project", icon: .mapPin, route: .project(.detail(projectID, .index)))
.attributes(.data("active", value: active == .projects ? "true" : "false"))
row(
title: "Project",
icon: .mapPin,
route: .project(.detail(projectID, .index(tab: .project)))
)
.attributes(.data("active", value: active == .project ? "true" : "false"))
row(title: "Rooms", icon: .doorClosed, route: .project(.detail(projectID, .rooms(.index))))
.attributes(.data("active", value: active == .rooms ? "true" : "false"))
@@ -109,13 +149,3 @@ struct Sidebar: HTML {
row(title: title, icon: icon, href: SiteRoute.View.router.path(for: route))
}
}
extension Sidebar {
enum ActiveTab: Equatable, Sendable {
case projects
case rooms
case effectiveLength
case frictionRate
case ductSizing
}
}

View File

@@ -67,10 +67,19 @@ extension ProjectsTable {
td { "\(project.name)" }
td { "\(project.streetAddress)" }
td {
a(
.class("btn btn-success"),
.href(route: .project(.detail(project.id, .index)))
) { ">" }
Row {
div {}
TrashButton()
.attributes(
.hx.delete(route: .project(.delete(id: project.id))),
.hx.confirm("Are you sure?"),
.hx.target("closest tr")
)
a(
.class("btn btn-success dark:text-white"),
.href(route: .project(.detail(project.id, .index())))
) { ">" }
}
}
}
}

View File

@@ -10,34 +10,48 @@ struct RoomForm: HTML, Sendable {
let dismiss: Bool
let projectID: Project.ID
let room: Room?
var body: some HTML {
ModalForm(id: "roomForm", dismiss: dismiss) {
h1(.class("text-3xl font-bold pb-6")) { "Room" }
// TODO: Use htmx here.
form(
.method(.post),
.action(route: .project(.detail(projectID, .rooms(.index))))
room == nil
? .hx.post(route: .project(.detail(projectID, .rooms(.index))))
: .hx.patch(route: .project(.detail(projectID, .rooms(.index)))),
.hx.target("body"),
.hx.swap(.outerHTML)
) {
input(.class("hidden"), .name("projectID"), .value("\(projectID)"))
if let id = room?.id {
input(.class("hidden"), .name("id"), .value("\(id)"))
}
div {
label(.for("name")) { "Name:" }
Input(id: "name", placeholder: "Room Name")
.attributes(.type(.text), .required, .autofocus)
.attributes(.type(.text), .required, .autofocus, .value(room?.name))
}
div {
label(.for("heatingLoad")) { "Heating Load:" }
Input(id: "heatingLoad", placeholder: "Heating Load")
.attributes(.type(.number), .required, .min("0"))
.attributes(.type(.number), .required, .min("0"), .value(room?.heatingLoad))
}
div {
label(.for("coolingLoad")) { "Cooling Load:" }
Input(id: "coolingLoad", placeholder: "Cooling Load")
.attributes(.type(.number), .required, .min("0"))
.attributes(.type(.number), .required, .min("0"), .value(room?.coolingLoad))
}
div {
label(.for("registerCount")) { "Registers:" }
Input(id: "registerCount", placeholder: "Register Count")
.attributes(.type(.number), .required, .value("1"), .min("1"))
.attributes(
.type(.number), .required, .min("0"),
.value("\(room != nil ? room!.registerCount : 1)"),
)
}
Row {
// Force button to the right, probably a better way.

View File

@@ -39,23 +39,13 @@ struct RoomsView: HTML, Sendable {
th { Label("Heating Load") }
th { Label("Cooling Total") }
th { Label("Register Count") }
th {}
}
}
tbody {
for room in rooms {
tr {
td { room.name }
td {
Number(room.heatingLoad)
.attributes(.class("text-error"))
}
td {
Number(room.coolingLoad)
.attributes(.class("text-success"))
}
td {
Number(room.registerCount)
}
div(.id("rooms")) {
for room in rooms {
RoomRow(room: room)
}
}
// TOTALS
@@ -75,7 +65,48 @@ struct RoomsView: HTML, Sendable {
}
}
}
RoomForm(dismiss: true, projectID: projectID)
RoomForm(dismiss: true, projectID: projectID, room: nil)
}
}
public struct RoomRow: HTML, Sendable {
let room: Room
public var body: some HTML {
tr(.id("\(room.id)")) {
td { room.name }
td {
Number(room.heatingLoad)
.attributes(.class("text-error"))
}
td {
Number(room.coolingLoad)
.attributes(.class("text-success"))
}
td {
Number(room.registerCount)
}
td {
Row {
TrashButton()
.attributes(
.hx.delete(route: .project(.detail(room.projectID, .rooms(.delete(id: room.id))))),
.hx.target("closest tr"),
.hx.confirm("Are you sure?")
)
EditButton()
.attributes(
.hx.get(
route: .project(
.detail(room.projectID, .rooms(.form(id: room.id, dismiss: false)))
)
),
.hx.target("#roomForm"),
.hx.swap(.outerHTML)
)
}
}
}
}
}