2024-10-02 16:40:58 -05:00

80 lines
2.0 KiB
Plaintext

package components
import (
"fmt"
"git.ekzyis.com/ekzyis/delphi.market/types"
"strconv"
"math"
)
templ MarketForm(m types.Market, outcome int, q types.MarketQuote, uQ int) {
<form
id={ formId(outcome) }
autocomplete="off"
class="grid grid-cols-2 gap-3"
hx-post={ fmt.Sprintf("/market/%d/order", m.Id) }
hx-target="#modal"
hx-swap="outerHTML"
hx-select="#modal"
>
<input type="hidden" name="o" value={ fmt.Sprint(outcome) }/>
<div class="none col-span-2 htmx-request"></div>
<label for="p">price:</label>
<div id="p">{ formatPrice(q.AvgPrice, 3) }</div>
<label for="q">shares:</label>
<input
id={ inputId(outcome) }
name="q"
class="text-black px-1"
type="number"
step="100"
min="0"
autofocus
hx-get={ fmt.Sprintf("/market/%d", m.Id) }
hx-replace-url="true"
hx-target={ fmt.Sprintf("#%s", formId(outcome)) }
hx-swap="outerHTML"
hx-select={ fmt.Sprintf("#%s", formId(outcome)) }
hx-trigger="input changed delay:1s"
hx-preserve
hx-disabled-elt="next button"
hx-indicator={ hxIndicator(outcome) }
/>
<label for="total">total:</label>
<div id="total">{ formatPrice(q.TotalPrice, 3) }</div>
<label for="reward">{ "potential earn:" }</label>
<span id="reward" class={ colorize(q.Reward) }>+{ formatPrice(q.Reward, 3) }</span>
<label for="uQ">you have:</label>
<div id="uQ">{ fmt.Sprint(uQ) }</div>
<button type="submit" class="col-span-2">submit</button>
</form>
}
func formId(outcome int) string {
return fmt.Sprintf("outcome-%d-form", outcome)
}
func inputId(outcome int) string {
return fmt.Sprintf("outcome-%d-q", outcome)
}
func hxIndicator(outcome int) string {
return fmt.Sprintf(
"#%s>#p, #%s>#total, #%s>#reward",
formId(outcome), formId(outcome), formId(outcome))
}
func formatPrice(p float64, n int) string {
if p == 0 || math.IsNaN(p) {
return "0 sats"
}
return fmt.Sprintf("%v sats", strconv.FormatFloat(p, 'f', n, 64))
}
func colorize(f float64) string {
eps := 1e-5
if f-eps > 0 {
return "text-success"
}
return "text-reset"
}