-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): add step to UI flow with paragraph reading
- Loading branch information
Showing
3 changed files
with
75 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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">→</button> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters