htmgo/sandbox/partials/patient/patient.go

144 lines
3.4 KiB
Go
Raw Normal View History

2024-09-11 21:08:35 +00:00
package patient
2024-09-11 17:31:40 +00:00
import (
2024-09-17 17:13:22 +00:00
"github.com/labstack/echo/v4"
2024-09-14 00:05:55 +00:00
"github.com/maddalax/htmgo/framework-ui/ui"
"github.com/maddalax/htmgo/framework/h"
2024-09-13 21:33:50 +00:00
"starter-template/features/patient"
"starter-template/partials/sheet"
"strings"
2024-09-11 17:31:40 +00:00
)
2024-09-17 17:13:22 +00:00
func List(ctx echo.Context) *h.Partial {
patients, err := patient.NewService(ctx).List()
2024-09-11 17:31:40 +00:00
if err != nil {
return h.NewPartial(h.Div(
h.Class("patient-list"),
h.Pf("Error loading patients"),
2024-09-11 17:31:40 +00:00
))
}
if len(patients) == 0 {
return h.NewPartial(h.Div(
h.Class("patient-list"),
h.Pf("No patients found"),
2024-09-11 17:31:40 +00:00
))
}
return h.NewPartial(h.Div(
2024-09-11 18:09:55 +00:00
h.Class("mt-8"),
2024-09-11 17:31:40 +00:00
h.Id("patient-list"),
2024-09-11 21:08:35 +00:00
h.List(patients, Row),
2024-09-11 17:31:40 +00:00
))
}
2024-09-17 17:13:22 +00:00
func AddPatientSheetPartial(ctx echo.Context) *h.Partial {
closePathQs := h.GetQueryParam(ctx, "onClosePath")
2024-09-12 02:06:34 +00:00
return h.NewPartialWithHeaders(
h.PushQsHeader(ctx, "adding", "true"),
AddPatientSheet(
h.Ternary(closePathQs != "", closePathQs, h.CurrentPath(ctx)),
),
2024-09-12 02:06:34 +00:00
)
}
func AddPatientSheet(onClosePath string) h.Renderable {
return sheet.Opened(
2024-09-11 18:09:55 +00:00
sheet.Props{
2024-09-12 02:06:34 +00:00
OnClosePath: onClosePath,
ClassName: "w-[400px] bg-gray-100 p-4",
2024-09-11 18:09:55 +00:00
Root: h.Div(
h.Class("flex flex-col gap-4"),
h.P(h.Text("Add Patient"), h.Class("text-lg font-bold")),
2024-09-11 18:09:55 +00:00
addPatientForm(),
),
2024-09-12 02:06:34 +00:00
})
2024-09-11 17:31:40 +00:00
}
2024-09-17 17:13:22 +00:00
func ValidateForm(ctx echo.Context) *h.Partial {
trigger := h.GetTriggerName(ctx)
value := ctx.FormValue(trigger)
if trigger == "name" {
if strings.ToLower(value) == "sydne" {
return h.NewPartial(h.Pf("that name is reserved"))
}
}
if trigger == "reason-for-visit" {
if strings.ToLower(value) == "arm hurts" {
return h.NewPartial(h.Pf("lol that reason is fake"))
}
}
if trigger == "location-name" {
if strings.ToLower(value) == "hospital" {
return h.NewPartial(h.Pf("that location is reserved"))
}
}
return h.NewPartial(h.Fragment())
}
func addPatientForm() h.Renderable {
2024-09-11 18:09:55 +00:00
return h.Form(
h.HxExtension("debug, trigger-children"),
h.Attribute("hx-target-5*", "#submit-error"),
2024-09-11 21:08:35 +00:00
h.Post(h.GetPartialPath(Create)),
2024-09-11 17:31:40 +00:00
h.Class("flex flex-col gap-2"),
2024-09-11 18:09:55 +00:00
ui.Input(ui.InputProps{
Type: "text",
Label: "Name",
Name: "name",
DefaultValue: "",
ValidationPath: h.GetPartialPath(ValidateForm),
2024-09-11 18:09:55 +00:00
}),
ui.Input(ui.InputProps{
Type: "text",
Label: "Reason for visit",
Name: "reason-for-visit",
ValidationPath: h.GetPartialPath(ValidateForm),
2024-09-11 18:09:55 +00:00
}),
ui.Input(ui.InputProps{
Type: "date",
Label: "Appointment Date",
Name: "appointment-date",
}),
ui.Input(ui.InputProps{
Type: "text",
Label: "Location Name",
Name: "location-name",
ValidationPath: h.GetPartialPath(ValidateForm),
2024-09-11 18:09:55 +00:00
}),
ui.PrimaryButton(ui.ButtonProps{
Text: "Add Patient",
Class: "rounded p-2",
Type: "submit",
}),
h.Div(
h.Id("submit-error"),
h.Class("text-red-500"),
),
2024-09-11 18:09:55 +00:00
)
}
func Row(patient *patient.Patient, index int) h.Renderable {
2024-09-11 18:09:55 +00:00
return h.Div(
h.Class("flex flex-col gap-2 rounded p-4", h.Ternary(index%2 == 0, "bg-red-100", "")),
2024-09-11 17:31:40 +00:00
h.Pf("Name: %s", patient.Name),
h.Pf("Reason for visit: %s", patient.ReasonForVisit),
)
}
func AddPatientButton() h.Renderable {
2024-09-11 17:31:40 +00:00
return ui.Button(ui.ButtonProps{
Id: "add-patient",
Text: "Add Patient",
Class: "bg-blue-700 text-white rounded p-2 h-12",
Trigger: "qs:adding, click",
Target: sheet.Id,
Get: h.GetPartialPathWithQs(AddPatientSheetPartial, "onClosePath=/patients"),
2024-09-11 17:31:40 +00:00
})
}