Added quiz-edit

This commit is contained in:
Mawoka
2022-03-06 11:23:16 +01:00
parent b697115bfb
commit 3cdcea8fd4
5 changed files with 280 additions and 4 deletions
+91
View File
@@ -0,0 +1,91 @@
<script lang='ts'>
interface Data {
public: boolean;
title: string;
description: string;
questions: Question[];
}
interface Question {
question: string;
time: string;
answers: Answer[];
}
interface Answer {
right: boolean;
answer: string;
}
export let data: Data;
export let submit_button_text = "Create";
const empty_question: Question = {
question: '',
time: '20',
answers: [{
right: false,
answer: ''
}]
};
const empty_answer: Answer = {
right: false,
answer: ''
};
</script>
<label>
<input type='checkbox' bind:checked={data.public}>
Public?
</label>
<label>
<input type='text' placeholder='Title' bind:value={data.title} class='text-black' />
Title
</label>
<label>
<textarea placeholder='Description' bind:value={data.description} class='text-black'></textarea>
Description
</label>
{#each data.questions as question, index_question}
<div class='ml-8 grid grid-cols-1 gap-2 m-2 border border-black border-2'>
<h1 class='text-3xl'>Question {index_question + 1}</h1>
<label>
<input type='text' placeholder='Question' bind:value={question.question} class='text-black' />
Question
</label>
<label>
<input type='text' placeholder='20' bind:value={question.time} class='text-black' />
Time in seconds
</label>
{#each question.answers as answer, index_answer}
<div class='ml-8 grid grid-cols-1 gap-2 m-2 border border-black border-2'>
<h1 class='text-3xl'>Answer {index_answer + 1}</h1>
<p>Answer: {index_answer} Question: {index_question}</p>
<label>
<input type='text' placeholder='Answer'
bind:value={data.questions[index_question].answers[index_answer].answer}
class='text-black' />
Answer
</label>
<label>
<input type='checkbox' bind:checked={answer.right} class='text-black' />
Right?
</label>
<button class='text-left'
on:click={() => {data.questions[index_question].answers = [...data.questions[index_question].answers, {...empty_answer}]}}>
Add new answer
</button>
</div>
{/each}
<button class='text-left' on:click={() => {data.questions = [...data.questions, {...empty_question}]}}>
Add new
question
</button>
</div>
{/each}
<button type='submit'>{submit_button_text}</button>
+165
View File
@@ -0,0 +1,165 @@
<script lang='ts' context='module'>
export async function load({ url }) {
const quiz_id = url.searchParams.get('quiz_id');
if (quiz_id === null) {
return {
status: 404
};
}
return {
props: {
quiz_id
}
};
}
</script>
<script lang='ts'>
import Editor from '$lib/editor.svelte';
let responseData = {
open: false,
data: ''
};
interface Data {
public: boolean;
title: string;
description: string;
questions: Question[];
}
interface Question {
question: string;
time: string;
answers: Answer[];
}
interface Answer {
right: boolean;
answer: string;
}
export let quiz_id: string;
let data: Data;
const get_quiz = async (): Promise<void> => {
const response = await fetch(`/api/v1/quiz/get/${quiz_id}`);
if (response.status === 404) {
throw new Error('Quiz not found');
} else if (response.status === 200) {
data = await response.json();
return;
}
};
const submit = async () => {
const res = await fetch(`/api/v1/quiz/update/${quiz_id}`, {
method: 'PUT',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
});
if (res.status === 401) {
throw new Error('Unauthorized');
} else if (res.status === 404) {
throw new Error('Quiz not found');
} else if (res.status === 200) {
responseData.data = '200';
responseData.open = true;
}
};
</script>
{#await get_quiz()}
<svg class='h-8 w-8 animate-spin mx-auto my-20' viewBox='3 3 18 18'>
<path
class='fill-black'
d='M12 5C8.13401 5 5 8.13401 5 12C5 15.866 8.13401 19 12 19C15.866 19 19 15.866 19 12C19 8.13401 15.866 5 12 5ZM3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12Z'
/>
<path
class='fill-blue-100'
d='M16.9497 7.05015C14.2161 4.31648 9.78392 4.31648 7.05025 7.05015C6.65973 7.44067 6.02656 7.44067 5.63604 7.05015C5.24551 6.65962 5.24551 6.02646 5.63604 5.63593C9.15076 2.12121 14.8492 2.12121 18.364 5.63593C18.7545 6.02646 18.7545 6.65962 18.364 7.05015C17.9734 7.44067 17.3403 7.44067 16.9497 7.05015Z'
/>
</svg>
{:then lol}
{#if data !== undefined}
<form on:submit|preventDefault={submit} class='grid grid-cols-1 gap-2'>
<Editor bind:data submit_button_text={"Save"} />
</form>
{/if}
{:catch err}
<div class='text-center'>
<h1 class='text-5xl font-bold'>{err.message}</h1>
</div>
{/await}
<div
class='fixed z-10 inset-0 overflow-y-auto'
aria-labelledby='modal-title'
role='dialog'
aria-modal='true'
class:hidden={!responseData.open}
>
<div
class='flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0'
>
<div
class='fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity'
aria-hidden='true'
/>
<span class='hidden sm:inline-block sm:align-middle sm:h-screen' aria-hidden='true'
>&#8203;</span
>
<div
class='inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full'
>
<div class='bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4'>
<div class='sm:flex sm:items-start'>
<div
class='mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-red-100 sm:mx-0 sm:h-10 sm:w-10'
>
<!-- Heroicon name: outline/exclamation -->
<svg
class='w-6 h-6 text-green-600'
fill='none'
stroke='currentColor'
viewBox='0 0 24 24'
xmlns='http://www.w3.org/2000/svg'
>
<path
stroke-linecap='round'
stroke-linejoin='round'
stroke-width='2'
d='M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z'
/>
</svg>
</div>
<div class='mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left'>
<h3 class='text-lg leading-6 font-medium text-gray-900' id='modal-title'>
Successfully updated quiz!
</h3>
<div class='mt-2'>
<p class='text-sm text-gray-500'>
Updated the quiz successfully!
</p>
</div>
</div>
</div>
</div>
<div class='bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse'>
<button
type='button'
on:click={() => {
window.location.href = "/overview"
}}
class='mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm'
>Close
</button>
</div>
</div>
</div>
</div>
+1 -1
View File
@@ -149,7 +149,7 @@
<td
class='py-4 px-6 text-sm text-gray-500 whitespace-nowrap dark:text-gray-400'
>
<a href='/edit/{quiz.id}'>Edit</a>
<a href='/edit?quiz_id={quiz.id}'>Edit</a>
</td>
<td
class='py-4 px-6 text-sm text-gray-500 whitespace-nowrap dark:text-gray-400'