Skip to content

Instantly share code, notes, and snippets.

@timperrett
Created May 4, 2010 22:28
Show Gist options
  • Save timperrett/390121 to your computer and use it in GitHub Desktop.
Save timperrett/390121 to your computer and use it in GitHub Desktop.
class Details extends StatefulSnippet with AuctionHelpers with Loggable {
val dispatch: DispatchIt = {
case "show" => show _
case "bid" => bid _
}
val currentAmountSpan = "current_amount"
val nextAmountSpan = "next_amount"
val amountInputId = "amount"
val auction = Auction.find(By(Auction.id,S.param("id").map(_.toLong).openOr(0L)))
def boxToNoticeWithJsCmd[T](win: String, loose: String)(f: => Box[T]){
f match {
case Full(value) => S.notice(win)
case Failure(msg,_,_) => S.error(msg)
case _ => S.warning(loose)
}
}
def bid(xhtml: NodeSeq): NodeSeq =
if(!Customer.loggedIn_?){
S.warning("You must be logged in to bid on auctions.")
NodeSeq.Empty
} else {
var amountBox: Box[String] = Empty
def submit =
boxToNoticeWithJsCmd(
"Your bid was accepted!",
"Unable to place bid at this time."){
for(ass <- amountBox ?~! "Amount is not a number";
amo <- tryo(BigDecimal(ass).doubleValue) ?~! "Amount is not a number";
auc <- auction;
vld <- tryo(amo).filter(_ >= auc.nextAmount) ?~ "Your bid is lower than required!"
) yield (
new Bid().auction(auction).customer(Customer.currentUser).amount(amo).saveMe
)
}
SHtml.ajaxForm(bind("b",xhtml,
"amount" -%> SHtml.text(amountBox.openOr(""), s => amountBox = Full(s)) % ("id" -> amountInputId),
"submit" -> SHtml.ajaxSubmit("Place Bid", {() => submit; Noop})
),
Noop,
SetHtml(currentAmountSpan, Text(amountBox.openOr("n/a"))) &
SetHtml(nextAmountSpan, Text(auction.map(_.nextAmount.toString).openOr("n/a"))) &
SetValueAndFocus(amountInputId, ""))
}
def show(xhtml: NodeSeq): NodeSeq = auction.map(a =>
bind("a", single(a, xhtml),
"countdown" -> <span class="countdown">1:27:22</span>,
"current_amount" -> <span>{a.currentAmount.toString}</span> % ("id" -> currentAmountSpan),
"next_amount" -> <span>{a.nextAmount.toString}</span> % ("id" -> nextAmountSpan)
)).openOr(Text("That auction does not exist"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment