Skip to content

Commit

Permalink
feat(frontend): Implement jumps for StepOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Brayan-724 committed Sep 14, 2024
1 parent 2255ad4 commit 90f8227
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
29 changes: 26 additions & 3 deletions crates/backend/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use forms_models::{
D1EntityQueries,
};
use forms_shared::db::D1Action;
use forms_shared::{get_auth, get_body, FormsResponse, WorkerHttpResponse};
use forms_shared::{
get_auth, get_body, string_into_response, FormsResponse, IntoResponse, WorkerHttpResponse,
};

use crate::admins::needs_admin;
use crate::session::get_device_id_hash;
Expand Down Expand Up @@ -90,9 +92,30 @@ pub async fn post(req: Request, ctx: RouterContext) -> WorkerHttpResponse {

let body = get_body::<FormCreate>(&mut req).await?;

D1EntityCreate::create_query(body, &db).run().await?;
let Some(new_form) = D1EntityCreate::create_query(body, &db)
.all::<FormJs>()
.await?
.first() else {
FormsResponse::json(
201,
&serde_json::json!({
"errors": [],
"success": true,
"data": new_form_id
}),
)
};

let new_form_id = new_form.id;

FormsResponse::ok("OK")
FormsResponse::json(
201,
&serde_json::json!({
"errors": [],
"success": true,
"data": new_form_id
}),
)
})
.await
}
Expand Down
8 changes: 6 additions & 2 deletions crates/models/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,14 @@ impl From<SessionCompleteJs> for SessionComplete {
create_queries! {
Session where select_all = [ id, form_id, external_id, device_id, last_answer, steps ],
SessionRead where select = |session, db| {
let mut queries = vec!["deleted = ?"];
let mut queries = vec![];
let mut args = vec![false.into()];
let mut or_queries = None;
let mut or_args = vec![];

let query = if session.complete {
queries.push("Session.deleted = ?");

if let Some(token) = session.token {
queries.push("Session.token = ?");
args.push(token.into());
Expand All @@ -212,12 +214,14 @@ create_queries! {
}

"SELECT \
Session.id, Session.form_id, Session.external_id, Session.created_at, \
Session.id, Session.form_id, Session.device_id, Session.external_id, Session.created_at, Session.last_answer, Session.steps, \
External.kind AS external_kind, External.email AS external_email \
FROM Session \
LEFT JOIN External ON Session.external_id = External.id
WHERE "
} else {
queries.push("deleted = ?");

if let Some(token) = session.token {
queries.push("token = ?");
args.push(token.into());
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/lib/form/presentation/FormStep.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
const dispatch = createEventDispatcher<{
prev: null;
next: null;
next: number | null;
answer: [questionId: number, answer: string];
}>();
Expand Down Expand Up @@ -53,6 +53,8 @@
if (answer) dispatch("answer", [step.id, answer]);
}, 550);
}
let customNext: number | null = null;
</script>

{#if active}
Expand All @@ -69,7 +71,7 @@
{:else if step.type === "questionText"}
<FormStepQuestionText {step} bind:answer {sendAnswer} />
{:else if step.type === "options" && !step.data.canMultiple}
<FormStepOptions {step} bind:answer {sendAnswer} />
<FormStepOptions {step} bind:answer {sendAnswer} bind:customNext />
{:else if step.type === "options" && step.data.canMultiple}
<FormStepOptionsMultiple {step} bind:answer {sendAnswer} />
{:else}
Expand All @@ -80,7 +82,7 @@
<button disabled={!canPrev} on:click={() => canPrev && dispatch("prev")}>
Anterior
</button>
<button {disabled} on:click={() => !disabled && dispatch("next")}>
<button {disabled} on:click={() => !disabled && dispatch("next", customNext)}>
{#if canNext}
Siguiente
{:else}
Expand Down
21 changes: 19 additions & 2 deletions frontend/src/lib/form/presentation/steps/FormStepOptions.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<script lang="ts">
import type { FormStepOptions } from "../../models/Step.d";
export let step: FormStepOptions;
export let answer: string;
export let customNext: number | null;
export let step: FormStepOptions;
export let sendAnswer: () => void;
if (step.data.jumps) {
const idx = step.data.options.findIndex(option => option === answer);
const jumpQuestion = step.data.jumps[idx];
if (jumpQuestion != null) {
customNext = jumpQuestion;
}
}
</script>

<ul>
Expand All @@ -16,7 +26,14 @@
type="radio"
bind:group={answer}
value={option}
on:change={sendAnswer} />
on:change={() => {
const jumpQuestion = step.data.jumps[idx] ?? 3;
if (jumpQuestion != null) {
customNext = jumpQuestion;
}

sendAnswer();
}} />

<label for={`q-${step.id}-op-${idx}`}>
<span />
Expand Down

0 comments on commit 90f8227

Please sign in to comment.