fix: only update the payments on payment creation

This commit is contained in:
Ruidy 2025-01-25 00:20:12 +01:00
parent 541c813be0
commit b8265cf80f
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
7 changed files with 87 additions and 88 deletions

View file

@ -27,7 +27,9 @@ func handleCreatePayment(bs *booking.Service) echo.HandlerFunc {
}
np := new(CreatePaymentInput)
c.Bind(np)
if err := c.Bind(np); err != nil {
return err
}
b := bs.One(id)
@ -38,26 +40,15 @@ func handleCreatePayment(bs *booking.Service) echo.HandlerFunc {
nb := bs.One(id)
return renderTempl(c, http.StatusOK, view.ItemList(view.ItemListViewModel{
Items: u.Map(nb.Items, func(i booking.Item) view.ItemViewModel {
return view.ItemViewModel{
Id: strconv.Itoa(i.Id),
Item: i.Item,
Quantity: strconv.Itoa(i.Quantity),
Price: strconv.FormatFloat(i.Price, 'f', 2, 64),
SubTotal: strconv.FormatFloat(i.Price*float64(i.Quantity), 'f', 2, 64),
PaymentStatus: i.PaymentStatus,
ItemUrl: fmt.Sprintf("%s/%d", constant.RouteItem, i.Id),
}
}),
Payments: u.Map(nb.Payments, func(p booking.Payment) view.PaymentViewModel {
return view.PaymentViewModel{
return renderTempl(c, http.StatusOK, view.PaymentList(
u.Map(nb.Payments, func(p booking.Payment) *view.PaymentViewModel {
return &view.PaymentViewModel{
Amount: strconv.FormatFloat(p.Amount, 'f', 2, 64),
PaymentMethod: p.PaymentMethod,
PaymentUrl: fmt.Sprintf("%s/%d", constant.RoutePayment, p.ID),
}
}),
}))
))
}
}

View file

@ -135,7 +135,7 @@ templ PaymentModal(paymentUrl string) {
<form
class="py-4 space-y-4"
hx-post={ paymentUrl }
hx-target="#line-items"
hx-target="#payment-lines"
hx-swap="outerHTML"
hx-on::after-request="if(event.detail.successful) payment_modal.close()"
>

View file

@ -275,7 +275,7 @@ func PaymentModal(paymentUrl string) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 22, "\" hx-target=\"#line-items\" hx-swap=\"outerHTML\" hx-on::after-request=\"if(event.detail.successful) payment_modal.close()\"><div class=\"form-control w-full\"><label class=\"label\"><span class=\"label-text\">Amount</span></label> <input type=\"number\" step=\"0.01\" name=\"amount\" class=\"input input-bordered w-full\" required autofocus></div><div class=\"form-control w-full\"><label class=\"label\"><span class=\"label-text\">Payment Method</span></label> <select name=\"paymentMethod\" class=\"select select-bordered w-full\" required><option value=\"\">Select payment method</option> <option value=\"Cash\">Cash</option> <option value=\"Card\">Card</option> <option value=\"Cheque\">Cheque</option> <option value=\"Transfer\">Bank Transfer</option></select></div><button type=\"submit\" class=\"btn btn-primary\">Add Payment </button></form></div></dialog>")
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 22, "\" hx-target=\"#payment-lines\" hx-swap=\"outerHTML\" hx-on::after-request=\"if(event.detail.successful) payment_modal.close()\"><div class=\"form-control w-full\"><label class=\"label\"><span class=\"label-text\">Amount</span></label> <input type=\"number\" step=\"0.01\" name=\"amount\" class=\"input input-bordered w-full\" required autofocus></div><div class=\"form-control w-full\"><label class=\"label\"><span class=\"label-text\">Payment Method</span></label> <select name=\"paymentMethod\" class=\"select select-bordered w-full\" required><option value=\"\">Select payment method</option> <option value=\"Cash\">Cash</option> <option value=\"Card\">Card</option> <option value=\"Cheque\">Cheque</option> <option value=\"Transfer\">Bank Transfer</option></select></div><button type=\"submit\" class=\"btn btn-primary\">Add Payment </button></form></div></dialog>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}

View file

@ -16,19 +16,10 @@ templ ItemList(itemList ItemListViewModel) {
<th></th>
</tr>
</thead>
<tbody>
<tbody id="payment-lines">
if len(itemList.Payments) >0 {
for _,payment := range itemList.Payments {
<tr class="hover">
<td></td>
<td></td>
<td>- { payment.Amount }</td>
<td>{ payment.PaymentMethod }</td>
<td></td>
<td class="flex gap-2">
<button class="btn btn-sm btn-outline" hx-get={ payment.PaymentUrl } hx-target="closest tr" hx-swap="outerHTML">Edit</button>
</td>
</tr>
@PaymentLine(&payment)
}
}
</tbody>

View file

@ -39,58 +39,19 @@ func ItemList(itemList ItemListViewModel) templ.Component {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</tbody> <thead><tr><th></th><th></th><th>Price (€)</th><th>Payment Method</th><th>Sub-total (€)</th><th></th></tr></thead> <tbody>")
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</tbody> <thead><tr><th></th><th></th><th>Price (€)</th><th>Payment Method</th><th>Sub-total (€)</th><th></th></tr></thead> <tbody id=\"payment-lines\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if len(itemList.Payments) > 0 {
for _, payment := range itemList.Payments {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "<tr class=\"hover\"><td></td><td></td><td>- ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(payment.Amount)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/item_list.templ`, Line: 25, Col: 27}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</td><td>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(payment.PaymentMethod)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/item_list.templ`, Line: 26, Col: 32}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "</td><td></td><td class=\"flex gap-2\"><button class=\"btn btn-sm btn-outline\" hx-get=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(payment.PaymentUrl)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/item_list.templ`, Line: 29, Col: 72}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "\" hx-target=\"closest tr\" hx-swap=\"outerHTML\">Edit</button></td></tr>")
templ_7745c5c3_Err = PaymentLine(&payment).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "</tbody>")
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</tbody>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}

View file

@ -8,7 +8,12 @@ templ PaymentLine(payment *PaymentViewModel) {
<td>{ payment.PaymentMethod }</td>
<td></td>
<td class="flex gap-2">
<button class="btn btn-sm btn-outline" hx-get={ payment.PaymentUrl } hx-target="closest tr" hx-swap="outerHTML">Edit</button>
<button
class="btn btn-sm btn-outline"
hx-get={ payment.PaymentUrl }
hx-target="closest tr"
hx-swap="outerHTML"
>Edit</button>
</td>
</tr>
}
@ -19,21 +24,21 @@ templ PaymentForm(payment *PaymentViewModel) {
<td></td>
<td></td>
<td>
<input
class="input input-bordered input-sm w-full"
type="number"
inputmode="decimal"
step="0.01"
value={ payment.Amount }
name="amount"
<input
class="input input-bordered input-sm w-full"
type="number"
inputmode="decimal"
step="0.01"
value={ payment.Amount }
name="amount"
form="edit-payment"
/>
</td>
<td>
<input
class="input input-bordered input-sm w-full"
value={ payment.PaymentMethod }
name="paymentMethod"
<input
class="input input-bordered input-sm w-full"
value={ payment.PaymentMethod }
name="paymentMethod"
form="edit-payment"
/>
</td>
@ -43,4 +48,14 @@ templ PaymentForm(payment *PaymentViewModel) {
</td>
</form>
</tr>
}
}
templ PaymentList(payments []*PaymentViewModel) {
<tbody id="payment-lines">
if len(payments) >0 {
for _,payment := range payments {
@PaymentLine(payment)
}
}
</tbody>
}

View file

@ -62,7 +62,7 @@ func PaymentLine(payment *PaymentViewModel) templ.Component {
var templ_7745c5c3_Var4 string
templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(payment.PaymentUrl)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/payment.templ`, Line: 11, Col: 69}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/payment.templ`, Line: 13, Col: 31}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4))
if templ_7745c5c3_Err != nil {
@ -104,7 +104,7 @@ func PaymentForm(payment *PaymentViewModel) templ.Component {
var templ_7745c5c3_Var6 string
templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(payment.PaymentUrl)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/payment.templ`, Line: 18, Col: 35}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/payment.templ`, Line: 23, Col: 35}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
if templ_7745c5c3_Err != nil {
@ -117,7 +117,7 @@ func PaymentForm(payment *PaymentViewModel) templ.Component {
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(payment.Amount)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/payment.templ`, Line: 27, Col: 27}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/payment.templ`, Line: 32, Col: 27}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {
@ -130,7 +130,7 @@ func PaymentForm(payment *PaymentViewModel) templ.Component {
var templ_7745c5c3_Var8 string
templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(payment.PaymentMethod)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/payment.templ`, Line: 35, Col: 34}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/payment.templ`, Line: 40, Col: 34}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8))
if templ_7745c5c3_Err != nil {
@ -144,4 +144,45 @@ func PaymentForm(payment *PaymentViewModel) templ.Component {
})
}
func PaymentList(payments []*PaymentViewModel) templ.Component {
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
return templ_7745c5c3_CtxErr
}
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
if !templ_7745c5c3_IsBuffer {
defer func() {
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
if templ_7745c5c3_Err == nil {
templ_7745c5c3_Err = templ_7745c5c3_BufErr
}
}()
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var9 := templ.GetChildren(ctx)
if templ_7745c5c3_Var9 == nil {
templ_7745c5c3_Var9 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "<tbody id=\"payment-lines\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if len(payments) > 0 {
for _, payment := range payments {
templ_7745c5c3_Err = PaymentLine(payment).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 10, "</tbody>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
return nil
})
}
var _ = templruntime.GeneratedTemplate