From 388bd0a08da1c651c3a5c6d7dfc5c9867372faf7 Mon Sep 17 00:00:00 2001 From: Felipe D'Abrantes <felidabrantes@gmail> Date: Fri, 21 Apr 2023 19:58:55 +0100 Subject: [PATCH] Add a Questions Management table --- .../src/pages/questions.tsx | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 daily-thought-frontend/src/pages/questions.tsx diff --git a/daily-thought-frontend/src/pages/questions.tsx b/daily-thought-frontend/src/pages/questions.tsx new file mode 100644 index 00000000..9e57b67a --- /dev/null +++ b/daily-thought-frontend/src/pages/questions.tsx @@ -0,0 +1,55 @@ +import NavBar from '@/components/navigation/NavBar'; +import { useQuestions } from '@/hooks/useQuestions'; +import { useUser } from '@/hooks/useUser'; +import { useState } from 'react'; +import NewQuestionForm from '@/components/questions/NewQuestionForm'; +import QuestionList from '@/components/questions/QuestionList'; + +const Questions = () => { + const { user } = useUser(); + const { questions, setRehydrateQuestions } = useQuestions(); + const [newQuestion, setNewQuestion] = useState(''); + + const insertNewQuestion = async (newQuestion: String): Promise<void> => { + const JSONdata = JSON.stringify({ questionText: newQuestion }); + const endpoint = `${process.env.NEXT_PUBLIC_FEED_SERVICE_URL}question`; + const options = { + method: 'POST', + headers: { + Authorization: `Bearer ${sessionStorage.getItem('token')}`, + 'Content-Type': 'application/json' + }, + body: JSONdata + }; + await fetch(endpoint, options); + }; + + const onQuestionSubmit = (): void => { + insertNewQuestion(newQuestion); + setRehydrateQuestions(true); + }; + + return ( + <div className="flex flex-col w-full items-center h-screen"> + {/* Navigation Bar */} + <div className="w-full"> + <NavBar user={user} /> + </div> + + {/* Question Insert */} + <NewQuestionForm + newQuestion={newQuestion} + setNewQuestion={setNewQuestion} + onSubmit={onQuestionSubmit} + /> + + {/* List of Questions */} + <div className="flex flex-col items-center w-full shadow-lg min-h-32 pb-4 overflow-auto"> + <h2 className="text-xl font-semibold leading-10 pt-4 text-gray-900">Questions Stored</h2> + {questions === undefined ? <p>Loading</p> : <QuestionList questions={questions} />} + </div> + </div> + ); +}; + +export default Questions; -- GitLab