Skip to content

Commit

Permalink
feat(frontend): add step to UI flow with paragraph reading
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamhexi committed Oct 29, 2024
1 parent bb775f5 commit 1186a21
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 61 deletions.
64 changes: 4 additions & 60 deletions frontend/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@
import { goto } from '$app/navigation';
let formData = { context: '', userAnswer: '', correctAnswer: '', question: '' };
const minimumAnswerLength = 5;
const handleSubmit = () => {
const answerInput = document.querySelector('.answer-input');
const answer = String(answerInput.value);
if (answer.length < minimumAnswerLength) {
alert(`Answer is too short. Minimum length: ${minimumAnswerLength}.`)
return;
}
if (typeof window !== 'undefined') {
sessionStorage.setItem('formData', JSON.stringify(formData));
goto('/evaluate');
}
};
/**
* @type {any[]}
Expand Down Expand Up @@ -72,16 +57,7 @@
question = result.data.question;
}
/**
* Make Ctrl + Enter send a form.
* @param {{ key: any; }} event
*/
function handleKeyDown(event) {
// @ts-ignore
if (event.ctrlKey && event.key === 'Enter') {
handleSubmit();
}
}
</script>

Expand All @@ -98,45 +74,13 @@
</div>
{/each}
{:else}
<form on:submit|preventDefault={handleSubmit}>
<p>
<b>Question:</b>
<br>
{question}
</p>
<input type="text" bind:value={formData.question} hidden readonly/>
<input type="text" bind:value={formData.context} hidden readonly/>
<input type="text" bind:value={formData.correctAnswer} hidden readonly/>
<label>
<p><b>Your answer:</b></p>
<textarea
class="answer-input"
bind:value={formData.userAnswer}
on:keydown={handleKeyDown}
required
autofocus
></textarea>
</label>
<button type="submit">&rarr;</button>
</form>
<p>Read a paragraph</p>
<p>{formData.context}</p>
<button on:click={() => goto('/answer')}>&rarr;</button>
{/if}


<style>
label > p {
/* padding: 10px; */
display: block;
}
label {
height: 10vh;
}
.answer-input {
width: 80%;
height: 10rem;
}
.learning-material {
margin: 50px 0;
display: flex;
Expand Down
71 changes: 71 additions & 0 deletions frontend/src/routes/answer/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<script>
import { goto } from "$app/navigation";
import { onMount } from "svelte";
const minimumAnswerLength = 5;
let formData = { context: '', userAnswer: '', correctAnswer: '', question: '' };
/**
* @type {string|null}
*/
let evaluation = null;
function receiveFormData() {
if (typeof window !== 'undefined' && sessionStorage.getItem('formData')) {
// @ts-ignore
formData = JSON.parse(sessionStorage.getItem('formData'));
}
}
onMount(async () => {
await receiveFormData();
formData.userAnswer = '';
});
const handleSubmit = () => {
const answerInput = document.querySelector('.answer-input');
const answer = String(answerInput.value);
if (answer.length < minimumAnswerLength) {
alert(`Answer is too short. Minimum length: ${minimumAnswerLength}.`)
return;
}
if (typeof window !== 'undefined') {
sessionStorage.setItem('formData', JSON.stringify(formData));
goto('/evaluate');
}
};
/**
* Make Ctrl + Enter send a form.
* @param {{ key: any; }} event
*/
function handleKeyDown(event) {
// @ts-ignore
if (event.ctrlKey && event.key === 'Enter') {
handleSubmit();
}
}
</script>

<form on:submit|preventDefault={handleSubmit}>
<p>
<b>Question:</b>
<br>
{formData.question}
</p>
<input type="text" bind:value={formData.question} hidden readonly/>
<input type="text" bind:value={formData.context} hidden readonly/>
<input type="text" bind:value={formData.correctAnswer} hidden readonly/>
<label>
<p><b>Your answer:</b></p>
<textarea
class="answer-input"
bind:value={formData.userAnswer}
on:keydown={handleKeyDown}
required
autofocus
></textarea>
</label>
<button type="submit">&rarr;</button>
</form>
1 change: 0 additions & 1 deletion frontend/src/routes/evaluate/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { onMount } from "svelte";
import { API_URL } from "../../lib/config.js";
import { showErrorMessage, disableLoader, enableLoader } from "../../lib/utils.js"
import { goto } from "$app/navigation";
let formData = { context: '', userAnswer: '', correctAnswer: '', question: '' };
/**
Expand Down

0 comments on commit 1186a21

Please sign in to comment.