diff --git a/frontend/src/pages/Q&A/Q&A.jsx b/frontend/src/pages/Q&A/Q&A.jsx index 75cb30b2..870a95dd 100644 --- a/frontend/src/pages/Q&A/Q&A.jsx +++ b/frontend/src/pages/Q&A/Q&A.jsx @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect } from "react"; import { Button2, Button1 } from "../../components/util/Button"; import style from "../Resources/components/ResourceSharingForm/resource-sharing-form.module.scss"; import "./Ques.scss"; @@ -6,8 +6,12 @@ import { useState } from "react"; import Joi from "joi-browser"; import Loader from "../../components/util/Loader/index"; import { SimpleToast } from "../../components/util/Toast"; -import {END_POINT} from "../../config/api" -import { ErrorSharp } from "@material-ui/icons"; +import { END_POINT } from "../../config/api"; +import { + AirplayTwoTone, + ErrorSharp, + SettingsBluetoothSharp, +} from "@material-ui/icons"; function Ques(props) { let dark = props.theme; @@ -53,12 +57,13 @@ function Ques(props) { const [isUploadingData, setIsUploadingData] = useState(false); const [open, setOpenToast] = useState(false); - const [toastMessage,setToastMessage] = useState(""); - const [severity,setSeverity] = useState('success') + const [toastMessage, setToastMessage] = useState(""); + const [severity, setSeverity] = useState("success"); const [isButtonPressed, setButtonPressed] = useState(false); const [checkedState, setCheckedState] = useState( new Array(Tags.length).fill(false) ); + const [loading, setLoading] = useState(true); const [formdata, setFormData] = useState({ title: "", @@ -70,7 +75,7 @@ function Ques(props) { setTimeout(() => { setOpenToast(false); }, 500); - } + }; const handleOnChange = (position) => { const updatedCheckedState = checkedState.map((item, index) => index === position ? !item : item @@ -105,39 +110,38 @@ function Ques(props) { const data = { ...formdata }; data[input.name] = input.value; setFormData({ ...data, [input.name]: input.value }); - setFormErrors({}) + setFormErrors({}); }; const uploadData = async (formdata) => { try { const url = `${END_POINT}/question`; - const response = await fetch(url,{ - method:"POST", - headers : { - "content-type":"application/json" + const response = await fetch(url, { + method: "POST", + headers: { + "content-type": "application/json", }, - body : JSON.stringify(formdata) + body: JSON.stringify(formdata), }); const data = await response.json(); - setIsUploadingData(false) - setToastMessage("Q&A added successfully!") - setOpenToast(true) - setSeverity("success") + setIsUploadingData(false); + setToastMessage("Q&A added successfully!"); + setOpenToast(true); + setSeverity("success"); setFormData({ title: "", description: "", tags: [], - }) - setFormErrors({}) - setCheckedState(new Array(Tags.length).fill(false)) - } - catch(err) { - setIsUploadingData(false) + }); + setFormErrors({}); + setCheckedState(new Array(Tags.length).fill(false)); + } catch (err) { + setIsUploadingData(false); setToastMessage("Something went wrong!"); - setOpenToast(true) + setOpenToast(true); setSeverity("error"); } - } + }; const handleSubmit = (e) => { e.preventDefault(); let isValid = true; @@ -145,13 +149,13 @@ function Ques(props) { Object.keys(formdata).map((key) => { if (formdata[key] === "" || formdata[key] === null) { errors[key] = `${key} is not allowed to be empty`; - setFormErrors(errors) + setFormErrors(errors); isValid = false; } return 0; }); - if(isValid && formdata.tags.length !== 0) { - setIsUploadingData(true) + if (isValid && formdata.tags.length !== 0) { + setIsUploadingData(true); uploadData(formdata); } }; @@ -161,12 +165,120 @@ function Ques(props) { function DeactiveButton() { setButtonPressed(!isButtonPressed); } + + const [getQuestions, setQuestions] = useState([]); + + function getQues() { + fetch(`${END_POINT}/question/getallquestions`, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }) + .then((res) => res.json()) + .then((data) => { + setLoading(false); + setQuestions(data); + // console.log(data); + }); + } + + const upvote = async (questionId) => { + const response = await fetch(`${END_POINT}/question/upvote`, { + method: "PATCH", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ questionId }), + }); + + if (!response.ok) { + setToastMessage("Failed to upvote question"); + setOpenToast(true); + setSeverity("error"); + throw new Error("Failed to upvote question"); + } + // const data = await response.json(); + getQues(); + setToastMessage("Upvote Successfully"); + setOpenToast(true); + setSeverity("success"); + }; + + const downvote = async (questionId) => { + const response = await fetch(`${END_POINT}/question/downvote`, { + method: "PATCH", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ questionId }), + }); + + if (!response.ok) { + setToastMessage("Failed to downvote question"); + setOpenToast(true); + setSeverity("error"); + throw new Error("Failed to downvote question"); + } + // const data = await response.json(); + // console.log(data); + getQues(); + setToastMessage("Downvote Successfully"); + setOpenToast(true); + setSeverity("success"); + }; + + useEffect(() => { + getQues(); + }, []); + return (
{item.title}
+{item.description}
+ {item.tags.map((i, key) => ( + + #{i} + + ))} +Created At {new Date(item.createdAt).toLocaleString()}
+