Added /explore and /view

This commit is contained in:
Mawoka
2022-04-14 14:20:48 +02:00
parent d43795b35f
commit 5d4ace77a9
17 changed files with 3161 additions and 3854 deletions
-44
View File
@@ -1,44 +0,0 @@
/// <reference types="@sveltejs/kit" />
// See https://kit.svelte.dev/docs/types#the-app-namespace
// for information about these interfaces
declare namespace App {
// interface Locals {}
// interface Platform {}
interface Session {
authenticated: boolean;
token: string | null;
email: string | null;
}
// interface Stuff {}
}
export interface QuizData {
title: string;
description: string;
quiz_id: string;
questions: Question[];
game_id: string;
game_pin: string;
started: boolean;
}
export interface Question {
time: string;
question: string;
image?: string;
answers: Answer[];
}
export interface Answer {
right: boolean;
answer: string;
}
// TODO Keep an eye on this shit
// export interface Answer {
// username: string;
// answer: string;
// right: boolean;
// }
+62
View File
@@ -0,0 +1,62 @@
<script>
export let headerText;
let expanded = false;
</script>
<div class='collapsible'>
<h3>
<button aria-expanded={expanded} on:click={() => expanded = !expanded}>{headerText}
<svg viewBox='0 0 20 20' fill='none'>
<path class='vert' d='M10 1V19' stroke='black' stroke-width='2' />
<path d='M1 10L19 10' stroke='black' stroke-width='2' />
</svg>
</button>
</h3>
<div class='contents' class:hidden={!expanded}>
<slot></slot>
</div>
</div>
<style>
.collapsible {
border-bottom: 1px solid var(--gray-light, #eee);
}
h3 {
margin: 0;
}
button {
background-color: var(--background, #fff);
color: var(--gray-darkest, #282828);
display: flex;
justify-content: space-between;
width: 100%;
border: none;
margin: 0;
padding: 1em 0.5em;
}
button[aria-expanded="true"] {
border-bottom: 1px solid var(--gray-light, #eee);
}
button[aria-expanded="true"] .vert {
display: none;
}
button:focus svg {
outline: 2px solid;
}
button [aria-expanded="true"] rect {
fill: currentColor;
}
svg {
height: 0.7em;
width: 0.7em;
}
</style>
+1 -1
View File
@@ -1,5 +1,5 @@
<script lang="ts">
import type { QuizData } from '../../app';
import type { QuizData } from '$lib/quiz_types';
export let quiz_data: QuizData;
export let final_results: Array<null> | Array<Array<PlayerAnswer>>;
+1 -1
View File
@@ -1,5 +1,5 @@
<script lang="ts">
import type { Question } from '../../app';
import type { Question } from '$lib/quiz_types';
import { socket } from '$lib/socket';
export let question: Question;
+1 -1
View File
@@ -1,5 +1,5 @@
<script lang="ts">
import type { Answer, QuizData } from '../../app';
import type { Answer, QuizData } from '$lib/quiz_types';
export let results: Array<Answer>;
export let game_data: QuizData;
+44
View File
@@ -0,0 +1,44 @@
/// <reference types="@sveltejs/kit" />
// See https://kit.svelte.dev/docs/types#the-app-namespace
// for information about these interfaces
declare namespace App {
// interface Locals {}
// interface Platform {}
interface Session {
authenticated: boolean;
token: string | null;
email: string | null;
}
// interface Stuff {}
}
export interface QuizData {
title: string;
description: string;
quiz_id: string;
questions: Question[];
game_id: string;
game_pin: string;
started: boolean;
}
export interface Question {
time: string;
question: string;
image?: string;
answers: Answer[];
}
export interface Answer {
right: boolean;
answer: string;
}
// TODO Keep an eye on this shit
// export interface Answer {
// username: string;
// answer: string;
// right: boolean;
// }
+1 -1
View File
@@ -23,7 +23,7 @@
</script>
<script lang="ts">
import type { QuizData } from '../app';
import type { QuizData } from '$lib/quiz_types';
import { socket } from '$lib/socket';
import { getLocalization } from '$lib/i18n';
+1 -1
View File
@@ -1,5 +1,5 @@
<script lang="ts">
import type { QuizData } from '../app';
import type { QuizData } from '$lib/quiz_types';
import Title from '$lib/play/title.svelte';
import Question from '$lib/play/question.svelte';
import { socket } from '$lib/socket';
+41
View File
@@ -0,0 +1,41 @@
<script lang='ts'>
const getData = async () => {
const response = await fetch('/api/v1/search/search?q=*');
return await response.json();
};
</script>
{#await getData()}
<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 quizzes}
<div class='grid grid-cols-3'>
{#each quizzes.hits as quiz}
<div class='flex justify-center'>
<a href='/view/{quiz.id}' class='h-max w-fit'>
<div class='max-w-md py-4 px-8 bg-white shadow-lg rounded-lg my-20'>
<!-- <div class='flex justify-center md:justify-end -mt-16'>
<img class='w-20 h-20 object-cover rounded-full border-2 border-indigo-500'
src='https://images.unsplash.com/photo-1499714608240-22fc6ad53fb2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80'>
</div>-->
<div>
<h2 class='text-gray-800 text-3xl font-semibold'>{quiz.title}</h2>
<p class='mt-2 text-gray-600'>{quiz.description}</p>
</div>
<div class='flex mt-4'>
<span>Made by {quiz.user}</span>
</div>
</div>
</a>
</div>
{/each}
</div>
{/await}
+1 -1
View File
@@ -15,7 +15,7 @@
</script>
<script lang="ts">
import type { Question } from '../app';
import type { Question } from '$lib/quiz_types';
import { DateTime } from 'luxon';
import { getLocalization } from '$lib/i18n';
import Footer from '$lib/footer.svelte';
+1 -1
View File
@@ -17,7 +17,7 @@
<script lang="ts">
import { socket } from '$lib/socket';
import JoinGame from '$lib/play/join.svelte';
import type { Answer, QuizData } from '../app';
import type { Answer, QuizData } from '$lib/quiz_types';
import ShowTitle from '$lib/play/title.svelte';
import Question from '$lib/play/question.svelte';
import ShowResults from '$lib/play/show_results.svelte';
+156
View File
@@ -0,0 +1,156 @@
<script lang='ts' context='module'>
export async function load({ params, fetch, session }) {
const { quiz_id } = params;
const res = await fetch(`/api/v1/quiz/get/public/${quiz_id}`);
if (res.status === 404) {
return {
status: 404
};
} else if (res.status === 200) {
const quiz = await res.json();
return {
props: {
quiz: quiz,
logged_in: session.authenticated
}
};
} else {
return {
status: 500
};
}
}
</script>
<script lang='ts'>
import { getLocalization } from '$lib/i18n';
import CollapsSection from '$lib/collapsible.svelte';
import { createTippy } from 'svelte-tippy';
import 'tippy.js/animations/perspective-subtle.css';
import 'tippy.js/dist/tippy.css';
const tippy = createTippy({
arrow: true,
animation: 'perspective-subtle',
placement: 'right'
});
const { t } = getLocalization();
export let logged_in: boolean;
export let quiz: QuizData;
interface Question {
time: string;
question: string;
image?: string;
answers: Answer[];
}
interface Answer {
right: boolean;
answer: string;
}
interface QuizData {
id: string;
public: boolean;
title: string;
description: string;
created_at: string;
updated_at: string;
user_id: string;
questions: Question[];
}
const startGame = async (id: string): Promise<void> => {
console.log('start game', id);
const res = await fetch(`/api/v1/quiz/start/${id}`, {
method: 'POST'
// headers: {
// 'Content-Type': 'application/json'
// }
});
if (res.status !== 200) {
throw new Error('Failed to start game');
}
const data = await res.json();
// eslint-disable-next-line no-undef
plausible('Started Game', { props: { quiz_id: id } });
window.location.replace(`/admin?token=${data.game_id}&pin=${data.game_pin}&connect=1`);
};
</script>
<div>
<h1 class='text-4xl text-center'>{quiz.title}</h1>
<div class='text-center'>
<p>{quiz.description}</p>
</div>
<div class='flex justify-center m-8'>
{#if logged_in}
<button
class='px-4 py-2 leading-5 text-white transition-colors duration-200 transform bg-gray-700 rounded text-center hover:bg-gray-600 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50'
on:click={() => {
startGame(quiz.id);
}}>
{$t('words.start')}
</button>
{:else }
<button
class='px-4 py-2 leading-5 text-white transition-colors duration-200 transform bg-gray-700 rounded text-center hover:bg-gray-600 focus:outline-none cursor-not-allowed opacity-50'
use:tippy={{content: "You need to be logged in to start a game"}}>
{$t('words.start')}
</button>
{/if}
</div>
{#each quiz.questions as question, index_question}
<div class='px-4 py-1'>
<CollapsSection headerText={question.question}>
<div class='ml-8 grid grid-cols-1 gap-2 m-2 border border-black border-2'>
<h1 class='text-3xl m-1'>{$t('words.question')} {index_question + 1}</h1>
<!-- <label class='m-1 flex flex-row gap-2 w-3/5'>-->
<p
class='text-black w-full bg-inherit'
>{$t('words.question')}: {question.question}</p>
<!-- </label>-->
{#if question.image}
<label class='m-1 flex flex-row gap-2 w-3/5'>
{$t('words.image')}:
<img src='{question.image}' alt='Not provided'>
</label>
{/if}
<label class='m-1 flex flex-row gap-2 w-3/5 flex-nowrap whitespace-nowrap'>
{$t('editor.time_in_seconds')}:
<p>{question.time}</p>
</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 m-1'>
<h1 class='text-3xl m-1'>{$t('words.answer')} {index_answer + 1}</h1>
<p class='m-1'>
{$t('words.answer')}: {index_answer + 1}
{$t('words.question')}: {index_question + 1}
</p>
<p>{$t('words.answer')}
: {quiz.questions[index_question].answers[index_answer].answer}</p>
<label class='m-1 flex flex-row gap-2 w-2/6 flex-nowrap whitespace-nowrap'>
<input type='checkbox' bind:checked={answer.right} class='text-black w-fit'
disabled />
<span class='w-fit'>{$t('editor.right_or_true?')}</span>
</label>
</div>
{/each}
</div>
</CollapsSection>
</div>
{/each}
</div>