feat: Adds employee views.
This commit is contained in:
63
Sources/App/Views/Employees/EmployeeForm.swift
Normal file
63
Sources/App/Views/Employees/EmployeeForm.swift
Normal file
@@ -0,0 +1,63 @@
|
||||
import Elementary
|
||||
import ElementaryHTMX
|
||||
import SharedModels
|
||||
|
||||
struct EmployeeForm: HTML {
|
||||
let employee: Employee?
|
||||
let shouldShow: Bool
|
||||
|
||||
init(employee: Employee? = nil, shouldShow: Bool = false) {
|
||||
self.employee = employee
|
||||
self.shouldShow = shouldShow
|
||||
}
|
||||
|
||||
init(employee: Employee) {
|
||||
self.employee = employee
|
||||
self.shouldShow = true
|
||||
}
|
||||
|
||||
var content: some HTML {
|
||||
Float(shouldDisplay: shouldShow, resetURL: "/employees") {
|
||||
form(
|
||||
employee == nil ? .hx.post(targetURL) : .hx.put(targetURL),
|
||||
employee == nil ? .hx.target("#employee-table") : .hx.target("#employee_\(employee!.id)"),
|
||||
employee == nil
|
||||
? .hx.swap(.beforeEnd.transition(true).swap("0.5s"))
|
||||
: .hx.swap(.outerHTML.transition(true).swap("0.5s")),
|
||||
.custom(
|
||||
name: "hx-on::after-request",
|
||||
value: "if (event.detail.successful) toggleContent('float'); window.location.href='/employees';"
|
||||
)
|
||||
) {
|
||||
div(.class("row")) {
|
||||
input(
|
||||
.type(.text), .class("col-5"),
|
||||
.name("firstName"), .value(employee?.firstName ?? ""),
|
||||
.placeholder("First Name"), .required
|
||||
)
|
||||
div(.class("col-2")) {}
|
||||
input(
|
||||
.type(.text), .class("col-5"),
|
||||
.name("lastName"), .value(employee?.lastName ?? ""),
|
||||
.placeholder("Last Name"), .required
|
||||
)
|
||||
}
|
||||
div(.class("btn-row")) {
|
||||
button(.type(.submit), .class("btn-primary")) {
|
||||
buttonLabel
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var buttonLabel: String {
|
||||
guard employee != nil else { return "Create" }
|
||||
return "Update"
|
||||
}
|
||||
|
||||
private var targetURL: String {
|
||||
guard let employee else { return "/employees" }
|
||||
return "/employees/\(employee.id)"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user