diff --git a/peer-prep/src/hooks/useApi.tsx b/peer-prep/src/hooks/useApi.tsx index e28dc207f9..e691d199b2 100644 --- a/peer-prep/src/hooks/useApi.tsx +++ b/peer-prep/src/hooks/useApi.tsx @@ -4,16 +4,10 @@ import { useLocalStorage } from "@mantine/hooks"; import { useAuth } from "./useAuth"; import { useNavigate } from "react-router-dom"; -export interface QuestionServerResponse { - success: boolean; - status: number; - data: T; - message?: string; -} - -export interface UserServerResponse { - user?: T; +export interface ServerResponse { + statusCode: number; message: string; + data: T; } // export interface ServerError { diff --git a/peer-prep/src/hooks/useAuth.tsx b/peer-prep/src/hooks/useAuth.tsx index b5322b55cc..a930497bea 100644 --- a/peer-prep/src/hooks/useAuth.tsx +++ b/peer-prep/src/hooks/useAuth.tsx @@ -1,8 +1,8 @@ import { useLocalStorage } from "@mantine/hooks"; import { createContext, ReactElement, useContext, useMemo } from "react"; import { useNavigate } from "react-router-dom"; -import { User } from "../types/user"; -import useApi, { SERVICE, UserServerResponse } from "./useApi"; +import { User, UserResponseData } from "../types/user"; +import useApi, { ServerResponse, SERVICE } from "./useApi"; import { notifications } from "@mantine/notifications"; export interface AuthContextType { @@ -45,7 +45,7 @@ export const AuthProvider = ({ // call this function when you want to authenticate the user const login = async (data: { email: string; password: string }) => { - fetchData>( + fetchData>( `/user-service/users/login`, SERVICE.USER, { @@ -56,10 +56,10 @@ export const AuthProvider = ({ body: JSON.stringify(data), } ) - .then((data) => { + .then((response) => { // ok! - setUser(data.user || null); - console.log(data.user, "<<<<"); + setUser(response.data.user || null); + console.log(response.data.user, "<<<<"); navigate("/dashboard", { replace: true }); notifications.show({ diff --git a/peer-prep/src/pages/Questions/CreateQuestionPage/CreateQuestionPage.tsx b/peer-prep/src/pages/Questions/CreateQuestionPage/CreateQuestionPage.tsx index b04a6fb8bf..7a364b03c6 100644 --- a/peer-prep/src/pages/Questions/CreateQuestionPage/CreateQuestionPage.tsx +++ b/peer-prep/src/pages/Questions/CreateQuestionPage/CreateQuestionPage.tsx @@ -20,9 +20,10 @@ import ReactQuill from "react-quill"; import "react-quill/dist/quill.snow.css"; import classes from "./CreateQuestionPage.module.css"; -import { Question, QuestionOlsd, TestCase } from "../../../types/question"; -import useApi, { QuestionServerResponse, SERVICE } from "../../../hooks/useApi"; +import { QuestionResponseData, TestCase } from "../../../types/question"; +import useApi, { ServerResponse, SERVICE } from "../../../hooks/useApi"; import { notifications } from "@mantine/notifications"; +import { useNavigate } from "react-router-dom"; export default function CreateQuestionPage() { const [name, setName] = useState(""); @@ -30,6 +31,10 @@ export default function CreateQuestionPage() { const [categories, setCategories] = useState([]); const [description, setDescription] = useState(""); const [testCases, setTestCases] = useState([]); + const [solution, setSolution] = useState(""); + const [link, setLink] = useState(""); + + const navigate = useNavigate(); const [fetchedCategories, setFetchedCategories] = useState< { value: string; label: string }[] @@ -50,27 +55,18 @@ export default function CreateQuestionPage() { const fetchCategories = async () => { try { - const response = await fetchData>( + const response = await fetchData>( "/question-service/categories", SERVICE.QUESTION ); - if (response.success) { - const categories = response.data; - const transformedCategories = categories.map((category: string) => ({ - value: category.toUpperCase(), - label: - category.charAt(0).toUpperCase() + category.slice(1).toLowerCase(), - })); - setFetchedCategories(transformedCategories); - } else { - notifications.show({ - message: - response.message || - "Error fetching categories, please try again later.", - color: "red", - }); - } + const categories = response.data.categories || []; + const transformedCategories = categories.map((category: string) => ({ + value: category.toUpperCase(), + label: + category.charAt(0).toUpperCase() + category.slice(1).toLowerCase(), + })); + setFetchedCategories(transformedCategories); } catch (error: any) { console.error("Error fetching categories", error); notifications.show({ @@ -89,7 +85,7 @@ export default function CreateQuestionPage() { })); try { - const response = await fetchData>( + const response = await fetchData>( "/question-service", SERVICE.QUESTION, { @@ -103,21 +99,19 @@ export default function CreateQuestionPage() { categories, difficulty, testCases: updatedTestCases, + solutionCode: solution, + link, }), } ); + notifications.show({ + message: "Question created successfully!", + color: "green", + }); - if (response.success) { - alert("Question created successfully!"); - window.location.href = "/questions"; - } else { - notifications.show({ - message: - response.message || - "Error creating question, please try again later.", - color: "red", - }); - } + // Redirect to questions page + // todo: redirect to the specific qn page + navigate("/questions", { replace: true }); } catch (error: any) { console.log("Error creating question:", error); notifications.show({ @@ -183,6 +177,14 @@ export default function CreateQuestionPage() { minRows={8} required /> + +