-
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.
Merge pull request #29 from HereEast/feat/questions-page
feat: Add all Questions page
- Loading branch information
Showing
14 changed files
with
90 additions
and
156 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 |
---|---|---|
@@ -1,18 +1,23 @@ | ||
import axios from "axios"; | ||
|
||
import { IQuestion } from "~/models/Question"; | ||
import { BASE_URL } from "~/utils/constants"; | ||
|
||
// GET ALL QUESTIONS | ||
export async function getQuestions() { | ||
try { | ||
const response = await axios.get<IQuestion[]>(`/api/questions`); | ||
const response = await fetch(`${BASE_URL}/api/questions`); | ||
|
||
if (!response.ok) { | ||
throw new Error("🔴 Data fetch failed (questions)"); | ||
} | ||
|
||
const data = response.data; | ||
const people: IQuestion[] = await response.json(); | ||
|
||
return data; | ||
return people; | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
console.log(err); | ||
console.log("🔴 Error:", err.message); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,83 +1,5 @@ | ||
"use client"; | ||
import { QuestionsPage } from "~/components/layouts/QuestionsPage"; | ||
|
||
import { FormEvent, useState } from "react"; | ||
|
||
import { PageLayout } from "~/components/layouts/PageLayout"; | ||
|
||
import { IFormDataProps, submitAnswers } from "~/api-client/answers"; | ||
import { useQuestions } from "~/hooks"; | ||
|
||
export default function QuestionsPage() { | ||
const { data: questions, isLoading } = useQuestions(); | ||
|
||
const [error, setError] = useState(""); | ||
|
||
async function handleSubmit(e: FormEvent<HTMLFormElement>) { | ||
e.preventDefault(); | ||
setError(""); | ||
|
||
const formData = new FormData(e.currentTarget); | ||
|
||
const isEmptyValue = Array.from(formData.values()).some( | ||
(value) => String(value).trim().length === 0, | ||
); | ||
|
||
if (isEmptyValue) { | ||
setError("All inputs are required."); | ||
return; | ||
} | ||
|
||
const answersData: IFormDataProps[] = Array.from(formData.entries()).map( | ||
([questionId, answer], index) => { | ||
return { | ||
questionId, | ||
question: questions?.[index].body || "", | ||
answer: answer as string, | ||
}; | ||
}, | ||
); | ||
|
||
console.log(answersData); | ||
|
||
const result = await submitAnswers(answersData); | ||
console.log(result); // Console | ||
} | ||
|
||
return ( | ||
<PageLayout className="bg-stone-200 px-20"> | ||
{isLoading && <div>Loading...</div>} | ||
|
||
{error && ( | ||
<div className="mb-6 bg-red-600 px-4 py-3 text-base text-stone-50"> | ||
{error} | ||
</div> | ||
)} | ||
|
||
{questions && questions.length > 0 && ( | ||
<form onSubmit={handleSubmit} className="mb-20 space-y-10"> | ||
{questions.map((question, index) => ( | ||
<div className="flex w-full flex-col gap-2" key={index}> | ||
<label | ||
htmlFor={String(question._id)} | ||
className="font-medium tracking-tight" | ||
> | ||
{`${question.order}. ${question.body}`} | ||
</label> | ||
|
||
<textarea | ||
id={String(question._id)} | ||
name={String(question._id)} | ||
rows={6} | ||
className="border border-stone-700 px-5 py-4 text-base outline-none focus:border-stone-400" | ||
/> | ||
</div> | ||
))} | ||
|
||
<button type="submit" className="w-40 bg-black p-4 text-stone-50"> | ||
Submit | ||
</button> | ||
</form> | ||
)} | ||
</PageLayout> | ||
); | ||
export default function Questions() { | ||
return <QuestionsPage />; | ||
} |
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
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
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,50 @@ | ||
import Link from "next/link"; | ||
import { notFound } from "next/navigation"; | ||
|
||
import { PageContainer } from "./PageContainer"; | ||
|
||
import { getQuestions } from "~/api-client/questions"; | ||
import { IQuestion } from "~/models/Question"; | ||
import { BASE_URL } from "~/utils/constants"; | ||
|
||
export async function QuestionsPage() { | ||
const questions = await getQuestions(); | ||
|
||
console.log(questions); | ||
|
||
if (!questions) { | ||
notFound(); | ||
} | ||
|
||
return ( | ||
<PageContainer className="max-w-4xl"> | ||
{questions && ( | ||
<ul className="space-y-1"> | ||
{questions.map((item, index) => ( | ||
<li key={index}> | ||
<QuestionItem question={item} /> | ||
</li> | ||
))} | ||
</ul> | ||
)} | ||
</PageContainer> | ||
); | ||
} | ||
|
||
// Question Item | ||
interface QuestionItemProps { | ||
question: IQuestion; | ||
} | ||
|
||
function QuestionItem({ question }: QuestionItemProps) { | ||
return ( | ||
question && ( | ||
<Link | ||
href={`${BASE_URL}/questions/${question.slug}`} | ||
className="inline-block w-full rounded-full bg-stone-100 px-8 py-4 text-xl hover:bg-stone-900 hover:text-stone-50" | ||
> | ||
<span>{question.body}</span> | ||
</Link> | ||
) | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.