Skip to content

Commit

Permalink
[PH][MIBM-126] improve enums
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHodgson committed Oct 12, 2020
1 parent 15e39c9 commit 2bc5029
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 37 deletions.
38 changes: 27 additions & 11 deletions app/uk/gov/hmrc/merchandiseinbaggagefrontend/model/Enum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,17 @@ package uk.gov.hmrc.merchandiseinbaggagefrontend.model
import enumeratum.{EnumEntry, PlayEnum}
import play.api.data.Form
import play.api.i18n.Messages
import play.api.libs.json.{Format, JsError, JsString, JsSuccess, Reads, Writes}
import play.api.libs.json._
import uk.gov.hmrc.govukfrontend.views.Aliases.Text
import uk.gov.hmrc.govukfrontend.views.viewmodels.hint.Hint
import uk.gov.hmrc.govukfrontend.views.viewmodels.radios.RadioItem
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.core.GoodsDestinations.baseMessageKey

trait Enum[A <: EnumEntry] extends PlayEnum[A] {
val baseMessageKey: String

def forCode(code: String): Option[A] = values.find(_.toString == code)

def options(form: Form[_])(implicit messages: Messages): Seq[RadioItem] = values.map { value =>
RadioItem(
value = Some(value.toString),
content = Text(messages(s"$baseMessageKey.${value.toString}")),
checked = form("value").value.contains(value.toString),
hint = hint(value, messages)
)
}

protected def hint(value: A, messages: Messages): Option[Hint] = None
}

object EnumFormat {
Expand All @@ -50,4 +41,29 @@ object EnumFormat {
Writes(v => JsString(v.entryName)))
}

trait EnumEntryRadioItemSupport {
this: EnumEntry =>

protected val baseMessageKeyForEntry: String = s"$baseMessageKey.$entryName"
protected val hintMessageKey: Option[String] = Some(s"$baseMessageKeyForEntry.hint")
protected val maybeHintMessageKey: Option[String] = None

def radioItem(form: Form[_])(implicit messages: Messages): RadioItem =
RadioItem(
value = Some(entryName),
content = Text(messages(s"$baseMessageKeyForEntry")),
checked = form("value").value.contains(entryName),
hint = maybeHintMessageKey.flatMap { messageKey =>
Some(Hint(content = Text(messages(messageKey))))
}
)
}

