This repository has been archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Add Search UI #113
Merged
Merged
Add Search UI #113
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package controllers | ||
|
||
import com.example.auction.search.api.{SearchRequest, SearchService} | ||
import com.example.auction.user.api.UserService | ||
import com.example.auction.utils.PaginatedSequence | ||
import com.typesafe.config.Config | ||
import play.api.data.Forms.{nonEmptyText, _} | ||
import play.api.data.{Form, Mapping} | ||
import play.api.mvc.{ControllerComponents, _} | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
class SearchController(config: Config, | ||
searchService: SearchService, | ||
userService: UserService, | ||
controllerComponents: ControllerComponents)(implicit ec: ExecutionContext) | ||
extends AbstractAuctionController(userService, controllerComponents) { | ||
|
||
private val showInlineInstruction: Boolean = config.getBoolean("online-auction.instruction.show") | ||
private val pageSize: Int = config.getInt("items-search.page-size") | ||
|
||
def searchForm(): Action[AnyContent] = Action.async { implicit request => | ||
requireUser(loadNav(_).map { implicit nav => | ||
Ok(views.html.searchItems(showInlineInstruction = showInlineInstruction, | ||
form = SearchItemsForm.form.fill(SearchItemsForm()), | ||
optionalSearchItemPaginatedSequence = None)) | ||
}) | ||
} | ||
|
||
def search(): Action[AnyContent] = Action.async { implicit request => | ||
requireUser(user => | ||
loadNav(user).flatMap { implicit nav => | ||
SearchItemsForm.form.bindFromRequest().fold( | ||
errorForm => { | ||
Future.successful(Ok(views.html.searchItems( | ||
showInlineInstruction = showInlineInstruction, | ||
form = errorForm, | ||
optionalSearchItemPaginatedSequence = None))) | ||
}, | ||
searchItemsForm => { | ||
searchService.search(searchItemsForm.pageNumber, pageSize) | ||
.invoke(SearchRequest( | ||
if (searchItemsForm.keywords.isEmpty) None else Some(searchItemsForm.keywords), | ||
Some(searchItemsForm.maximumPrice.intValue()), | ||
Some(searchItemsForm.currency.name))) | ||
.map(searchResponse => { | ||
Ok(views.html.searchItems( | ||
showInlineInstruction = showInlineInstruction, | ||
form = SearchItemsForm.form.fill(searchItemsForm), | ||
optionalSearchItemPaginatedSequence = Some(PaginatedSequence( | ||
searchResponse.items, | ||
searchResponse.pageNo, | ||
searchResponse.pageSize, | ||
searchResponse.numResults)))) | ||
}) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
|
||
case class SearchItemsForm(keywords: String = "", | ||
maximumPrice: BigDecimal = 0.0, | ||
currency: Currency = Currency.USD, | ||
pageNumber: Int = 0) | ||
|
||
object SearchItemsForm { | ||
val currency: Mapping[Currency] = nonEmptyText | ||
.verifying("invalid.currency", c => Currency.isDefined(c)) | ||
.transform[Currency](Currency.valueOf, _.name) | ||
val form = Form(mapping( | ||
"keywords" -> text, | ||
"maximumPrice" -> bigDecimal, | ||
"currency" -> currency, | ||
"pageNumber" -> number(min = 0) | ||
)(SearchItemsForm.apply)(SearchItemsForm.unapply)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
@import com.example.auction.item.api.ItemStatus | ||
@import com.example.auction.item.api.ItemSummary | ||
@import com.example.auction.search.api.SearchItem | ||
@import com.example.auction.utils | ||
@import controllers.Currency | ||
@import helper._ | ||
@import controllers.{Currency => Currencies} | ||
|
||
@import java.util.Locale | ||
|
||
@import com.example.auction.utils.PaginatedSequence | ||
@(showInlineInstruction: Boolean, | ||
form: Form[SearchItemsForm], | ||
optionalSearchItemPaginatedSequence: Option[PaginatedSequence[SearchItem]] | ||
)(implicit nav: Nav, request: RequestHeader) | ||
|
||
@main(Messages("searchItems")) { | ||
|
||
<h2>@Messages("searchItems")</h2> | ||
|
||
@if(showInlineInstruction) { | ||
<p>@Messages("instruction.search")</p> | ||
|
||
<p>@Html(Messages("instruction.enableSearch", "<a href=\"https://github.com/lagom/online-auction-scala/blob/master/README.md\">README.md</a>"))</p> | ||
} | ||
|
||
@foundationForm(form, routes.SearchController.search()) { | ||
<div class="row"> | ||
<div class="medium-6 columns"> | ||
@inputText(form("keywords")) | ||
</div> | ||
</div> | ||
<div class="column row"> | ||
@inputText(form("maximumPrice"), 'type -> "number", 'min -> 0, 'value -> form("maximumPrice").value) | ||
@select( | ||
field = form("currency"), | ||
options = Currencies.values.map(c => c.name -> c.getDisplayName).sortBy(_._2) | ||
) | ||
</div> | ||
<input type="hidden" id="pageNumber" value="0" name="@form("pageNumber").name"/> | ||
|
||
<input class="button" type="submit" value="@Messages("searchItems")"/> | ||
} | ||
@if(optionalSearchItemPaginatedSequence.isDefined) { | ||
@defining(optionalSearchItemPaginatedSequence.get) { searchItemPaginatedSequence: PaginatedSequence[SearchItem] => | ||
|
||
@if(!searchItemPaginatedSequence.isEmpty) { | ||
<script> | ||
var updatePage = function (pageNum) { | ||
document.getElementById("pageNumber").value = pageNum; | ||
document.getElementById("searchForm").submit(); | ||
} | ||
</script> | ||
|
||
@if(searchItemPaginatedSequence.count < searchItemPaginatedSequence.pageSize) { | ||
<div>@Messages("searchFoundCount", searchItemPaginatedSequence.count)</div> | ||
} else { | ||
<div>@Messages("searchFoundCountWithPagination", | ||
searchItemPaginatedSequence.page * searchItemPaginatedSequence.pageSize + 1, | ||
searchItemPaginatedSequence.page * searchItemPaginatedSequence.pageSize + searchItemPaginatedSequence.items.size, | ||
searchItemPaginatedSequence.count)</div> | ||
} | ||
|
||
<table> | ||
|
||
<thead> | ||
<tr> | ||
<th>@Messages("title")</th> | ||
<th>@Messages("status")</th> | ||
<th>@Messages("currentPrice")</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
@for(item <- searchItemPaginatedSequence.items) { | ||
<tr> | ||
<td> | ||
<a href="@routes.ItemController.getItem(item.id)">@item.title</a> | ||
</td> | ||
<td>@item.itemStatus</td> | ||
<td> | ||
@if( | ||
item.itemStatus.equals(ItemStatus.Auction.toString) | ||
&& item.price.isDefined | ||
&& item.price.get > 0) { | ||
@Currency.valueOf(item.currencyId).format(item.price.get) | ||
} else { | ||
- | ||
} | ||
</td> | ||
</tr> | ||
<tr> | ||
<td colspan="4">@item.description</td> | ||
</tr> | ||
} | ||
</tbody> | ||
</table> | ||
@if(searchItemPaginatedSequence.count > searchItemPaginatedSequence.pageSize * (searchItemPaginatedSequence.page + 1)) { | ||
@foundationForm(form, routes.SearchController.search()) { | ||
@defining(form("keywords")) { field: Field => | ||
<input type="hidden" id="@field.id" value="@field.value" name="@field.name"/> | ||
} | ||
@defining(form("maximumPrice")) { field: Field => | ||
<input type="hidden" id="@field.id" value="@field.value" name="@field.name"/> | ||
} | ||
@defining(form("currency")) { field: Field => | ||
<input type="hidden" id="@field.id" value="@field.value" name="@field.name"/> | ||
} | ||
@defining(form("pageNumber")) { field: Field => | ||
<input type="hidden" id="@field.id" value="@(searchItemPaginatedSequence.page + 1)" name="@field.name"/> | ||
} | ||
|
||
<input class="button" type="submit" value="@Messages("nextResultPage")"/> | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,9 @@ | |
|
||
lagom.circuit-breaker.default.call-timeout = 5s | ||
|
||
online-auction.instruction.show = false | ||
|
||
play.application.loader = loader.WebGatewayLoader | ||
play.filters.headers.contentSecurityPolicy = "img-src 'self' data:; default-src 'self'" | ||
|
||
items-search.page-size = 3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is pretty low, but I wanted the pagination features to be easily demonstrable. Let me know if you think another value is more appropriate. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the java app defaults this to
true
, however, I've seen no traces of instructions in the scala implementation. Do we want to show the instructions?