feat: Initial view controller dependency and snapshot tests.
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
import Dependencies
|
||||
import Elementary
|
||||
import ElementaryHTMX
|
||||
import SharedModels
|
||||
|
||||
struct PurchaseOrderForm: HTML {
|
||||
|
||||
@Dependency(\.dateFormatter) var dateFormatter
|
||||
|
||||
let purchaseOrder: PurchaseOrder?
|
||||
let shouldShow: Bool
|
||||
|
||||
init(purchaseOrder: PurchaseOrder? = nil, shouldShow: Bool = false) {
|
||||
self.purchaseOrder = purchaseOrder
|
||||
self.shouldShow = shouldShow
|
||||
}
|
||||
|
||||
var content: some HTML {
|
||||
Float(shouldDisplay: shouldShow, resetURL: .purchaseOrder(.index)) {
|
||||
if shouldShow {
|
||||
if purchaseOrder != nil {
|
||||
p {
|
||||
span(.class("label"), .style("margin-right: 15px;")) { "Note:" }
|
||||
span { i(.style("font-size: 1em;")) {
|
||||
"Vendor and Employee can not be changed once a purchase order has been created."
|
||||
} }
|
||||
}
|
||||
}
|
||||
form(
|
||||
.hx.post(route: .purchaseOrder(.index)),
|
||||
.hx.target(.id(.purchaseOrder(.table))),
|
||||
.hx.swap(.afterBegin),
|
||||
.hx.on(.afterRequest, .ifSuccessful(.toggleContent(.float)))
|
||||
) {
|
||||
div(.class("row")) {
|
||||
label(
|
||||
.for("customer"), .class("label col-2"), .style("margin-right: 15px; margin-bottom: 5px;")
|
||||
) { "Customer:" }
|
||||
input(
|
||||
.type(.text), .class("col-3"),
|
||||
.name("customer"), .placeholder("Customer"),
|
||||
.value(purchaseOrder?.customer ?? ""),
|
||||
.required, .autofocus
|
||||
)
|
||||
label(
|
||||
.for("workOrder"), .class("label col-2"), .style("margin-right: 15px; margin-bottom: 5px;")
|
||||
) { "Work Order:" }
|
||||
input(
|
||||
.type(.text), .class("col-4"),
|
||||
.name("workOrder"), .placeholder("Work Order: (12345)"),
|
||||
.value("\(purchaseOrder?.workOrder != nil ? String(purchaseOrder!.workOrder!) : "")")
|
||||
)
|
||||
}
|
||||
div(.class("row")) {
|
||||
label(
|
||||
.for("materials"), .class("label col-2"), .style("margin-right: 15px; margin-bottom: 5px;")
|
||||
) { "Materials:" }
|
||||
input(
|
||||
.type(.text), .class("col-3"),
|
||||
.name("materials"), .placeholder("Materials"),
|
||||
.value(purchaseOrder?.materials ?? ""),
|
||||
.required
|
||||
)
|
||||
label(
|
||||
.for("vendorBranchID"), .class("label col-2"), .style("margin-right: 15px; margin-bottom: 5px;")
|
||||
) { "Vendor:" }
|
||||
if purchaseOrder == nil {
|
||||
VendorBranchSelect.purchaseOrderForm()
|
||||
} else {
|
||||
input(
|
||||
.type(.text), .class("col-4"),
|
||||
.name("vendorBranchID"),
|
||||
.value("\(purchaseOrder!.vendorBranch.vendor.name) - \(purchaseOrder!.vendorBranch.name)"),
|
||||
.disabled
|
||||
)
|
||||
}
|
||||
}
|
||||
div(.class("row")) {
|
||||
label(
|
||||
.for("createdForID"), .class("label col-2"), .style("margin-right: 15px; margin-bottom: 5px;")
|
||||
) { "Employee:" }
|
||||
if purchaseOrder == nil {
|
||||
EmployeeSelect.purchaseOrderForm()
|
||||
} else {
|
||||
input(
|
||||
.type(.text), .class("col-3"),
|
||||
.value(purchaseOrder!.createdFor.fullName),
|
||||
.disabled
|
||||
)
|
||||
}
|
||||
label(
|
||||
.for("truckStock"), .class("label col-2"), .style("margin-right: 15px; margin-bottom: 5px;")
|
||||
) { "Truck Stock:" }
|
||||
if purchaseOrder?.truckStock == true {
|
||||
input(
|
||||
.type(.checkbox), .class("col-2"), .name("truckStock"), .style("margin-top: 20px;"), .checked
|
||||
)
|
||||
} else {
|
||||
input(
|
||||
.type(.checkbox), .class("col-2"), .name("truckStock"), .style("margin-top: 20px;")
|
||||
)
|
||||
}
|
||||
}
|
||||
if let purchaseOrder, let createdAt = purchaseOrder.createdAt {
|
||||
div(.class("row")) {
|
||||
label(.class("label col-2")) { "Created:" }
|
||||
h3(.class("col-2")) { dateFormatter.string(from: createdAt) }
|
||||
if let updatedAt = purchaseOrder.updatedAt {
|
||||
div(.class("col-1")) {}
|
||||
label(.class("label col-2")) { "Updated:" }
|
||||
h3(.class("col-2")) { dateFormatter.string(from: updatedAt) }
|
||||
}
|
||||
}
|
||||
}
|
||||
div(.class("btn-row")) {
|
||||
button(.class("btn-primary"), .type(.submit)) { buttonLabel }
|
||||
if purchaseOrder != nil {
|
||||
Button.danger { "Delete" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var buttonLabel: String {
|
||||
guard purchaseOrder != nil else { return "Create" }
|
||||
return "Update"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import Elementary
|
||||
import ElementaryHTMX
|
||||
import SharedModels
|
||||
import Vapor
|
||||
|
||||
struct PurchaseOrderSearch: HTML {
|
||||
|
||||
typealias Context = SharedModels.ViewRoute.PurchaseOrderRoute.Search.Context
|
||||
|
||||
let context: Context
|
||||
|
||||
init(context: Context? = nil) {
|
||||
self.context = context ?? .employee
|
||||
}
|
||||
|
||||
var content: some HTML {
|
||||
form(
|
||||
.id(.purchaseOrder(.search)),
|
||||
.hx.post(route: .purchaseOrder(.search(.index()))),
|
||||
.hx.target(.id(.purchaseOrder())),
|
||||
.hx.swap(.outerHTML)
|
||||
) {
|
||||
div(.class("btn-row")) {
|
||||
button(
|
||||
.class("btn-secondary"), .style("position: absolute; top: 80px; right: 20px;"),
|
||||
.hx.get(route: .purchaseOrder(.index)), .hx.pushURL(true), .hx.target("body")
|
||||
)
|
||||
{ "x" }
|
||||
}
|
||||
div(.class("row")) {
|
||||
select(
|
||||
.name("context"), .class("col-3"),
|
||||
.hx.get(route: .purchaseOrder(.search(.index()))),
|
||||
.hx.target(.id(.purchaseOrder(.search))),
|
||||
.hx.swap(.outerHTML.transition(true).swap("0.5s")),
|
||||
.hx.pushURL(true)
|
||||
) {
|
||||
for context in Context.allCases {
|
||||
option(.value(context.rawValue)) { context.rawValue.capitalized }
|
||||
.attributes(.selected, when: self.context == context)
|
||||
}
|
||||
}
|
||||
|
||||
if context == .employee {
|
||||
EmployeeSelect.purchaseOrderSearch()
|
||||
} else if context == .customer {
|
||||
input(
|
||||
.type(.text), .class("col-6"), .style("margin-left: 60px; margin-top: 18px;"),
|
||||
.name("customerSearch"), .placeholder("Search"), .required
|
||||
)
|
||||
} else if context == .vendor {
|
||||
VendorBranchSelect.purchaseOrderSearch()
|
||||
}
|
||||
}
|
||||
|
||||
div(.class("btn-row")) {
|
||||
button(.type(.submit), .class("btn-primary"))
|
||||
{ "Search" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
import Elementary
|
||||
import ElementaryHTMX
|
||||
import Fluent
|
||||
import SharedModels
|
||||
import Vapor
|
||||
|
||||
struct PurchaseOrderTable: HTML {
|
||||
typealias SearchContext = SharedModels.ViewRoute.PurchaseOrderRoute.Search.Context
|
||||
|
||||
let page: Page<PurchaseOrder>
|
||||
let context: Context
|
||||
let searchContext: SearchContext?
|
||||
|
||||
init(
|
||||
page: Page<PurchaseOrder>,
|
||||
context: Context = .default,
|
||||
searchContext: SearchContext? = nil
|
||||
) {
|
||||
self.page = page
|
||||
self.context = context
|
||||
self.searchContext = searchContext
|
||||
}
|
||||
|
||||
var content: some HTML {
|
||||
table(.id(.purchaseOrder())) {
|
||||
thead {
|
||||
buttonRow
|
||||
tableHeader
|
||||
}
|
||||
tbody(.id(.purchaseOrder(.table))) {
|
||||
Rows(page: page)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var tableHeader: some HTML<HTMLTag.tr> {
|
||||
tr {
|
||||
th { "PO" }
|
||||
th { "Work Order" }
|
||||
th { "Customer" }
|
||||
th { "Vendor" }
|
||||
th { "Materials" }
|
||||
th { "Created For" }
|
||||
th {
|
||||
if context != .search {
|
||||
Button.add()
|
||||
.attributes(
|
||||
.hx.get(route: .purchaseOrder(.form)), .hx.target(.id(.float)),
|
||||
.hx.swap(.outerHTML), .hx.pushURL(true)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var buttonRow: some HTML<HTMLTag.tr> {
|
||||
tr {
|
||||
div(.class("btn-row")) {
|
||||
if context != .search {
|
||||
button(
|
||||
.id("btn-search"),
|
||||
.class("btn-primary"), .style("position: absolute; top: 80px; right: 20px;"),
|
||||
.hx.get(route: .purchaseOrder(.search(.index(context: .employee, table: true)))),
|
||||
.hx.target(.body),
|
||||
.hx.swap(.outerHTML.transition(true).swap("0.5s")),
|
||||
.hx.pushURL(true)
|
||||
)
|
||||
{ Img.search() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Produces only the rows for the given page
|
||||
struct Rows: HTML {
|
||||
let page: Page<PurchaseOrder>
|
||||
|
||||
var content: some HTML {
|
||||
for purchaseOrder in page.items {
|
||||
Row(purchaseOrder: purchaseOrder)
|
||||
}
|
||||
// We set page to 0 when we're on search, but have not completed the search
|
||||
// form yet, so don't add the infinite scroll row / trigger otherwise it will
|
||||
// load the first page, which is not what we want, but we need the empty table
|
||||
// to be available once the search form is completed.
|
||||
if page.metadata.page > 0, page.metadata.pageCount > page.metadata.page {
|
||||
tr(
|
||||
.hx.get(route: .purchaseOrder(.page(page: page.metadata.page + 1, limit: page.metadata.per))),
|
||||
.hx.trigger(.event(.revealed)),
|
||||
.hx.swap(.outerHTML.transition(true).swap("1s")),
|
||||
.hx.target(.this),
|
||||
.hx.indicator("next .htmx-indicator")
|
||||
) {
|
||||
img(.src("/images/spinner.svg"), .class("htmx-indicator"), .width(60), .height(60))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A single row.
|
||||
struct Row: HTML {
|
||||
let purchaseOrder: PurchaseOrder
|
||||
|
||||
var content: some HTML<HTMLTag.tr> {
|
||||
tr(
|
||||
.id(.purchaseOrder(.row(id: purchaseOrder.id)))
|
||||
) {
|
||||
td { "\(purchaseOrder.id)" }
|
||||
td { purchaseOrder.workOrder != nil ? String(purchaseOrder.workOrder!) : "" }
|
||||
td { purchaseOrder.customer }
|
||||
td { purchaseOrder.vendorBranch.displayName }
|
||||
td { purchaseOrder.materials }
|
||||
td { purchaseOrder.createdFor.fullName }
|
||||
td {
|
||||
Button.detail()
|
||||
.attributes(
|
||||
.hx.get(route: .purchaseOrder(.get(id: purchaseOrder.id))),
|
||||
.hx.target(.id(.float)),
|
||||
.hx.swap(.outerHTML.transition(true).swap("0.5s")),
|
||||
.hx.pushURL(true)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Context: String {
|
||||
case `default`
|
||||
case search
|
||||
}
|
||||
}
|
||||
|
||||
private extension VendorBranch.Detail {
|
||||
var displayName: String {
|
||||
"\(vendor.name.capitalized) - \(name.capitalized)"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user