Skip to content

Commit

Permalink
Fix questions
Browse files Browse the repository at this point in the history
  • Loading branch information
EloToJaa committed Jan 9, 2024
1 parent f9873fc commit d84babf
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 72 deletions.
12 changes: 12 additions & 0 deletions src/api/types/quiz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,16 @@ export type QuizResponse = {
description: string;
averageRating: number;
numberOfRatings: number;
questions: QuestionResponse[];
}

export type QuestionResponse = {
id: string;
question: string;
answers: Answer[];
}

export type Answer = {
text: string;
isCorrect: boolean;
}
20 changes: 10 additions & 10 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Route, Routes } from "react-router-dom";
import Layout from "./layout/Layout";
import Flashcards from "./layout/Quiz/Flashcards";
import Quiz from "./layout/Quiz/Quiz";
import Test from "./layout/Quiz/Test";
import ForgotPassword from "./page/ForgotPassword";
import Home from "./page/Home";
import PasswordReset from "./page/PasswordReset";
import SignIn from "./page/SignIn";
import SignUp from "./page/SignUp";
import Users from "./page/Users";
import Verification from "./page/Verification";
import Verified from "./page/Verified";
import PersistLogin from "./router/PersistLogin";
import RequireAuth from "./router/RequireAuth";
import NotFound from "./status/NotFound";
import SignUp from "./page/SignUp";
import Verification from "./page/Verification";
import Verified from "./page/Verified";
import PasswordReset from "./page/PasswordReset";
import ForgotPassword from "./page/ForgotPassword";
import Quiz from "./layout/Quiz/Quiz";
import Flashcards from "./layout/Quiz/Flashcards";
import Test from "./layout/Quiz/Test";

