[d7b7f00] | 1 | import styles from "../../css/AdminPanelCss/admin-style.module.css";
|
---|
| 2 | import ApplicationCard from "./ApplicationCard";
|
---|
| 3 | import React, { useEffect, useState } from "react";
|
---|
| 4 | import AdminReviewCard from "./AdminReviewCard";
|
---|
| 5 | import ReviewModal from "./AdminReviewModal";
|
---|
| 6 | import ApplicationModal from "./AdminApplicationModal";
|
---|
| 7 | import { useNavigate } from "react-router-dom";
|
---|
| 8 | import AdminOrderCard from "./AdminOrderCard";
|
---|
| 9 | import AdminOrderReviewCard from "./AdminOrderReviewCard";
|
---|
| 10 | import AdminOrderModal from "./AdminOrderModal";
|
---|
| 11 | import AdminOrderReviewModal from "./AdminOrderReviewModal";
|
---|
| 12 | import AdminRecipeApplicationCard from "./AdminRecipeApplicationCard";
|
---|
| 13 | import AdminRecipeApplicationModal from "./AdminRecipeApplicationModal";
|
---|
| 14 |
|
---|
| 15 | const AdminPanel = () => {
|
---|
| 16 | const [active, setActive] = useState("applications");
|
---|
| 17 | const [page, setPage] = useState(0);
|
---|
| 18 | const [totalPages, setTotalPages] = useState(0);
|
---|
| 19 | const [cards, setCards] = useState([]);
|
---|
| 20 | const [reload, setReload] = useState(false);
|
---|
| 21 | const [selectedReview, setSelectedReview] = useState(null);
|
---|
| 22 | const [selectedApplication, setSelectedApplication] = useState(null);
|
---|
| 23 | const [selectedOrder, setSelectedOrder] = useState(null);
|
---|
| 24 | const [selectedOrderReview, setSelectedOrderReview] = useState(null);
|
---|
| 25 | const [selectedRecipeApplication, setSelectedRecipeApplication] = useState(null)
|
---|
| 26 | const [showPending, setShowPending] = useState(false);
|
---|
| 27 | const [showAccepted, setShowAccepted] = useState(false);
|
---|
| 28 | const [showFinished, setShowFinished] = useState(false);
|
---|
| 29 | const [pending, setPending] = useState([]);
|
---|
| 30 | const [accepted, setAccepted] = useState([]);
|
---|
| 31 | const [finished, setFinished] = useState([]);
|
---|
| 32 | const navigate = useNavigate();
|
---|
| 33 |
|
---|
| 34 | useEffect(() => {
|
---|
| 35 | const url = "http://localhost:8080/api";
|
---|
| 36 | const token = localStorage.getItem("token");
|
---|
| 37 | setReload(false);
|
---|
| 38 |
|
---|
| 39 | const fetchData = async () => {
|
---|
| 40 | try {
|
---|
| 41 | let response;
|
---|
| 42 | if(active === "orders")
|
---|
| 43 | {
|
---|
| 44 | response = await fetch(`${url}/admin/orders?page=0&size=100`, {
|
---|
| 45 | method: 'GET',
|
---|
| 46 | headers: {
|
---|
| 47 | 'Authorization': `Bearer ${token}`,
|
---|
| 48 | "Content-Type": 'application/json'
|
---|
| 49 | }
|
---|
| 50 | });
|
---|
| 51 | }
|
---|
| 52 | else
|
---|
| 53 | {
|
---|
| 54 | response = await fetch(`${url}/admin/${active}?page=${page}&size=4`, {
|
---|
| 55 | method: "GET",
|
---|
| 56 | headers: {
|
---|
| 57 | Authorization: `Bearer ${token}`,
|
---|
| 58 | "Content-Type": "application/json",
|
---|
| 59 | },
|
---|
| 60 | });
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | if (response.ok) {
|
---|
| 64 | const data = await response.json();
|
---|
| 65 | setCards(data.content || []);
|
---|
| 66 | setTotalPages(data.totalPages || 0);
|
---|
| 67 |
|
---|
| 68 | if (active === "orders") {
|
---|
| 69 | const newPending = [];
|
---|
| 70 | const newAccepted = [];
|
---|
| 71 | const newFinished = [];
|
---|
| 72 | data.content.forEach((content) => {
|
---|
| 73 | let deliveryPerson = content.deliveryPerson;
|
---|
| 74 | let order = content.order;
|
---|
| 75 |
|
---|
| 76 | if (deliveryPerson === null) {
|
---|
| 77 | newPending.push(content);
|
---|
| 78 | } else if (order.finished) {
|
---|
| 79 | newFinished.push(content);
|
---|
| 80 | } else {
|
---|
| 81 | newAccepted.push(content);
|
---|
| 82 | }
|
---|
| 83 | });
|
---|
| 84 |
|
---|
| 85 | setPending(newPending);
|
---|
| 86 | setAccepted(newAccepted);
|
---|
| 87 | setFinished(newFinished);
|
---|
| 88 | } else {
|
---|
| 89 | setPending([]);
|
---|
| 90 | setAccepted([]);
|
---|
| 91 | setFinished([]);
|
---|
| 92 | }
|
---|
| 93 | } else if (response.status === 403) {
|
---|
| 94 | console.log(response.status)
|
---|
| 95 | // navigate("/");
|
---|
| 96 | } else {
|
---|
| 97 | alert("Bad request.");
|
---|
| 98 | }
|
---|
| 99 | } catch (error) {
|
---|
| 100 | console.log(error);
|
---|
| 101 | }
|
---|
| 102 | };
|
---|
| 103 |
|
---|
| 104 | fetchData();
|
---|
| 105 | }, [active, page, reload, navigate]);
|
---|
| 106 |
|
---|
| 107 | const handleApplicationClick = () => {
|
---|
| 108 | setActive("applications");
|
---|
| 109 | setPage(0);
|
---|
| 110 | };
|
---|
| 111 |
|
---|
| 112 | const handleCommentsClick = () => {
|
---|
| 113 | setActive("reviews");
|
---|
| 114 | setPage(0);
|
---|
| 115 | };
|
---|
| 116 |
|
---|
| 117 | const handleOrdersClick = () => {
|
---|
| 118 | setActive("orders");
|
---|
| 119 | setPage(0);
|
---|
| 120 | };
|
---|
| 121 |
|
---|
| 122 | const handleOrderReviewsClick = () => {
|
---|
| 123 | setActive("orderreviews");
|
---|
| 124 | setPage(0);
|
---|
| 125 | };
|
---|
| 126 |
|
---|
| 127 | const handleRecipeApplicationClick = () => {
|
---|
| 128 | setActive("recipeapplications");
|
---|
| 129 | setPage(0);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | const handlePageChange = (newPage) => {
|
---|
| 133 | setPage(newPage);
|
---|
| 134 | };
|
---|
| 135 |
|
---|
| 136 | const openReviewModal = (reviewData) => {
|
---|
| 137 | setSelectedReview(reviewData);
|
---|
| 138 | };
|
---|
| 139 |
|
---|
| 140 | const openApplicationModal = (applicationData) => {
|
---|
| 141 | setSelectedApplication(applicationData);
|
---|
| 142 | };
|
---|
| 143 |
|
---|
| 144 | const openOrderModal = (orderData) => {
|
---|
| 145 | setSelectedOrder(orderData);
|
---|
| 146 | };
|
---|
| 147 |
|
---|
| 148 | const openOrderReviewModal = (orderReviewData) => {
|
---|
| 149 | setSelectedOrderReview(orderReviewData);
|
---|
| 150 | };
|
---|
| 151 |
|
---|
| 152 | const openRecipeApplicationModal = (recipeApplicationData) => {
|
---|
| 153 | setSelectedRecipeApplication(recipeApplicationData)
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | const closeModals = () => {
|
---|
| 157 | setSelectedReview(null);
|
---|
| 158 | setSelectedApplication(null);
|
---|
| 159 | setSelectedOrder(null);
|
---|
| 160 | setSelectedOrderReview(null);
|
---|
| 161 | setSelectedRecipeApplication(null);
|
---|
| 162 | };
|
---|
| 163 |
|
---|
| 164 | const handleRemoveReview = async (reviewId) => {
|
---|
| 165 | let token = localStorage.getItem("token");
|
---|
| 166 | try {
|
---|
| 167 | const response = await fetch(`http://localhost:8080/api/admin/reviews/${reviewId}`, {
|
---|
| 168 | method: 'DELETE',
|
---|
| 169 | headers: {
|
---|
| 170 | "Content-Type": "application/json",
|
---|
| 171 | 'Authorization': `Bearer ${token}`
|
---|
| 172 | }
|
---|
| 173 | });
|
---|
| 174 | if (response.ok) {
|
---|
| 175 | setReload(true);
|
---|
| 176 | } else {
|
---|
| 177 | alert("Bad request.");
|
---|
| 178 | }
|
---|
| 179 | } catch (error) {
|
---|
| 180 | console.error(error);
|
---|
| 181 | alert("Error removing review.");
|
---|
| 182 | }
|
---|
| 183 | };
|
---|
| 184 |
|
---|
| 185 | const handleRemoveOrderReview = async (orderId) => {
|
---|
| 186 | let token = localStorage.getItem("token");
|
---|
| 187 | try {
|
---|
| 188 | const response = await fetch(`http://localhost:8080/api/admin/orderreviews/${orderId}`, {
|
---|
| 189 | method: 'DELETE',
|
---|
| 190 | headers: {
|
---|
| 191 | 'Content-Type': 'application/json',
|
---|
| 192 | 'Authorization': `Bearer ${token}`
|
---|
| 193 | }
|
---|
| 194 | });
|
---|
| 195 | if(response.ok)
|
---|
| 196 | {
|
---|
| 197 | setReload(true)
|
---|
| 198 | }
|
---|
| 199 | else if(response.status === 403)
|
---|
| 200 | {
|
---|
| 201 | navigate("/");
|
---|
| 202 | }
|
---|
| 203 | else
|
---|
| 204 | {
|
---|
| 205 | console.log(response.status);
|
---|
| 206 | alert("Bad request.")
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 | catch (error) {
|
---|
| 210 | console.error(error);
|
---|
| 211 | alert("Error removing order review.")
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | return (
|
---|
| 216 | <>
|
---|
| 217 | <div className={styles.container}>
|
---|
| 218 | <div className={styles.topButtons}>
|
---|
| 219 | <div
|
---|
| 220 | className={active === "applications" ? styles.buttonClicked : styles.button}
|
---|
| 221 | onClick={handleApplicationClick}
|
---|
| 222 | >
|
---|
| 223 | User Applications
|
---|
| 224 | </div>
|
---|
| 225 | <div
|
---|
| 226 | className={active === "orders" ? styles.buttonClicked : styles.button}
|
---|
| 227 | onClick={handleOrdersClick}
|
---|
| 228 | >
|
---|
| 229 | All Orders
|
---|
| 230 | </div>
|
---|
| 231 | <div
|
---|
| 232 | className={active === "orderreviews" ? styles.buttonClicked : styles.button}
|
---|
| 233 | onClick={handleOrderReviewsClick}
|
---|
| 234 | >
|
---|
| 235 | Order Reviews
|
---|
| 236 | </div>
|
---|
| 237 | <div
|
---|
| 238 | className={active === "reviews" ? styles.buttonClicked : styles.button}
|
---|
| 239 | onClick={handleCommentsClick}
|
---|
| 240 | >
|
---|
| 241 | User Comments
|
---|
| 242 | </div>
|
---|
| 243 | <div
|
---|
| 244 | className={active === "recipeapplications" ? styles.buttonClicked : styles.button}
|
---|
| 245 | onClick={handleRecipeApplicationClick}
|
---|
| 246 | >
|
---|
| 247 | Recipe Applications
|
---|
| 248 | </div>
|
---|
| 249 | </div>
|
---|
| 250 | <div className={styles.cardContainer}>
|
---|
| 251 | {
|
---|
| 252 | cards.length !== 0 ? (
|
---|
| 253 | cards.map((cardData, index) =>
|
---|
| 254 | active === "applications" ? (
|
---|
| 255 | <ApplicationCard
|
---|
| 256 | key={index}
|
---|
| 257 | data={cardData}
|
---|
| 258 | onClick={(completeData) => openApplicationModal(completeData)}
|
---|
| 259 | />
|
---|
| 260 | ) : (
|
---|
| 261 | active === "orders" ? (
|
---|
| 262 | <></>
|
---|
| 263 | ) : (
|
---|
| 264 | active === "orderreviews" ? (
|
---|
| 265 | <AdminOrderReviewCard
|
---|
| 266 | key={index}
|
---|
| 267 | data={cardData}
|
---|
| 268 | onClick={() => openOrderReviewModal(cardData)}
|
---|
| 269 | />
|
---|
| 270 | ) : (
|
---|
| 271 | active === "recipeapplications" ? (
|
---|
| 272 | <AdminRecipeApplicationCard
|
---|
| 273 | key={cardData.id}
|
---|
| 274 | data={cardData}
|
---|
| 275 | onClick={() => openRecipeApplicationModal(cardData)}
|
---|
| 276 | />
|
---|
| 277 | ) : (
|
---|
| 278 | <AdminReviewCard
|
---|
| 279 | key={index}
|
---|
| 280 | data={cardData}
|
---|
| 281 | onClick={() => openReviewModal(cardData)}
|
---|
| 282 | />
|
---|
| 283 | )
|
---|
| 284 | )
|
---|
| 285 | )
|
---|
| 286 | )
|
---|
| 287 | )
|
---|
| 288 | ) : <p className={styles.noDataText}>No data found.</p>
|
---|
| 289 | }
|
---|
| 290 | {
|
---|
| 291 | active === "orders" ? (
|
---|
| 292 | <>
|
---|
| 293 | <div className={styles.orderContainer}>
|
---|
| 294 | <h3 onClick={() => setShowPending(!showPending)} className={styles.toggleSection}>
|
---|
| 295 | {showPending ? "▼" : "▶"} Pending Orders
|
---|
| 296 | </h3>
|
---|
| 297 | {showPending &&
|
---|
| 298 | pending.map((orderData) => (
|
---|
| 299 | <AdminOrderCard
|
---|
| 300 | key={orderData.order.id}
|
---|
| 301 | data={orderData}
|
---|
| 302 | onClick={() => openOrderModal(orderData)}
|
---|
| 303 | />
|
---|
| 304 | ))}
|
---|
| 305 | </div>
|
---|
| 306 | <div className={styles.orderContainer}>
|
---|
| 307 | <h3 onClick={() => setShowAccepted(!showAccepted)} className={styles.toggleSection}>
|
---|
| 308 | {showAccepted ? "▼" : "▶"} Accepted Orders
|
---|
| 309 | </h3>
|
---|
| 310 | {showAccepted &&
|
---|
| 311 | accepted.map((orderData) => (
|
---|
| 312 | <AdminOrderCard
|
---|
| 313 | key={orderData.order.id}
|
---|
| 314 | data={orderData}
|
---|
| 315 | onClick={() => openOrderModal(orderData)}
|
---|
| 316 | />
|
---|
| 317 | ))}
|
---|
| 318 | </div>
|
---|
| 319 | <div className={styles.orderContainer}>
|
---|
| 320 | <h3 onClick={() => setShowFinished(!showFinished)} className={styles.toggleSection}>
|
---|
| 321 | {showFinished ? "▼" : "▶"} Finished Orders
|
---|
| 322 | </h3>
|
---|
| 323 | {showFinished &&
|
---|
| 324 | finished.map((orderData) => (
|
---|
| 325 | <AdminOrderCard
|
---|
| 326 | key={orderData.order.id}
|
---|
| 327 | data={orderData}
|
---|
| 328 | onClick={() => openOrderModal(orderData)}
|
---|
| 329 | />
|
---|
| 330 | ))}
|
---|
| 331 | </div>
|
---|
| 332 | </>
|
---|
| 333 | ) : null
|
---|
| 334 | }
|
---|
| 335 | </div>
|
---|
| 336 | {active !== "orders" && <div className={styles.pagination}>
|
---|
| 337 | {page > 0 && (
|
---|
| 338 | <button
|
---|
| 339 | onClick={() => handlePageChange(page - 1)}
|
---|
| 340 | className={styles.pageButton}
|
---|
| 341 | >
|
---|
| 342 | ←
|
---|
| 343 | </button>
|
---|
| 344 | )}
|
---|
| 345 | <span className={styles.currentPage}>
|
---|
| 346 | Page {page + 1} of {totalPages}
|
---|
| 347 | </span>
|
---|
| 348 | {page < totalPages - 1 && (
|
---|
| 349 | <button
|
---|
| 350 | onClick={() => handlePageChange(page + 1)}
|
---|
| 351 | className={styles.pageButton}
|
---|
| 352 | >
|
---|
| 353 | →
|
---|
| 354 | </button>
|
---|
| 355 | )}
|
---|
| 356 | </div>}
|
---|
| 357 |
|
---|
| 358 | <ReviewModal
|
---|
| 359 | isOpen={selectedReview !== null}
|
---|
| 360 | reviewData={selectedReview}
|
---|
| 361 | onClose={closeModals}
|
---|
| 362 | onRemove={handleRemoveReview}
|
---|
| 363 | />
|
---|
| 364 | <ApplicationModal
|
---|
| 365 | isOpen={selectedApplication !== null}
|
---|
| 366 | applicationData={selectedApplication}
|
---|
| 367 | onClose={closeModals}
|
---|
| 368 | setReload={setReload}
|
---|
| 369 | />
|
---|
| 370 | <AdminOrderModal
|
---|
| 371 | isOpen={selectedOrder !== null}
|
---|
| 372 | orderData={selectedOrder}
|
---|
| 373 | onClose={closeModals}
|
---|
| 374 | />
|
---|
| 375 | <AdminOrderReviewModal
|
---|
| 376 | isOpen={selectedOrderReview !== null}
|
---|
| 377 | orderReviewData={selectedOrderReview}
|
---|
| 378 | onClose={closeModals}
|
---|
| 379 | onRemove={handleRemoveOrderReview}
|
---|
| 380 | />
|
---|
| 381 | <AdminRecipeApplicationModal
|
---|
| 382 | isOpen={selectedRecipeApplication !== null}
|
---|
| 383 | recipeApplicationData={selectedRecipeApplication}
|
---|
| 384 | onClose={closeModals}
|
---|
| 385 | setReload={setReload}
|
---|
| 386 | />
|
---|
| 387 | </div>
|
---|
| 388 | </>
|
---|
| 389 | );
|
---|
| 390 | };
|
---|
| 391 |
|
---|
| 392 | export default AdminPanel;
|
---|