feat(15): start form, save state, decode steps

This commit is contained in:
efim 2023-07-14 20:49:00 +00:00
parent febc77032b
commit 6ed489835f
4 changed files with 67 additions and 24 deletions

View File

@ -166,6 +166,7 @@
<input
class="mr-2 w-9 h-5 ml-2 rounded-full appearance-none mt-[0.3rem] bg-marine-blue after:absolute after:h-3 after:w-3 after:rounded-full after:border-none after:bg-neutral-100 after:transition-[background-color_0.2s,transform_0.2s] checked:after:ml-[1.2rem] after:ml-[0.25rem] after:mt-[0.25rem] hover:cursor-pointer col-start-2 row-start-1 peer"
type="checkbox"
name="isPackageYearly"
role="switch"
id="packageDuration"
/>

View File

@ -114,6 +114,7 @@
<input
id="multiplayer-games"
type="checkbox"
value="OnlineService"
name="addon-services"
class="my-7 w-6 h-6 peer"
/>
@ -137,6 +138,7 @@
id="larger-storage"
type="checkbox"
name="addon-services"
value="LargerStorage"
class="my-7 w-6 h-6 peer"
/>
<div
@ -159,6 +161,7 @@
id="custom-profile"
type="checkbox"
name="addon-services"
value="CustomProfile"
class="my-7 w-6 h-6 peer"
/>
<div

View File

@ -14,21 +14,25 @@ object Models {
def fragmentName: String = s"step${currentStep}"
def updateStep(stepNum: Int, rawData: String): Answers = {
stepNum match {
case 1 => this.copy(
step1 = this.step1.fromFormData(rawData),
currentStep = stepNum + 1
)
case 2 => this.copy(
step2 = this.step2.fromFormData(rawData),
currentStep = stepNum + 1
)
case 3 => this.copy(
step3 = this.step3.fromFormData(rawData),
currentStep = stepNum + 1
)
case 4 => this.copy(
currentStep = stepNum + 1
)
case 1 =>
this.copy(
step1 = this.step1.fromFormData(rawData),
currentStep = stepNum + 1
)
case 2 =>
this.copy(
step2 = this.step2.fromFormData(rawData),
currentStep = stepNum + 1
)
case 3 =>
this.copy(
step3 = this.step3.fromFormData(rawData),
currentStep = stepNum + 1
)
case 4 =>
this.copy(
currentStep = stepNum + 1
)
case _ => this
}
}
@ -50,10 +54,14 @@ object Models {
phone: String = ""
) extends StepAnswers {
override def fromFormData(rawData: String): Step1 = {
val fieldValues = rawData.split("&").map { field =>
val Array(name, value) = field.split("=")
name -> value
}.toMap
println(s"parsing step 1 data $rawData")
val fieldValues = rawData
.split("&")
.map { field =>
val Array(name, value) = field.split("=")
name -> value
}
.toMap
val name = fieldValues.getOrElse("name", "")
val email = fieldValues.getOrElse("email", "")
@ -67,15 +75,41 @@ object Models {
isYearly: Boolean = false
) extends StepAnswers {
override def fromFormData(rawData: String): Step2 = {
val a = 1
Step2()
println(s"parsing step 2 data $rawData")
val fieldValues = rawData
.split("&")
.map { field =>
val Array(name, value) = field.split("=")
name -> value
}
.toMap
val planType =
PlanType.valueOf(fieldValues.getOrElse("plan-type", "Arcade"))
val isYearly = fieldValues.get("isPackageYearly").contains("on")
Step2(planType, isYearly)
}
}
final case class Step3(addons: Set[Addons] = Set.empty)
extends StepAnswers {
override def fromFormData(rawData: String): Step3 = {
val a = 1
Step3()
println(s"parsing step 3 data $rawData")
// for multiple checkboxes data comes in form of
// addon-services=OnlineService&addon-services=CustomProfile
val fieldValues = rawData
.split("&")
.map { field =>
val Array(name, value) = field.split("=")
name -> value
}
val addonsStrings = fieldValues.groupMap(_._1)(_._2)
.getOrElse("addon-services", Array.empty[String])
println(s"in step 3 got strings $addonsStrings")
val addons = addonsStrings.map(Addons.valueOf(_)).toSet
Step3(addons)
}
}
}

View File

@ -69,7 +69,9 @@ case class Routes()(implicit cc: castor.Context, log: cask.Logger)
*/
@cask.get("/get-form")
def getForm(sessionId: cask.Cookie) = {
val state = Models.Answers(currentStep = 1)
val id = sessionId.value
val state = Sessions.sessionReplies.getOrElse(id, Answers(id))
println(s"starting form for $state")
val context = new Context()
context.setVariable(formDataContextVarName, state)
val formFragment = templateEngine.process(
@ -96,6 +98,9 @@ case class Routes()(implicit cc: castor.Context, log: cask.Logger)
val userAnswers = Sessions.sessionReplies.getOrElse(id, Answers(id))
val updatedAnswers = userAnswers.updateStep(stepNum, request.text())
Sessions.sessionReplies.update(id, updatedAnswers)
val context = new Context()
context.setVariable(formDataContextVarName, updatedAnswers)
val nextFormFragment = templateEngine.process(