export default function App() {
return (
Expand All @@ -28,13 +28,13 @@ export default function App() {
<Route path="/password_reset" element={<PasswordReset />} />
<Route path="/forgot_password" element={<ForgotPassword />} />

<Route path="/quiz" element={<Quiz />} />
{/* <Route path="/flashcards" element={<Flashcards />} /> */}

<Route element={<RequireAuth />}>
<Route path="/users" element={<Users />} />
</Route>

<Route path="/:userName/:quizSlug" element={<Quiz />} />

<Route path="*" element={<NotFound />} />
</Route>
<Route path="/flashcards" element={<Flashcards />} />
Expand Down
12 changes: 6 additions & 6 deletions src/components/layout/Quiz/Flashcard.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { QuestionResponse } from "@/api/types/quiz";
import { useState } from "react";
import { TermProps } from "./Term";

interface FlashcardProps {
currTerm: number;
terms: TermProps[];
question: QuestionResponse | undefined;
}

export default function Flashcard({ currTerm, terms }: FlashcardProps) {
export default function Flashcard({ question }: FlashcardProps) {
const [isFlipped, setIsFlipped] = useState<boolean>(false);
if(!question) return (<></>);

const handleFlip = () => {
setIsFlipped(!isFlipped);
Expand All @@ -22,12 +22,12 @@ export default function Flashcard({ currTerm, terms }: FlashcardProps) {
>
<div className="bg-secondary flex items-center justify-center rounded-md p-4 absolute w-full h-full [backface-visibility:hidden]">
<p className="text-xl sm:text-2xl xl:text-3xl">
{terms[currTerm].term}
{question.question}
</p>
</div>
<div className="bg-secondary flex items-center justify-center rounded-md p-4 w-full h-full overflow-auto [backface-visibility:hidden] [transform:rotateX(180deg)]">
<p className="text-xl sm:text-2xl xl:text-3xl">
{terms[currTerm].desc}
{question.answers[0].text}
</p>
</div>
</button>
Expand Down
6 changes: 3 additions & 3 deletions src/components/layout/Quiz/Flashcards.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Toaster } from "react-hot-toast";
import { useState } from "react";
import { TermProps } from "./Term";
import { Toaster } from "react-hot-toast";
import ControlBar from "./ControlBar";
import Flashcard from "./Flashcard";
import QuizHeader from "./QuizHeader";
import { TermProps } from "./Term";

export default function Flashcards() {
const [currTerm, setCurrTerm] = useState<number>(0);
Expand Down Expand Up @@ -35,7 +35,7 @@ export default function Flashcards() {
<div className="mx-auto rounded-md max-w-[592px] md:max-w-[720px] lg:max-w-[976px]">
<div className="flex flex-col gap-10 w-full mb-16">
<div className="flex items-center justify-center h-[420px] sm:h-[460px] md:h-[460px] lg:h-[500px] [perspective:1000px]">
<Flashcard currTerm={currTerm} terms={terms} />
<Flashcard currTerm={currTerm} question={terms} />
</div>

<ControlBar
Expand Down
111 changes: 59 additions & 52 deletions src/components/layout/Quiz/Quiz.tsx
Original file line number Diff line number Diff line change
@@ -1,79 +1,86 @@
import { api } from "@/api/axios";
import { QuestionResponse, QuizResponse } from "@/api/types/quiz";
import NotFound from "@/components/status/NotFound";
import { useQuery } from "@tanstack/react-query";
import { useState } from "react";
import { useParams } from "react-router-dom";
import Category from "./Category";
import Term from "./Term";
import { TermProps } from "./Term";
import ControlBar from "./ControlBar";
import Flashcard from "./Flashcard";
import Term from "./Term";

export default function Quiz() {
const [currTerm, setCurrTerm] = useState<number>(0);
const { userName, quizSlug } = useParams();

const { isLoading, isError, data } = useQuery({
queryKey: ["quizes"],
queryFn: () => api.get<QuizResponse>(`/api/v1/Quiz/${userName}/${quizSlug}`),
});

const terms: TermProps[] = [
{ term: "Quicksort1", desc: "XXXXXXXXX1" },
{ term: "Quicksort2", desc: "XXXXXXXXX2" },
{ term: "Quicksort3", desc: "XXXXXXXXX3" },
{ term: "Quicksort4", desc: "XXXXXXXXX4" },
{ term: "Quicksort5", desc: "XXXXXXXXX5" },
];
const getNumberOfQuestions = (questions: QuestionResponse[] | undefined) => {
if(!questions) return 0;
return questions.length;
}

const increment = () => {
currTerm === terms.length - 1
currTerm === getNumberOfQuestions(data?.data.questions) - 1
? setCurrTerm(0)
: setCurrTerm(prev => prev + 1);
};

const decrement = () => {
currTerm === 0
? setCurrTerm(terms.length - 1)
? setCurrTerm(getNumberOfQuestions(data?.data.questions) - 1)
: setCurrTerm(prev => prev - 1);
};

return (
<div className="bg-primary w-full text-white px-4 py-10 sm:py-12 md:py-16 lg:py-20">
<div className="mx-auto rounded-md max-w-[592px] md:max-w-[720px] lg:max-w-[976px]">
<h2 className="text-2xl font-bold mb-10 xl:text-3xl">Karaluch</h2>
<div className="flex flex-col gap-10 w-full mb-16">
<div className="flex items-center justify-center h-[280px] sm:h-[320px] md:h-[360px] lg:h-[420px] [perspective:1000px]">
<Flashcard currTerm={currTerm} terms={terms} />
</div>
return (
<>
{isError && <NotFound />}
{isLoading && <div className="text-red-600">Loading...</div>}
{!isError && !isLoading && <div className="bg-primary w-full text-white px-4 py-10 sm:py-12 md:py-16 lg:py-20">
<div className="mx-auto rounded-md max-w-[592px] md:max-w-[720px] lg:max-w-[976px]">
<h2 className="text-2xl font-bold mb-10 xl:text-3xl">{data?.data.name}</h2>
<div className="flex flex-col gap-10 w-full mb-16">
<div className="flex items-center justify-center h-[280px] sm:h-[320px] md:h-[360px] lg:h-[420px] [perspective:1000px]">
<Flashcard question={data?.data.questions[currTerm]} />
</div>

<ControlBar
curr={currTerm + 1}
max={terms.length}
left={decrement}
right={increment}
/>
<ControlBar
curr={currTerm + 1}
max={getNumberOfQuestions(data?.data.questions)}
left={decrement}
right={increment}
/>

<div className="grid gap-2 sm:grid-cols-2 lg:grid-cols-4">
<Category text="Fiszki" />
<Category text="Ucz się" />
<Category text="Test" />
<Category text="Dopasowania" />
<div className="grid gap-2 sm:grid-cols-2 lg:grid-cols-4">
<Category text="Fiszki" />
<Category text="Ucz się" />
<Category text="Test" />
<Category text="Dopasowania" />
</div>
</div>
</div>
<div className="mb-16">
<div className="flex items-center gap-4 mb-6">
<div className="w-12 h-12 bg-lightBlue rounded-3xl"></div>
<p className="font-semibold">Szymon Budziak</p>
<div className="mb-16">
<div className="flex items-center gap-4 mb-6">
<div className="w-12 h-12 bg-lightBlue rounded-3xl"></div>
<p className="font-semibold">{data?.data.userName}</p>
</div>
<p className="overflow-auto">
{data?.data.description}
</p>
</div>
<h3 className="text-xl font-bold mb-10 xl:text-2xl">
Pojęcia w tym zestawie
</h3>
<div className="flex flex-col gap-5">
{data?.data.questions.map((el, id) => {
return <Term key={id} term={el.question} desc={el.answers[0].text} />;
})}
</div>
<p className="overflow-auto">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Placeat
illum explicabo necessitatibus fugiat soluta adipisci porro sit
atque. Repellendus magnam harum iure eaque atque aspernatur sapiente
obcaecati ad impedit et amet tempore mollitia quis quasi nihil
recusandae distinctio, voluptatem pariatur illo reprehenderit illum
esse fuga incidunt facere. Soluta, inventore doloribus.
</p>
</div>
<h3 className="text-xl font-bold mb-10 xl:text-2xl">
Pojęcia w tym zestawie
</h3>
<div className="flex flex-col gap-5">
{terms.map((el, id) => {
return <Term key={id} term={el.term} desc={el.desc} />;
})}
</div>
</div>
</div>
}
</>
);
}
2 changes: 1 addition & 1 deletion src/components/layout/Sections/TileGrid/QuizTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type QuizTileProps = {
};

export default function QuizTile({ quiz }: QuizTileProps) {
const url = `/${quiz.userName}/${quiz.name}`;
const url = `/${quiz.userName}/${quiz.slug}`;
return (
<div className="bg-secondary rounded-md w-full">
<a href={url} className="relative">
Expand Down

0 comments on commit d84babf

Please sign in to comment.