From 24dc68a7b1463b191f93828bfb7e7067417428e4 Mon Sep 17 00:00:00 2001
From: balaharisankar <balaharisankar2003@gmail.com>
Date: Thu, 1 Aug 2024 00:06:58 +0530
Subject: [PATCH] Q&A page fixed

---
 frontend/src/pages/Q&A/Q&A.jsx | 109 ++++++++++++++++++---------------
 1 file changed, 59 insertions(+), 50 deletions(-)

diff --git a/frontend/src/pages/Q&A/Q&A.jsx b/frontend/src/pages/Q&A/Q&A.jsx
index 0423c21d..90ef356f 100644
--- a/frontend/src/pages/Q&A/Q&A.jsx
+++ b/frontend/src/pages/Q&A/Q&A.jsx
@@ -122,14 +122,18 @@ function Ques(props) {
     }
   };
 
+  const filterApprovedQuestions = (questions) => {
+    return questions.filter((question) => question.isApproved == true)
+  }
+
   const [getQuestions, setQuestions] = useState([]);
   const fetchQuestions = () => {
     getAllQuestion(setToast).then((data) => {
-      setLoading(false);
+      setLoading(true);
       data = data.map((item) => {
         return { ...item, tags: item.tags[0] };
       });
-      setQuestions(data);
+      setQuestions(filterApprovedQuestions(data));
     });
   };
 
@@ -144,6 +148,7 @@ function Ques(props) {
   };
 
   useEffect(() => {
+    setLoading(false)
     fetchQuestions();
   }, []);
 
@@ -152,55 +157,59 @@ function Ques(props) {
       className="popup-creator"
       style={{ background: dark ? "#171717" : "white" }}
     >
-      {getQuestions.length <= 0 ? (
-        <Loader />
-      ) : (
-        <div className="question-cards">
-          {getQuestions?.map((item, key) => {
-            let tags = [...Object.values(item.tags)];
-            return (
-              <div className="question-card" key={key}>
-                <div className="card-up">
-                  <p>{item.title}</p>
-                  <p>{item.description}</p>
-                  <div className="tags-container">
-                    {tags.map((i, index) => {
-                      if (i == true)
-                        return (
-                          <span className="tag-space" key={index}>
-                            {i === true ? `#${Tags[index].value}` : ""}
-                          </span>
-                        );
-                    })}
-                  </div>
-                </div>
-                <div className="card-down">
-                  <div>
-                    <p>
-                      Created At {new Date(item.createdAt).toLocaleString()}
-                    </p>
-                  </div>
-                  <div>
-                    <button
-                      className="vote-btn"
-                      onClick={() => handleUpvote(item._id)}
-                    >
-                      👍{item.upvotes}
-                    </button>
-                    <button
-                      className="vote-btn"
-                      onClick={() => handleDownvote(item._id)}
-                    >
-                      👎 {item?.downvotes}
-                    </button>
+      {
+        !loading ?
+          <Loader /> :
+          getQuestions.length == 0 ?
+            <div>
+              <h1 className="py-5 text-center">No Questions Found !</h1>
+            </div>
+            :
+            <div className="question-cards">
+              {getQuestions?.map((item, key) => {
+                let tags = [...Object.values(item.tags)];
+                return (
+                  <div className="question-card" key={key}>
+                    <div className="card-up">
+                      <p>{item.title}</p>
+                      <p>{item.description}</p>
+                      <div className="tags-container">
+                        {tags.map((i, index) => {
+                          if (i == true)
+                            return (
+                              <span className="tag-space" key={index}>
+                                {i === true ? `#${Tags[index].value}` : ""}
+                              </span>
+                            );
+                        })}
+                      </div>
+                    </div>
+                    <div className="card-down">
+                      <div>
+                        <p>
+                          Created At {new Date(item.createdAt).toLocaleString()}
+                        </p>
+                      </div>
+                      <div>
+                        <button
+                          className="vote-btn"
+                          onClick={() => handleUpvote(item._id)}
+                        >
+                          👍{item.upvotes}
+                        </button>
+                        <button
+                          className="vote-btn"
+                          onClick={() => handleDownvote(item._id)}
+                        >
+                          👎 {item?.downvotes}
+                        </button>
+                      </div>
+                    </div>
                   </div>
-                </div>
-              </div>
-            );
-          })}
-        </div>
-      )}
-
+                );
+              })}
+            </div>
+      }
       {toast.toastStatus && (
         <SimpleToast
           open={toast.toastStatus}