commit 9881d9a73bc053d5e0ce09ab20b9769d8785dd4f
parent 1cb10633b6b4a49c8e8549d55b19e8eb4398a8ff
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Wed, 13 Dec 2023 05:39:32 -0500
improve withdraw UX
Diffstat:
3 files changed, 26 insertions(+), 10 deletions(-)
diff --git a/pkg/web/handlers/data.go b/pkg/web/handlers/data.go
@@ -934,6 +934,9 @@ type pokerData struct {
Img string
Tables []database.PokerTable
Transactions []database.PokerXmrTransaction
+ WithdrawAmount int
+ WithdrawAddress string
+ Error string
}
type powHelperData struct {
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -782,13 +782,19 @@ func PokerHomeHandler(c echo.Context) error {
}
if c.Request().Method == http.MethodPost {
- withdrawChips := utils.DoParseInt(c.Request().PostFormValue("withdraw_amount"))
- withdrawAddress := c.Request().PostFormValue("withdraw_address")
+ data.WithdrawAmount = utils.DoParseInt(c.Request().PostFormValue("withdraw_amount"))
+ data.WithdrawAddress = c.Request().PostFormValue("withdraw_address")
+ if len(data.WithdrawAddress) != 95 {
+ data.Error = "invalid xmr address"
+ return c.Render(http.StatusOK, "poker", data)
+ }
+ withdrawChips := data.WithdrawAmount
userChips := authUser.GetChips(true)
chips := utils.Clamp(withdrawChips, 100, userChips)
chips = utils.MinInt(chips, userChips)
if chips < 100 { // not enough for withdraw
- return c.Redirect(http.StatusFound, "/poker")
+ data.Error = "minimum withdraw amount is 100 chips"
+ return c.Render(http.StatusOK, "poker", data)
}
authUser.IncrChips(-chips, true)
authUser.DoSave(db)
@@ -798,19 +804,21 @@ func PokerHomeHandler(c echo.Context) error {
res, err := config.Xmr().Transfer(&wallet1.RequestTransfer{
Destinations: []*wallet1.Destination{
- {Address: withdrawAddress,
+ {Address: data.WithdrawAddress,
Amount: uint64(chips) * 100_000_000},
{Address: pokerHouseXmrAddressStag,
Amount: uint64(fee) * 100_000_000}}})
if err != nil {
logrus.Error(err)
- return c.Redirect(http.StatusFound, "/poker")
+ data.Error = err.Error()
+ return c.Render(http.StatusOK, "poker", data)
}
if _, err := db.CreatePokerXmrTransaction(authUser.ID, res); err != nil {
logrus.Error(err)
- return c.Redirect(http.StatusFound, "/poker")
+ data.Error = err.Error()
+ return c.Render(http.StatusOK, "poker", data)
}
- return c.Redirect(http.StatusFound, "/poker")
+ return c.Redirect(http.StatusFound, c.Request().Referer())
}
return c.Render(http.StatusOK, "poker", data)
diff --git a/pkg/web/public/views/pages/poker.gohtml b/pkg/web/public/views/pages/poker.gohtml
@@ -15,12 +15,17 @@
Balance: {{ .Data.Balance }} chips
</div>
<div class="mb-3">
- <form method="post">
+ <hr />
+ {{ if .Data.Error }}
+ <div class="alert alert-danger">{{ .Data.Error }}</div>
+ {{ end }}
+ <form method="post" class="form-inline">
<input type="hidden" name="csrf" value="{{ .CSRF }}" />
- <input type="text" name="withdraw_address" placeholder="address" class="form-control" style="width: 400px;" />
- <input type="number" name="withdraw_amount" placeholder="amount" class="form-control" style="width: 150px;" />
+ <input type="text" name="withdraw_address" value="{{ .Data.WithdrawAddress }}" placeholder="address" class="form-control mr-2" style="width: 400px;" />
+ <input type="number" name="withdraw_amount" value="{{ .Data.WithdrawAmount }}" placeholder="amount" class="form-control mr-2" style="width: 150px;" />
<button class="btn btn-primary">Withdraw</button>
</form>
+ <hr />
</div>
<h3>Tables</h3>
<table class="table table-novpadding table-sm table-hover table-striped">