feat working on vendor views.
This commit is contained in:
@@ -3,31 +3,70 @@ import ElementaryHTMX
|
||||
import SharedModels
|
||||
|
||||
struct VendorForm: HTML {
|
||||
let vendor: Vendor?
|
||||
|
||||
var content: some HTML<HTMLTag.form> {
|
||||
let context: Context
|
||||
var vendor: Vendor? { context.vendor }
|
||||
|
||||
init(
|
||||
_ context: Context
|
||||
) {
|
||||
self.context = context
|
||||
}
|
||||
|
||||
init() { self.init(.float(nil)) }
|
||||
|
||||
enum Context {
|
||||
case float(Vendor? = nil, shouldShow: Bool = false)
|
||||
case formOnly(Vendor)
|
||||
|
||||
var vendor: Vendor? {
|
||||
switch self {
|
||||
case let .float(vendor, _): return vendor
|
||||
case let .formOnly(vendor): return vendor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var content: some HTML {
|
||||
switch context {
|
||||
case let .float(vendor, shouldDisplay):
|
||||
Float(shouldDisplay: shouldDisplay) {
|
||||
makeForm(vendor: vendor)
|
||||
}
|
||||
case let .formOnly(vendor):
|
||||
makeForm(vendor: vendor)
|
||||
}
|
||||
}
|
||||
|
||||
func makeForm(vendor: Vendor?) -> some HTML {
|
||||
form(
|
||||
.id("vendor-form"),
|
||||
vendor != nil ? .hx.put(targetURL) : .hx.post(targetURL),
|
||||
.hx.target("this"),
|
||||
.hx.target("#float"),
|
||||
.hx.swap(.outerHTML)
|
||||
) {
|
||||
div(.class("row")) {
|
||||
input(
|
||||
.type(.text),
|
||||
.class("col-9"),
|
||||
.id("vendor-name"),
|
||||
.name("name"),
|
||||
.value(vendor?.name ?? ""),
|
||||
.placeholder("Vendor Name"),
|
||||
.required
|
||||
)
|
||||
button(.type(.submit), .class("btn-primary")) { buttonLabel }
|
||||
button(
|
||||
.type(.submit),
|
||||
.class("col-1 btn-primary"),
|
||||
.style("float: right")
|
||||
) { buttonLabel }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var buttonLabel: String {
|
||||
guard vendor != nil else { return "Update" }
|
||||
return "Create"
|
||||
guard vendor != nil else { return "Create" }
|
||||
return "Update"
|
||||
}
|
||||
|
||||
var targetURL: String {
|
||||
|
||||
Reference in New Issue
Block a user