From bf33858e41c4bd98d8acfebf30623f751d3eebf4 Mon Sep 17 00:00:00 2001 From: efim Date: Sat, 15 Jul 2023 03:48:04 +0000 Subject: [PATCH] feat(15): route to get previous form step --- .../src/main/resources/templates/step2.html | 3 ++ .../src/main/resources/templates/step3.html | 3 ++ .../src/main/resources/templates/step4.html | 3 ++ .../src/main/scala/multistepform/Models.scala | 33 +++++++++----- .../src/main/scala/multistepform/Routes.scala | 43 +++++++++++++++++++ 5 files changed, 75 insertions(+), 10 deletions(-) diff --git a/15-multi-step-form/src/main/resources/templates/step2.html b/15-multi-step-form/src/main/resources/templates/step2.html index 0601aa7..981f54d 100644 --- a/15-multi-step-form/src/main/resources/templates/step2.html +++ b/15-multi-step-form/src/main/resources/templates/step2.html @@ -183,6 +183,9 @@ class="flex flex-row items-center py-4 w-full bg-white md:items-end md:h-full" > Go Back Go Back Go Back this.copy( - currentStep = stepNum + 1 + currentStep = stepNum + 1, + step4 = this.step4 ) case _ => this } @@ -46,12 +48,14 @@ object Models { sealed trait StepAnswers { def fromFormData(rawData: String): StepAnswers + def submitted: Boolean } object StepAnswers { final case class Step1( name: String = "", email: String = "", - phone: String = "" + phone: String = "", + override val submitted: Boolean = false ) extends StepAnswers { override def fromFormData(rawData: String): Step1 = { println(s"parsing step 1 data $rawData") @@ -67,12 +71,13 @@ object Models { val email = fieldValues.getOrElse("email", "") val phone = fieldValues.getOrElse("phone", "") - Step1(name, email, phone) + Step1(name, email, phone, submitted = true) } } final case class Step2( planType: PlanType = PlanType.Arcade, - isYearly: Boolean = false + isYearly: Boolean = false, + override val submitted: Boolean = false ) extends StepAnswers { override def fromFormData(rawData: String): Step2 = { println(s"parsing step 2 data $rawData") @@ -88,11 +93,13 @@ object Models { PlanType.valueOf(fieldValues.getOrElse("plan-type", "Arcade")) val isYearly = fieldValues.get("isPackageYearly").contains("on") - Step2(planType, isYearly) + Step2(planType, isYearly, submitted = true) } } - final case class Step3(addons: Set[Addons] = Set.empty) - extends StepAnswers { + final case class Step3( + addons: Set[Addons] = Set.empty, + override val submitted: Boolean = false + ) extends StepAnswers { override def fromFormData(rawData: String): Step3 = { println(s"parsing step 3 data $rawData") // for multiple checkboxes data comes in form of @@ -104,13 +111,19 @@ object Models { name -> value } - val addonsStrings = fieldValues.groupMap(_._1)(_._2) + 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) + Step3(addons, submitted = true) } } + final case class Step4( + override val submitted: Boolean = false + ) extends StepAnswers { + override def fromFormData(rawData: String): Step4 = Step4(true) + } } } diff --git a/15-multi-step-form/src/main/scala/multistepform/Routes.scala b/15-multi-step-form/src/main/scala/multistepform/Routes.scala index 0201045..7e3f0f1 100644 --- a/15-multi-step-form/src/main/scala/multistepform/Routes.scala +++ b/15-multi-step-form/src/main/scala/multistepform/Routes.scala @@ -85,6 +85,49 @@ case class Routes()(implicit cc: castor.Context, log: cask.Logger) ) } + @cask.get("/get-form/:stepNum") + def getFormByStep(stepNum: Int, sessionId: cask.Cookie) = { + val id = sessionId.value + val answersData = Sessions.sessionReplies.get(id) + println(s"returning to step $stepNum with data $answersData") + answersData match { + case Some(state) => { + val stepData = stepNum match { + case 1 => state.step1 + case 2 => state.step2 + case 3 => state.step3 + case 4 => state.step4 + } + + if (!stepData.submitted) { + cask.Response( + s"Your previous answer for step $stepNum not found, please reload the page" + ) + } else { + val context = new Context() + val updatedState = state.copy(currentStep = stepNum) + Sessions.sessionReplies.update(id, updatedState) + context.setVariable(formDataContextVarName, updatedState) + val formFragment = templateEngine.process( + updatedState.fragmentName, + Set("formFragment").asJava, + context + ) + cask.Response( + formFragment, + headers = Seq("Content-Type" -> "text/html;charset=UTF-8") + ) + } + } + case None => + cask.Response( + "Your previous answers not found, please reload the page", + 404 + ) + } + + } + // i guess let's make step a hidden input? @cask.post("/submit-step/:stepNum") def submitStep(