-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from hmrc/MIBM-81
[MIBM-81][JR] /goods-destination
- Loading branch information
Showing
25 changed files
with
655 additions
and
11 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
app/uk/gov/hmrc/merchandiseinbaggagefrontend/controllers/GoodsDestinationController.scala
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,40 @@ | ||
/* | ||
* Copyright 2020 HM Revenue & Customs | ||
* | ||
*/ | ||
|
||
package uk.gov.hmrc.merchandiseinbaggagefrontend.controllers | ||
|
||
import javax.inject.{Inject, Singleton} | ||
import play.api.data.Form | ||
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents} | ||
import uk.gov.hmrc.merchandiseinbaggagefrontend.config.AppConfig | ||
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.GoodsDestinationFormProvider | ||
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.GoodsDestination | ||
import uk.gov.hmrc.merchandiseinbaggagefrontend.views.html.GoodsDestinationView | ||
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendBaseController | ||
|
||
@Singleton | ||
class GoodsDestinationController @Inject()( | ||
override val controllerComponents: MessagesControllerComponents, | ||
formProvider: GoodsDestinationFormProvider, | ||
view: GoodsDestinationView | ||
)(implicit val appConfig: AppConfig) extends FrontendBaseController { | ||
|
||
val form: Form[GoodsDestination] = formProvider() | ||
|
||
def onPageLoad(): Action[AnyContent] = Action { implicit request => | ||
Ok(view(form)) | ||
} | ||
|
||
//TODO implement once session storage has been done under MIBM-77 | ||
def onSubmit(): Action[AnyContent] = Action { implicit request => | ||
form | ||
.bindFromRequest() | ||
.fold( | ||
formWithErrors => BadRequest(view(formWithErrors)), | ||
value => Ok | ||
) | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
app/uk/gov/hmrc/merchandiseinbaggagefrontend/forms/GoodsDestinationFormProvider.scala
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,20 @@ | ||
/* | ||
* Copyright 2020 HM Revenue & Customs | ||
* | ||
*/ | ||
|
||
package uk.gov.hmrc.merchandiseinbaggagefrontend.forms | ||
|
||
import javax.inject.Inject | ||
import play.api.data.Form | ||
import uk.gov.hmrc.merchandiseinbaggagefrontend.forms.mappings.Mappings | ||
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.GoodsDestination | ||
|
||
class GoodsDestinationFormProvider @Inject() extends Mappings { | ||
|
||
def apply(): Form[GoodsDestination] = | ||
Form( | ||
"value" -> enumerable[GoodsDestination]("goodsDestination.error.required") | ||
) | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
app/uk/gov/hmrc/merchandiseinbaggagefrontend/forms/mappings/Formatters.scala
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,40 @@ | ||
/* | ||
* Copyright 2020 HM Revenue & Customs | ||
* | ||
*/ | ||
|
||
package uk.gov.hmrc.merchandiseinbaggagefrontend.forms.mappings | ||
|
||
import play.api.data.FormError | ||
import play.api.data.format.Formatter | ||
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.Enumerable | ||
|
||
|
||
trait Formatters { | ||
|
||
private[mappings] def stringFormatter(errorKey: String): Formatter[String] = new Formatter[String] { | ||
|
||
override def bind(key: String, data: Map[String, String]): Either[Seq[FormError], String] = | ||
data.get(key) match { | ||
case None | Some("") => Left(Seq(FormError(key, errorKey))) | ||
case Some(s) => Right(s) | ||
} | ||
|
||
override def unbind(key: String, value: String): Map[String, String] = | ||
Map(key -> value) | ||
} | ||
|
||
private[mappings] def enumerableFormatter[A](requiredKey: String, invalidKey: String)(implicit ev: Enumerable[A]): Formatter[A] = | ||
new Formatter[A] { | ||
|
||
private val baseFormatter = stringFormatter(requiredKey) | ||
|
||
override def bind(key: String, data: Map[String, String]): Either[Seq[FormError], A] = | ||
baseFormatter.bind(key, data).right.flatMap { str => | ||
ev.withName(str).map(Right.apply).getOrElse(Left(Seq(FormError(key, invalidKey)))) | ||
} | ||
|
||
override def unbind(key: String, value: A): Map[String, String] = | ||
baseFormatter.unbind(key, value.toString) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
app/uk/gov/hmrc/merchandiseinbaggagefrontend/forms/mappings/Mappings.scala
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,17 @@ | ||
/* | ||
* Copyright 2020 HM Revenue & Customs | ||
* | ||
*/ | ||
|
||
package uk.gov.hmrc.merchandiseinbaggagefrontend.forms.mappings | ||
|
||
import play.api.data.FieldMapping | ||
import play.api.data.Forms.of | ||
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.Enumerable | ||
|
||
trait Mappings extends Formatters { | ||
|
||
protected def enumerable[A](requiredKey: String = "error.required", invalidKey: String = "error.invalid")( | ||
implicit ev: Enumerable[A]): FieldMapping[A] = | ||
of(enumerableFormatter[A](requiredKey, invalidKey)) | ||
} |
44 changes: 44 additions & 0 deletions
44
app/uk/gov/hmrc/merchandiseinbaggagefrontend/model/Enumerable.scala
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,44 @@ | ||
/* | ||
* Copyright 2020 HM Revenue & Customs | ||
* | ||
*/ | ||
|
||
package uk.gov.hmrc.merchandiseinbaggagefrontend.model | ||
|
||
import play.api.libs.json._ | ||
|
||
trait Enumerable[A] { | ||
|
||
def withName(str: String): Option[A] | ||
} | ||
|
||
object Enumerable { | ||
|
||
def apply[A](entries: (String, A)*): Enumerable[A] = | ||
new Enumerable[A] { | ||
override def withName(str: String): Option[A] = | ||
entries.toMap.get(str) | ||
} | ||
|
||
trait Implicits { | ||
|
||
implicit def reads[A](implicit ev: Enumerable[A]): Reads[A] = | ||
Reads { | ||
case JsString(str) => | ||
ev.withName(str) | ||
.map { s => | ||
JsSuccess(s) | ||
} | ||
.getOrElse(JsError("error.invalid")) | ||
case _ => | ||
JsError("error.invalid") | ||
} | ||
|
||
implicit def writes[A: Enumerable]: Writes[A] = | ||
Writes(value => JsString(value.toString)) | ||
} | ||
} | ||
|
||
class WithName(string: String) { | ||
override val toString: String = string | ||
} |
37 changes: 37 additions & 0 deletions
37
app/uk/gov/hmrc/merchandiseinbaggagefrontend/model/core/GoodsDestination.scala
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,37 @@ | ||
/* | ||
* Copyright 2020 HM Revenue & Customs | ||
* | ||
*/ | ||
|
||
package uk.gov.hmrc.merchandiseinbaggagefrontend.model.core | ||
|
||
import play.api.data.Form | ||
import play.api.i18n.Messages | ||
import uk.gov.hmrc.govukfrontend.views.Aliases.Text | ||
import uk.gov.hmrc.govukfrontend.views.viewmodels.radios.RadioItem | ||
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.{Enumerable, WithName} | ||
|
||
sealed trait GoodsDestination | ||
|
||
object GoodsDestination extends Enumerable.Implicits { | ||
|
||
case object NorthernIreland extends WithName("ni") with GoodsDestination | ||
case object EngScoWal extends WithName("gb") with GoodsDestination | ||
|
||
val values: Seq[GoodsDestination] = Seq( | ||
NorthernIreland, | ||
EngScoWal | ||
) | ||
|
||
def options(form: Form[_])(implicit messages: Messages): Seq[RadioItem] = values.map { value => | ||
RadioItem( | ||
value = Some(value.toString), | ||
content = Text(messages(s"goodsDestination.${value.toString}")), | ||
checked = form("value").value.contains(value.toString) | ||
) | ||
} | ||
|
||
|
||
implicit val enumerable: Enumerable[GoodsDestination] = | ||
Enumerable(values.map(v => v.toString -> v): _*) | ||
} |
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
34 changes: 34 additions & 0 deletions
34
app/uk/gov/hmrc/merchandiseinbaggagefrontend/views/GoodsDestinationView.scala.html
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,34 @@ | ||
@* | ||
* Copyright 2020 HM Revenue & Customs | ||
* | ||
*@ | ||
|
||
@import uk.gov.hmrc.govukfrontend.views.html.components | ||
@import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.GoodsDestination | ||
@import uk.gov.hmrc.merchandiseinbaggagefrontend.controllers.routes._ | ||
|
||
@this( | ||
layout: Layout, | ||
formHelper: FormWithCSRF, | ||
errorSummary: components.errorSummary, | ||
inputRadio: components.inputRadio, | ||
button: components.button | ||
) | ||
|
||
@(form: Form[_])(implicit request: Request[_], messages: Messages, appConfig: AppConfig) | ||
|
||
@layout(pageTitle = Some(messages("goodsDestination.title"))) { | ||
@formHelper(action = GoodsDestinationController.onSubmit(), 'autoComplete -> "off", 'novalidate -> "novalidate") { | ||
|
||
@errorSummary(form.errors) | ||
|
||
@inputRadio( | ||
form = form, | ||
legend = messages("goodsDestination.heading"), | ||
legendAsHeading = true, | ||
items = GoodsDestination.options(form) | ||
) | ||
|
||
@button("site.continue") | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/uk/gov/hmrc/merchandiseinbaggagefrontend/views/components/errorSummary.scala.html
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,22 @@ | ||
@* | ||
* Copyright 2020 HM Revenue & Customs | ||
* | ||
*@ | ||
|
||
@this(govukErrorSummary: GovukErrorSummary) | ||
|
||
@(errors: Seq[FormError], errorFieldName: Option[String] = None)(implicit messages: Messages) | ||
|
||
@if(errors.nonEmpty) { | ||
@defining(errors.map { error => | ||
ErrorLink( | ||
href = Some(s"#${errorFieldName.getOrElse(error.key)}"), | ||
content = Text(messages(error.message, error.args:_*)) | ||
) | ||
}) { errorLinks => | ||
@govukErrorSummary(ErrorSummary( | ||
errorList = errorLinks, | ||
title = Text(messages("error.summary.title")) | ||
)) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/uk/gov/hmrc/merchandiseinbaggagefrontend/views/components/inputRadio.scala.html
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,32 @@ | ||
@* | ||
* Copyright 2020 HM Revenue & Customs | ||
* | ||
*@ | ||
|
||
@this(govukRadios: GovukRadios) | ||
|
||
@( form: Form[_], | ||
legend: String, | ||
items: Seq[RadioItem], | ||
legendAsHeading: Boolean = true, | ||
hintKey: Option[String] = None, | ||
classes: String = "govuk-fieldset__legend--xl", | ||
inline: Boolean = false | ||
)(implicit messages: Messages) | ||
|
||
@govukRadios(Radios( | ||
classes = if(inline) "govuk-radios--inline" else "", | ||
name = "value", | ||
fieldset = Some(Fieldset( | ||
legend = Some(Legend( | ||
content = Text(messages(legend)), | ||
isPageHeading = legendAsHeading, | ||
classes = classes | ||
)) | ||
)), | ||
hint = hintKey.map { hint => Hint( | ||
content = Text(messages(hint)) | ||
)}, | ||
items = items, | ||
errorMessage = form("value").error.map(err => ErrorMessage(content = Text(messages(err.message, err.args:_*)))) | ||
)) |
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
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
Oops, something went wrong.