get line form on click edit button

This commit is contained in:
Ruidy 2024-03-22 15:19:55 +01:00
parent 057f9991e9
commit b89a760aa3
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
5 changed files with 140 additions and 2 deletions

View file

@ -92,6 +92,12 @@ func (bs Service) PayItem(id int) *Item {
return i
}
func (bs Service) OneItem(id int) *Item {
i := &Item{Id: id}
bs.db.First(i)
return i
}
type Report struct {
Lines []*Line
Total float64

View file

@ -167,6 +167,28 @@ func handleUpdateBooking(bs *booking.Service, hc *config.Host) echo.HandlerFunc
}
}
func handleLineItemForm(bs *booking.Service) echo.HandlerFunc {
return func(c echo.Context) error {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
return err
}
i := bs.OneItem(id)
form := view.LineItemForm(&view.ItemViewModel{
Id: strconv.Itoa(i.Id),
Item: i.Item,
Quantity: strconv.Itoa(i.Quantity),
Price: strconv.FormatFloat(i.Price, 'f', 2, 64),
PaymentMethod: i.PaymentMethod,
PaymentStatus: i.PaymentStatus,
SubTotal: strconv.FormatFloat(i.Price*float64(i.Quantity), 'f', 2, 64),
ItemUrl: fmt.Sprintf("%s/%d", constant.RouteItem, i.Id),
})
return renderTempl(c, http.StatusOK, form)
}
}
func handleCreateItem(bs *booking.Service) echo.HandlerFunc {
return func(c echo.Context) error {
bookingIdStr := c.Param("id")

View file

@ -12,6 +12,7 @@ func (s Server) MountHandlers() {
s.Router.PUT("/bookings/:id", handleUpdateBooking(s.bs, s.hc))
s.Router.POST("/bookings/:id/items", handleCreateItem(s.bs))
s.Router.POST("/items/:id", handlePayItem(s.bs))
s.Router.GET("/items/:id", handleLineItemForm(s.bs))
s.Router.GET("/bookings/pdf/:id", handleCreateInvoicePdf(s.bs, s.ps, s.hc))
s.Router.GET("/reports", handleReportsPage())
s.Router.GET("/reports/do", handleComputeReport(s.bs, s.hc))

View file

@ -9,10 +9,26 @@ templ LineItem(item *ItemViewModel) {
<td>{ item.PaymentStatus }</td>
<td>{ item.SubTotal }</td>
<td>
<button class="secondary">Edit</button>
<button class="secondary" hx-get={ item.ItemUrl } hx-target="closest tr" hx-swap="outerHTML">Edit</button>
if item.PaymentStatus != "Completed" {
<button class="secondary" hx-post={ item.ItemUrl } hx-target="closest tr" hx-swap="outerHTML">Paid</button>
}
</td>
</tr>
}
templ LineItemForm(item *ItemViewModel) {
<tr>
<form hx-put={ item.ItemUrl }>
<td><input value={ item.Item } name="item"/></td>
<td><input value={ item.Quantity } name="quantity"/></td>
<td><input value={ item.Price } name="price"/></td>
<td><input value={ item.PaymentMethod } name="paymentMethod"/></td>
<td><input value={ item.PaymentStatus } name="PaymentStatus"/></td>
<td>{ item.SubTotal }</td>
<td>
<button class="secondary">Save</button>
</td>
</form>
</tr>
}

View file

@ -101,7 +101,15 @@ func LineItem(item *ItemViewModel) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</td><td><button class=\"secondary\">Edit</button> ")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</td><td><button class=\"secondary\" hx-get=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(item.ItemUrl))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" hx-target=\"closest tr\" hx-swap=\"outerHTML\">Edit</button> ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -129,3 +137,88 @@ func LineItem(item *ItemViewModel) templ.Component {
return templ_7745c5c3_Err
})
}
func LineItemForm(item *ItemViewModel) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var8 := templ.GetChildren(ctx)
if templ_7745c5c3_Var8 == nil {
templ_7745c5c3_Var8 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<tr><form hx-put=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(item.ItemUrl))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"><td><input value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(item.Item))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"item\"></td><td><input value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(item.Quantity))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"quantity\"></td><td><input value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(item.Price))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"price\"></td><td><input value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(item.PaymentMethod))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"paymentMethod\"></td><td><input value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(item.PaymentStatus))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"PaymentStatus\"></td><td>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var9 string
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(item.SubTotal)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/line_item.templ`, Line: 27, Col: 22}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</td><td><button class=\"secondary\">Save</button></td></form></tr>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}