trait RadioSupport[A <: EnumEntry with EnumEntryRadioItemSupport] {
this: Enum[A] =>

def options(form: Form[_])(implicit messages: Messages): Seq[RadioItem] = values.map { value =>
value.radioItem(form)(messages)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ import java.time.format.DateTimeFormatter
import java.util.Locale.UK
import java.util.UUID.randomUUID

import enumeratum.EnumEntry
import play.api.i18n.Messages
import play.api.libs.functional.syntax._
import play.api.libs.json.{Format, Json, OFormat}
import uk.gov.hmrc.govukfrontend.views.Aliases._
import uk.gov.hmrc.govukfrontend.views.viewmodels.summarylist.SummaryListRow
import uk.gov.hmrc.merchandiseinbaggagefrontend.controllers.routes._
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.Enum
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.currencyconversion.Currency
import uk.gov.hmrc.merchandiseinbaggagefrontend.utils.ValueClassFormat

import scala.collection.immutable

case class SessionId(value: String)

object SessionId {
Expand Down Expand Up @@ -214,7 +218,7 @@ case class DeclarationJourney(sessionId: SessionId,
maybeCustomsAgent,
eori,
journeyDetails,
YesNo(maybeTravellingByVehicle.getOrElse(false)),
YesNo.from(maybeTravellingByVehicle.getOrElse(false)),
maybeRegistrationNumber
)
}
Expand Down Expand Up @@ -288,12 +292,17 @@ object CustomsAgent {
implicit val format: OFormat[CustomsAgent] = Json.format[CustomsAgent]
}

case class YesNo(yes: Boolean) {
override val toString: String = if (yes) "Yes" else "No"
}
sealed trait YesNo extends EnumEntry

object YesNo extends Enum[YesNo] {
override val baseMessageKey: String = "enum"
override val values: immutable.IndexedSeq[YesNo] = findValues

def from(bool: Boolean): YesNo = if (bool) Yes else No

case object No extends YesNo

object YesNo {
implicit val format: OFormat[YesNo] = Json.format[YesNo]
case object Yes extends YesNo
}

case class Declaration(sessionId: SessionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,24 @@
package uk.gov.hmrc.merchandiseinbaggagefrontend.model.core

import enumeratum.EnumEntry
import play.api.i18n.Messages
import play.api.libs.json.Format
import uk.gov.hmrc.govukfrontend.views.Aliases.Text
import uk.gov.hmrc.govukfrontend.views.viewmodels.hint.Hint
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.{Enum, EnumFormat}
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.{Enum, EnumFormat, EnumEntryRadioItemSupport, RadioSupport}

import scala.collection.immutable

sealed trait GoodsDestination extends EnumEntry
sealed trait GoodsDestination extends EnumEntry with EnumEntryRadioItemSupport

object GoodsDestination {
implicit val format: Format[GoodsDestination] = EnumFormat(GoodsDestinations)
}

object GoodsDestinations extends Enum[GoodsDestination] {
object GoodsDestinations extends Enum[GoodsDestination] with RadioSupport[GoodsDestination]{
override val baseMessageKey: String = "goodsDestination"
override val values: immutable.IndexedSeq[GoodsDestination] = findValues

case object NorthernIreland extends GoodsDestination

case object GreatBritain extends GoodsDestination

override def hint(value: GoodsDestination, messages: Messages): Option[Hint] =
if (value == GreatBritain) Some(Hint(content = Text(messages("goodsDestination.GreatBritain.hint")))) else None
case object GreatBritain extends GoodsDestination {
override val maybeHintMessageKey: Option[String] = hintMessageKey
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ package uk.gov.hmrc.merchandiseinbaggagefrontend.model.core

import enumeratum.EnumEntry
import play.api.libs.json.Format
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.{EnumFormat, Enum}
import uk.gov.hmrc.merchandiseinbaggagefrontend.model.{Enum, EnumEntryRadioItemSupport, EnumFormat, RadioSupport}

import scala.collection.immutable

sealed trait GoodsVatRate extends EnumEntry
sealed trait GoodsVatRate extends EnumEntry with EnumEntryRadioItemSupport

object GoodsVatRate {
implicit val format: Format[GoodsVatRate] = EnumFormat(GoodsVatRates)
}

object GoodsVatRates extends Enum[GoodsVatRate] {
object GoodsVatRates extends Enum[GoodsVatRate] with RadioSupport[GoodsVatRate] {
override val baseMessageKey: String = "goodsVatRate"

override val values: immutable.IndexedSeq[GoodsVatRate] = findValues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@
<div class="autocomplete-wrapper currency govuk-input--width-30">
<select name="currency" id="currency">
@if(form("value").value.isEmpty) {
<option value="" selected></option>
<option value="" selected>
}
@for(currency <- currencies) {
@if(form("currency").value.contains(currency.currencyCode)) {
<option value="@currency.currencyCode" selected>@currency.displayName</option>
<option value="@currency.currencyCode" selected>@currency.displayName
} else {
<option value="@currency.currencyCode">@currency.displayName</option>
<option value="@currency.currencyCode">@currency.displayName
}
}
</select>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@
<div class="autocomplete-wrapper country">
<select name="value" id="country-auto-complete">
@if(form("value").value.isEmpty) {
<option value="" selected></option>
<option value="" selected>
}
@for(country <- CountriesService.countries) {
@if(form("value").value.contains(country)) {
<option value="@country" selected>@country</option>
<option value="@country" selected>@country
} else {
<option value="@country">@country</option>
<option value="@country">@country
}
}
</select>
Expand Down
2 changes: 2 additions & 0 deletions conf/messages
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ site.remove = Remove
payment.button = Pay now
next.button = Next

enum.Yes = Yes
enum.No = No

reviewGoods.title = Review your goods
removeGoods.title = Are you sure you want to remove the x?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class DeclarationSpec extends BaseSpec with CoreTestData {
completedDeclarationJourney.maybeCustomsAgent,
completedDeclarationJourney.maybeEori.get,
completedDeclarationJourney.maybeJourneyDetails.get,
YesNo(completedDeclarationJourney.maybeTravellingByVehicle.get),
YesNo.from(completedDeclarationJourney.maybeTravellingByVehicle.get),
completedDeclarationJourney.maybeRegistrationNumber))
}
}
Expand Down

0 comments on commit 2bc5029

Please sign in to comment.