delphi.market/server/router/pages/components/marketForm.templ

67 lines
1.8 KiB
Plaintext
Raw Normal View History

2024-08-25 23:51:10 +00:00
package components
import (
2024-09-05 21:11:29 +00:00
"fmt"
"git.ekzyis.com/ekzyis/delphi.market/types"
"strconv"
2024-08-25 23:51:10 +00:00
)
2024-08-26 11:15:33 +00:00
templ MarketForm(m types.Market, outcome int, q types.MarketQuote, uQ int) {
2024-09-05 21:11:29 +00:00
<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">avg price per share:</label>
<div id="p">{ formatPrice(q.AvgPrice) }</div>
<label for="q">how many?</label>
<input
id={ inputId(outcome) }
name="q"
class="text-black px-1"
type="number"
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">you pay:</label>
<div id="total">{ formatPrice(q.TotalPrice) }</div>
<label for="reward">{ "if you win:" }</label>
<div id="reward">+{ formatPrice(q.Reward) }</div>
<label for="uQ">you have:</label>
<div id="uQ">{ fmt.Sprint(uQ) }</div>
<button type="submit" class="col-span-2">submit</button>
</form>
2024-08-25 23:51:10 +00:00
}
2024-09-05 21:11:29 +00:00
func formId(outcome int) string {
return fmt.Sprintf("outcome-%d-form", outcome)
2024-08-25 23:51:10 +00:00
}
2024-09-05 21:11:29 +00:00
func inputId(outcome int) string {
return fmt.Sprintf("outcome-%d-q", outcome)
2024-08-25 23:51:10 +00:00
}
2024-09-05 21:11:29 +00:00
func hxIndicator(outcome int) string {
return fmt.Sprintf(
"#%s>#p, #%s>#total, #%s>#reward",
formId(outcome), formId(outcome), formId(outcome))
2024-08-25 23:51:10 +00:00
}
func formatPrice(p float64) string {
return fmt.Sprintf("%v sats", strconv.FormatFloat(p, 'f', 3, 64))
}