WIP: Working rooms table and form for project.

This commit is contained in:
2026-01-03 17:02:21 -05:00
parent 1aeb6144d5
commit 9f63b96c80
7 changed files with 68 additions and 54 deletions

View File

@@ -53,8 +53,7 @@ extension Room.Create {
return .init( return .init(
name: name, name: name,
heatingLoad: heatingLoad, heatingLoad: heatingLoad,
coolingTotal: coolingTotal, coolingLoad: coolingLoad,
coolingSensible: coolingSensible,
registerCount: registerCount, registerCount: registerCount,
projectID: projectID projectID: projectID
) )
@@ -67,12 +66,9 @@ extension Room.Create {
guard heatingLoad >= 0 else { guard heatingLoad >= 0 else {
throw ValidationError("Room heating load should not be less than 0.") throw ValidationError("Room heating load should not be less than 0.")
} }
guard coolingTotal >= 0 else { guard coolingLoad >= 0 else {
throw ValidationError("Room cooling total should not be less than 0.") throw ValidationError("Room cooling total should not be less than 0.")
} }
guard coolingSensible >= 0 else {
throw ValidationError("Room cooling sensible should not be less than 0.")
}
guard registerCount >= 1 else { guard registerCount >= 1 else {
throw ValidationError("Room cooling sensible should not be less than 1.") throw ValidationError("Room cooling sensible should not be less than 1.")
} }
@@ -88,8 +84,7 @@ extension Room {
.id() .id()
.field("name", .string, .required) .field("name", .string, .required)
.field("heatingLoad", .double, .required) .field("heatingLoad", .double, .required)
.field("coolingTotal", .double, .required) .field("coolingLoad", .double, .required)
.field("coolingSensible", .double, .required)
.field("registerCount", .int8, .required) .field("registerCount", .int8, .required)
.field("createdAt", .datetime) .field("createdAt", .datetime)
.field("updatedAt", .datetime) .field("updatedAt", .datetime)
@@ -117,11 +112,8 @@ final class RoomModel: Model, @unchecked Sendable {
@Field(key: "heatingLoad") @Field(key: "heatingLoad")
var heatingLoad: Double var heatingLoad: Double
@Field(key: "coolingTotal") @Field(key: "coolingLoad")
var coolingTotal: Double var coolingLoad: Double
@Field(key: "coolingSensible")
var coolingSensible: Double
@Field(key: "registerCount") @Field(key: "registerCount")
var registerCount: Int var registerCount: Int
@@ -141,8 +133,7 @@ final class RoomModel: Model, @unchecked Sendable {
id: UUID? = nil, id: UUID? = nil,
name: String, name: String,
heatingLoad: Double, heatingLoad: Double,
coolingTotal: Double, coolingLoad: Double,
coolingSensible: Double,
registerCount: Int, registerCount: Int,
createdAt: Date? = nil, createdAt: Date? = nil,
updatedAt: Date? = nil, updatedAt: Date? = nil,
@@ -151,8 +142,7 @@ final class RoomModel: Model, @unchecked Sendable {
self.id = id self.id = id
self.name = name self.name = name
self.heatingLoad = heatingLoad self.heatingLoad = heatingLoad
self.coolingTotal = coolingTotal self.coolingLoad = coolingLoad
self.coolingSensible = coolingSensible
self.registerCount = registerCount self.registerCount = registerCount
self.createdAt = createdAt self.createdAt = createdAt
self.updatedAt = updatedAt self.updatedAt = updatedAt
@@ -165,11 +155,10 @@ final class RoomModel: Model, @unchecked Sendable {
projectID: $project.id, projectID: $project.id,
name: name, name: name,
heatingLoad: heatingLoad, heatingLoad: heatingLoad,
coolingLoad: .init(total: coolingTotal, sensible: coolingSensible), coolingLoad: coolingLoad,
registerCount: registerCount, registerCount: registerCount,
createdAt: createdAt!, createdAt: createdAt!,
updatedAt: updatedAt! updatedAt: updatedAt!
) )
} }
} }

View File

@@ -6,7 +6,7 @@ public struct Room: Codable, Equatable, Identifiable, Sendable {
public let projectID: Project.ID public let projectID: Project.ID
public let name: String public let name: String
public let heatingLoad: Double public let heatingLoad: Double
public let coolingLoad: CoolingLoad public let coolingLoad: Double
public let registerCount: Int public let registerCount: Int
public let createdAt: Date public let createdAt: Date
public let updatedAt: Date public let updatedAt: Date
@@ -16,7 +16,7 @@ public struct Room: Codable, Equatable, Identifiable, Sendable {
projectID: Project.ID, projectID: Project.ID,
name: String, name: String,
heatingLoad: Double, heatingLoad: Double,
coolingLoad: CoolingLoad, coolingLoad: Double,
registerCount: Int = 1, registerCount: Int = 1,
createdAt: Date, createdAt: Date,
updatedAt: Date updatedAt: Date
@@ -39,23 +39,20 @@ extension Room {
public let projectID: Project.ID public let projectID: Project.ID
public let name: String public let name: String
public let heatingLoad: Double public let heatingLoad: Double
public let coolingTotal: Double public let coolingLoad: Double
public let coolingSensible: Double
public let registerCount: Int public let registerCount: Int
public init( public init(
projectID: Project.ID, projectID: Project.ID,
name: String, name: String,
heatingLoad: Double, heatingLoad: Double,
coolingTotal: Double, coolingLoad: Double,
coolingSensible: Double,
registerCount: Int = 1 registerCount: Int = 1
) { ) {
self.projectID = projectID self.projectID = projectID
self.name = name self.name = name
self.heatingLoad = heatingLoad self.heatingLoad = heatingLoad
self.coolingTotal = coolingTotal self.coolingLoad = coolingLoad
self.coolingSensible = coolingSensible
self.registerCount = registerCount self.registerCount = registerCount
} }
} }
@@ -70,7 +67,7 @@ extension Room {
projectID: UUID(0), projectID: UUID(0),
name: "Kitchen", name: "Kitchen",
heatingLoad: 12345, heatingLoad: 12345,
coolingLoad: .init(total: 12345, sensible: 12345), coolingLoad: 1234,
registerCount: 2, registerCount: 2,
createdAt: Date(), createdAt: Date(),
updatedAt: Date() updatedAt: Date()
@@ -80,7 +77,7 @@ extension Room {
projectID: UUID(1), projectID: UUID(1),
name: "Bedroom - 1", name: "Bedroom - 1",
heatingLoad: 12345, heatingLoad: 12345,
coolingLoad: .init(total: 12345, sensible: 12345), coolingLoad: 1456,
registerCount: 1, registerCount: 1,
createdAt: Date(), createdAt: Date(),
updatedAt: Date() updatedAt: Date()
@@ -90,7 +87,7 @@ extension Room {
projectID: UUID(2), projectID: UUID(2),
name: "Family Room", name: "Family Room",
heatingLoad: 12345, heatingLoad: 12345,
coolingLoad: .init(total: 12345, sensible: 12345), coolingLoad: 1673,
registerCount: 3, registerCount: 3,
createdAt: Date(), createdAt: Date(),
updatedAt: Date() updatedAt: Date()

View File

@@ -104,8 +104,9 @@ extension SiteRoute.View {
extension SiteRoute.View { extension SiteRoute.View {
public enum RoomRoute: Equatable, Sendable { public enum RoomRoute: Equatable, Sendable {
case form(dismiss: Bool = false) case form(Project.ID, dismiss: Bool = false)
case index(Project.ID) case index(Project.ID)
case submit(Room.Create)
static let rootPath = "rooms" static let rootPath = "rooms"
@@ -117,6 +118,7 @@ extension SiteRoute.View {
} }
Method.get Method.get
Query { Query {
Field("projectID") { Project.ID.parser() }
Field("dismiss", default: false) { Bool.parser() } Field("dismiss", default: false) { Bool.parser() }
} }
} }
@@ -127,6 +129,20 @@ extension SiteRoute.View {
Field("projectID") { Project.ID.parser() } Field("projectID") { Project.ID.parser() }
} }
} }
Route(.case(Self.submit)) {
Path { rootPath }
Method.post
Body {
FormData {
Field("projectID") { Project.ID.parser() }
Field("name", .string)
Field("heatingLoad") { Double.parser() }
Field("coolingLoad") { Double.parser() }
Field("registerCount") { Digits() }
}
.map(.memberwise(Room.Create.init))
}
}
} }
} }
} }

