diff --git a/frontend/src/pages/Admin/Components/Faq/Q&A/ManageQ&A/ManageQ&A.jsx b/frontend/src/pages/Admin/Components/Faq/Q&A/ManageQ&A/ManageQ&A.jsx index 66a55b16..3a125f80 100644 --- a/frontend/src/pages/Admin/Components/Faq/Q&A/ManageQ&A/ManageQ&A.jsx +++ b/frontend/src/pages/Admin/Components/Faq/Q&A/ManageQ&A/ManageQ&A.jsx @@ -1,10 +1,34 @@ import { useEffect, useState } from "react"; import { END_POINT } from "../../../../../../config/api"; import style from "./manage.module.scss"; +import { makeStyles } from "@material-ui/core/styles"; +import Modal from "@material-ui/core/Modal"; +import Button from "@material-ui/core/Button"; +import Typography from "@material-ui/core/Typography"; import { AiOutlineArrowLeft } from "react-icons/ai"; import { SimpleToast } from "../../../../../../components/util/Toast/Toast"; import Loader from "../../../../../../components/util/Loader"; +const useStyles = makeStyles((theme) => ({ + modal: { + display: "flex", + alignItems: "center", + justifyContent: "center", + }, + paper: { + backgroundColor: theme.palette.background.paper, + border: "2px solid #000", + boxShadow: theme.shadows[5], + padding: theme.spacing(2, 4, 3), + }, + buttons: { + display: "flex", + marginTop: "10px", + justifyContent: "center", + gap: "10px", + }, +})); + export function Manageqa({ setTab, qId }) { const [ans, setAns] = useState([]); const [qns, setQns] = useState(); @@ -15,6 +39,9 @@ export function Manageqa({ setTab, qId }) { toastType: "", toastMessage: "", }); + const [confirmDelete, setConfirmDelete] = useState(false); + const [questionToDelete, setQuestionToDelete] = useState(null); + const classes = useStyles(); const getQuestion = async (id) => { setIsLoaded(true); try { @@ -39,6 +66,79 @@ export function Manageqa({ setTab, qId }) { }); } }; + + const handleOpenConfirmModal = (id) => { + setConfirmDelete(true); + setQuestionToDelete(id); + }; + + const handleCloseConfirmModal = () => { + setConfirmDelete(false); + }; + + const handleDeleteAnswer = async (answerId) => { + try { + const url = `${END_POINT}/answers/deleteanswer`; + const response = await fetch(url, { + method: "DELETE", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${localStorage.getItem("token")}`, + }, + body: JSON.stringify({ answerId }), + }); + + const data = await response.json(); + setToast({ + ...toast, + toastMessage: "Successfully deleted answer", + toastStatus: true, + toastType: "success", + }); + setToogle(!toogle); + } catch (error) { + console.log(error); + setToast({ + ...toast, + toastMessage: "Unable to delete answer", + toastStatus: true, + toastType: "error", + }); + } + }; + + const handleDeleteQuestion = async () => { + try { + const url = `${END_POINT}/question/deletequestion`; + const response = await fetch(url, { + method: "DELETE", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${localStorage.getItem("token")}`, + }, + body: JSON.stringify({ questionId: questionToDelete }), + }); + + const data = await response.json(); + setToast({ + ...toast, + toastMessage: "Successfully deleted question", + toastStatus: true, + toastType: "success", + }); + setTab(18); + setToogle(!toogle); + } catch (error) { + console.log(error); + setToast({ + ...toast, + toastMessage: "Unable to delete question", + toastStatus: true, + toastType: "error", + }); + } + }; + const updateQuestion = async (id, status) => { try { const qUrl = `${END_POINT}/question/updateStatus`; @@ -68,6 +168,7 @@ export function Manageqa({ setTab, qId }) { }); } }; + const getAnswer = async (questionId) => { try { const aUrl = `${END_POINT}/answers/${questionId}`; @@ -92,6 +193,7 @@ export function Manageqa({ setTab, qId }) { }); } }; + const updateAnswer = async (id, status) => { try { const aUrl = `${END_POINT}/answers/updateStatus`; @@ -121,16 +223,19 @@ export function Manageqa({ setTab, qId }) { }); } }; + const handleCloseToast = (event, reason) => { if (reason === "clickaway") { return; } setToast({ ...toast, toastStatus: false }); }; + useEffect(() => { getQuestion(qId); getAnswer(qId); }, [toogle]); + return (

Manage Q&A

@@ -139,36 +244,57 @@ export function Manageqa({ setTab, qId }) {
{isLoaded ? : null}
{isLoaded || ( -
-
-

{qns?.title}

-

Question

-
-

{qns?.description}

-
- - + <> +

Question

+
+
+
+

{qns?.title}

+
+

{qns?.description}

+
+ + +
+
+ {qns?.tags?.map((tag) => ( +

{tag}

+ ))}
+
+
+
+
+

Answers

- {ans?.length === 0 ? ( - No answers Found - ) : ( - ans?.map((a) => ( - <> -

Answers

+ {ans?.length === 0 ? ( + No answers Found + ) : ( +
+ {ans?.map((a) => ( +
+

{a.answer}

@@ -178,28 +304,63 @@ export function Manageqa({ setTab, qId }) { ? style["button-delete"] : style["button-approve"] } - id={`${a._id}`} + id={`${a?._id}`} onClick={(e) => { updateAnswer(e.currentTarget.id, !a?.isApproved); }} > - {a?.isApproved ? "DisApprove" : "Approve"} + {a?.isApproved ? "Reject" : "Approve"} + + -
- - )) - )} - -
- {qns?.tags?.map((tag) => ( -

{tag}

+
+
))}
-
-
+ )} + )} + { + +
+ + Confirm Delete + + + Are you sure you want to delete this question and all its answers? + +
+ + +
+
+
+ } {toast.toastStatus && (