From 076dc76ca4f3da059d9b6b568dbca1ca930646d0 Mon Sep 17 00:00:00 2001 From: efim Date: Sat, 15 Jul 2023 12:05:04 +0000 Subject: [PATCH] feat(15): submitting form data on step back --- .../src/main/resources/templates/step1.html | 4 +- .../src/main/resources/templates/step2.html | 10 ++-- .../src/main/resources/templates/step3.html | 8 +-- .../src/main/resources/templates/step4.html | 8 +-- .../src/main/scala/multistepform/Models.scala | 33 ++++++------ .../src/main/scala/multistepform/Routes.scala | 53 +++---------------- 6 files changed, 40 insertions(+), 76 deletions(-) diff --git a/15-multi-step-form/src/main/resources/templates/step1.html b/15-multi-step-form/src/main/resources/templates/step1.html index f4a9f01..2fd9d06 100644 --- a/15-multi-step-form/src/main/resources/templates/step1.html +++ b/15-multi-step-form/src/main/resources/templates/step1.html @@ -39,9 +39,9 @@ class="flex flex-col items-center w-screen h-screen md:grid md:items-start md:p-5 md:bg-white md:rounded-2xl md:grid-cols-[auto_1fr] md:w-desktop-form md:h-desktop-form md:drop-shadow-2xl" id="form-step" th:fragment="formFragment(formData)" - hx-post="/submit-step/1" + hx-post="/submit-step/1/2" hx-swap="outerHTML" - action="/submit-step/1" + action="/submit-step/1/2" method="post" > @@ -119,7 +119,7 @@ name="plan-type" value="Arcade" class="hidden peer" - th:checked="${formData.step2.planType.toString()} == 'Acrade'" + th:checked="${formData.step2.planType.toString()} == 'Arcade'" checked />
Go Back @@ -191,9 +191,9 @@ class="flex flex-row items-center py-4 w-full bg-white md:items-end md:h-full" > Go Back Go Back this.copy( step1 = this.step1.fromFormData(rawData), - currentStep = stepNum + 1 + currentStep = nextStep ) case 2 => this.copy( step2 = this.step2.fromFormData(rawData), - currentStep = stepNum + 1 + currentStep = nextStep ) case 3 => this.copy( step3 = this.step3.fromFormData(rawData), - currentStep = stepNum + 1 + currentStep = nextStep ) case 4 => this.copy( - currentStep = stepNum + 1, - step4 = this.step4 + step4 = this.step4, + currentStep = nextStep ) case _ => this } 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 7e3f0f1..595fdb9 100644 --- a/15-multi-step-form/src/main/scala/multistepform/Routes.scala +++ b/15-multi-step-form/src/main/scala/multistepform/Routes.scala @@ -7,6 +7,8 @@ import cask.endpoints.ParamReader import java.util.UUID import scala.jdk.CollectionConverters._ import multistepform.Models.Answers +import scala.annotation.internal.requiresCapability +import java.net.URLDecoder case class Routes()(implicit cc: castor.Context, log: cask.Logger) extends cask.Routes { @@ -85,54 +87,11 @@ 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") + @cask.post("/submit-step/:stepNum/:nextStep") def submitStep( sessionId: cask.Cookie, stepNum: Int, + nextStep: Int, request: cask.Request ) = { val id = sessionId.value @@ -140,7 +99,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()) + val submittedData = URLDecoder.decode(request.text() , "UTF-8") + + val updatedAnswers = userAnswers.updateStep(stepNum, submittedData, nextStep) Sessions.sessionReplies.update(id, updatedAnswers)