View File

@@ -120,13 +120,25 @@ extension SiteRoute.View.RoomRoute {
@Dependency(\.database) var database @Dependency(\.database) var database
switch self { switch self {
case .form(let dismiss):
return RoomForm(dismiss: dismiss) case .form(let projectID, let dismiss):
return RoomForm(dismiss: dismiss, projectID: projectID)
case .index(let projectID): case .index(let projectID):
let rooms = try await database.rooms.fetch(projectID) let rooms = try await database.rooms.fetch(projectID)
return request.view { return request.view {
ProjectView(projectID: projectID, activeTab: .rooms) { ProjectView(projectID: projectID, activeTab: .rooms) {
RoomsView(rooms: rooms) RoomsView(projectID: projectID, rooms: rooms)
}
}
case .submit(let form):
request.logger.debug("New room form submitted.")
let _ = try await database.rooms.create(form)
let rooms = try await database.rooms.fetch(form.projectID)
return request.view {
ProjectView(projectID: form.projectID, activeTab: .rooms) {
RoomsView(projectID: form.projectID, rooms: rooms)
} }
} }
} }

View File

@@ -3,6 +3,8 @@ import ElementaryHTMX
import ManualDCore import ManualDCore
import Styleguide import Styleguide
// TODO: Need a back button to navigate to all projects table.
struct ProjectView<Inner: HTML>: HTML, Sendable where Inner: Sendable { struct ProjectView<Inner: HTML>: HTML, Sendable where Inner: Sendable {
let projectID: Project.ID let projectID: Project.ID
let activeTab: Sidebar.ActiveTab let activeTab: Sidebar.ActiveTab
@@ -54,7 +56,7 @@ struct Sidebar: HTML {
} }
.attributes(.class("p-4")) .attributes(.class("p-4"))
row(title: "Project", icon: .mapPin, route: .project(.index)) row(title: "Project", icon: .mapPin, route: .project(.detail(projectID)))
.attributes(.data("active", value: active == .projects ? "true" : "false")) .attributes(.data("active", value: active == .projects ? "true" : "false"))
row(title: "Rooms", icon: .doorClosed, route: .room(.index(projectID))) row(title: "Rooms", icon: .doorClosed, route: .room(.index(projectID)))

View File

@@ -9,11 +9,19 @@ import Styleguide
struct RoomForm: HTML, Sendable { struct RoomForm: HTML, Sendable {
let dismiss: Bool let dismiss: Bool
let projectID: Project.ID
var body: some HTML { var body: some HTML {
ModalForm(id: "roomForm", dismiss: dismiss) { ModalForm(id: "roomForm", dismiss: dismiss) {
h1(.class("text-3xl font-bold pb-6")) { "Room" } h1(.class("text-3xl font-bold pb-6")) { "Room" }
form { // TODO: Use htmx here.
form(
.method(.post),
.action(route: .room(.index(projectID)))
) {
div(.class("hidden")) {
input(.name("projectID"), .id("projectID"), .value("\(projectID)"))
}
div { div {
label(.for("name")) { "Name:" } label(.for("name")) { "Name:" }
Input(id: "name", placeholder: "Room Name") Input(id: "name", placeholder: "Room Name")
@@ -40,7 +48,7 @@ struct RoomForm: HTML, Sendable {
div(.class("space-x-4")) { div(.class("space-x-4")) {
CancelButton() CancelButton()
.attributes( .attributes(
.hx.get(route: .room(.form(dismiss: true))), .hx.get(route: .room(.form(projectID, dismiss: true))),
.hx.target("#roomForm"), .hx.target("#roomForm"),
.hx.swap(.outerHTML) .hx.swap(.outerHTML)
) )

View File

@@ -5,7 +5,10 @@ import Foundation
import ManualDCore import ManualDCore
import Styleguide import Styleguide
// TODO: Calculate rooms sensible based on project wide SHR.
struct RoomsView: HTML, Sendable { struct RoomsView: HTML, Sendable {
let projectID: Project.ID
let rooms: [Room] let rooms: [Room]
var body: some HTML { var body: some HTML {
@@ -17,7 +20,7 @@ struct RoomsView: HTML, Sendable {
.data("tip", value: "Add room") .data("tip", value: "Add room")
) { ) {
button( button(
.hx.get(route: .room(.form(dismiss: false))), .hx.get(route: .room(.form(projectID, dismiss: false))),
.hx.target("#roomForm"), .hx.target("#roomForm"),
.hx.swap(.outerHTML), .hx.swap(.outerHTML),
.class("btn btn-primary w-[40px] text-2xl") .class("btn btn-primary w-[40px] text-2xl")
@@ -35,7 +38,6 @@ struct RoomsView: HTML, Sendable {
th { Label("Name") } th { Label("Name") }
th { Label("Heating Load") } th { Label("Heating Load") }
th { Label("Cooling Total") } th { Label("Cooling Total") }
th { Label("Cooling Sensible") }
th { Label("Register Count") } th { Label("Register Count") }
} }
} }
@@ -48,13 +50,9 @@ struct RoomsView: HTML, Sendable {
.attributes(.class("text-error")) .attributes(.class("text-error"))
} }
td { td {
Number(room.coolingLoad.total) Number(room.coolingLoad)
.attributes(.class("text-success")) .attributes(.class("text-success"))
} }
td {
Number(room.coolingLoad.sensible)
.attributes(.class("text-info"))
}
td { td {
Number(room.registerCount) Number(room.registerCount)
} }
@@ -72,16 +70,12 @@ struct RoomsView: HTML, Sendable {
.attributes( .attributes(
.class("badge badge-outline badge-success badge-xl")) .class("badge badge-outline badge-success badge-xl"))
} }
td {
Number(rooms.coolingSensibleTotal)
.attributes(.class("badge badge-outline badge-info badge-xl"))
}
td {} td {}
} }
} }
} }
} }
RoomForm(dismiss: true) RoomForm(dismiss: true, projectID: projectID)
} }
} }
@@ -93,10 +87,6 @@ extension Array where Element == Room {
} }
var coolingTotal: Double { var coolingTotal: Double {
reduce(into: 0) { $0 += $1.coolingLoad.total } reduce(into: 0) { $0 += $1.coolingLoad }
}
var coolingSensibleTotal: Double {
reduce(into: 0) { $0 += $1.coolingLoad.sensible }
} }
} }