diff --git a/src/components/dashboard/shard/Error.jsx b/src/components/dashboard/shard/Error.jsx new file mode 100644 index 0000000..df94d50 --- /dev/null +++ b/src/components/dashboard/shard/Error.jsx @@ -0,0 +1,11 @@ +const Error = ({ errorMessage }) => { + return ( + + + {errorMessage} + + + ); +}; + +export default Error; diff --git a/src/components/dashboard/shard/LoadingWhile.jsx b/src/components/dashboard/shard/LoadingWhile.jsx new file mode 100644 index 0000000..f72cc3f --- /dev/null +++ b/src/components/dashboard/shard/LoadingWhile.jsx @@ -0,0 +1,12 @@ +import LoadingSpinner from "../../userProfile/shard/LoadingSpinner"; +const LoadingWhile = () => { + return ( + + + + + + ); +}; + +export default LoadingWhile; diff --git a/src/components/dashboard/shard/NotFoundData.jsx b/src/components/dashboard/shard/NotFoundData.jsx new file mode 100644 index 0000000..338d39d --- /dev/null +++ b/src/components/dashboard/shard/NotFoundData.jsx @@ -0,0 +1,13 @@ +import React from "react"; + +const NotFoundData = ({message}) => { + return ( + + + {message} + + + ); +}; + +export default NotFoundData; diff --git a/src/components/index.js b/src/components/index.js index 2116a2e..21e4e06 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -91,6 +91,9 @@ export { default as StatsContainer } from "./dashboard/StatsContainer"; export { default as NavigationLink } from "./dashboard/shard/NavigationLink"; export { default as NavBarLink } from "./dashboard/shard/NavBarLink"; +export { default as LoadingWhile } from "./dashboard/shard/LoadingWhile"; +export { default as Error } from "./dashboard/shard/Error"; +export { default as NotFoundData } from "./dashboard/shard/NotFoundData"; //Payment export { default as OrderDetails } from "./payment/OrderDetails"; diff --git a/src/hooks/usePostData.js b/src/hooks/usePostData.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/pages/dashboard/Dashboard.jsx b/src/pages/dashboard/Dashboard.jsx index 60d6508..34922e1 100644 --- a/src/pages/dashboard/Dashboard.jsx +++ b/src/pages/dashboard/Dashboard.jsx @@ -3,98 +3,85 @@ import { useState, useEffect } from "react"; import { customFetch } from "../../utils/customFetch"; import { PageIntro, StatsBox, StatsContainer } from "../../components"; import SyncLoader from "react-spinners/SyncLoader"; +import { LoadingSpinner } from "../../components"; + +import useFetchData from "../../hooks/useFetchData"; function Dashboard() { const { token } = useSelector((state) => state.userReducers); - const [siteStates, setSiteStates] = useState(null); - const [loading, setLoading] = useState(false); - const [error, setError] = useState(null); - - useEffect(() => { - const fetchData = async () => { - try { - setLoading(true); - const res = await customFetch.get("/admin/siteStatics", { - headers: { - Authorization: `Bearer ${token}`, - }, - }); + const { data, loading, error, fetchData } = useFetchData( + "admin/siteStatics", + { + headers: { + Authorization: `Bearer ${token}`, + }, - setSiteStates(res.data.data); - setLoading(false); - } catch (error) { - setLoading(false); - setError(error); - } - }; + requestedData: "statics", + } + ); + useEffect(() => { fetchData(); }, [token]); + console.log(data); return ( <>
- {loading && ( -
- -
- )} + {loading && !error && } {!loading && error &&

Error: {error.message}

} - {siteStates && !loading && ( + {data && !loading && ( - + diff --git a/src/pages/dashboard/FreeCourses.jsx b/src/pages/dashboard/FreeCourses.jsx index 8f69f6f..d93ad07 100644 --- a/src/pages/dashboard/FreeCourses.jsx +++ b/src/pages/dashboard/FreeCourses.jsx @@ -1,6 +1,5 @@ import { useState, useEffect } from "react"; import { useSelector } from "react-redux"; -import { customFetch } from "../../utils/customFetch"; import { PageIntro, @@ -8,59 +7,34 @@ import { CourseElement, CoursesHeader, Pagination, + LoadingWhile, + Error, + NotFoundData, } from "../../components"; +import useFetchData from "../../hooks/useFetchData"; function FreeCourses() { const { token } = useSelector((state) => state.userReducers); - const [courses, setCourses] = useState([]); - const [loading, setLoading] = useState(true); - - const [isMorePages, setIsMorePages] = useState(false); - const [itemsPerPage, setItemsPerPage] = useState(15); + const [itemsPerPage, setItemsPerPage] = useState(6); const [currentPage, setCurrentPage] = useState(1); - + const [isChanged, setIsChanged] = useState(false); + const { data, loading, error, isMorePages, fetchData } = useFetchData( + "/admin/freeCourses", + { + headers: { + Authorization: `Bearer ${token}`, + }, + params: { + page: currentPage, + limit: itemsPerPage, + sort: "-createdAt", + }, + requestedData: "courses", + } + ); useEffect(() => { - const fetchData = async () => { - try { - const res = await customFetch.get("/admin/freeCourses", { - headers: { - Authorization: `Bearer ${token}`, - }, - params: { - page: currentPage, - limit: itemsPerPage, - }, - }); - - if (res.data.data.courses) { - setCourses(res.data.data.courses); - setIsMorePages(res.data.data.courses.length === itemsPerPage); - } else { - setCourses([]); - setIsMorePages(false); - } - - setLoading(false); - } catch (error) { - console.log(error); - setLoading(false); - } - }; - fetchData(); - }, [token, currentPage, itemsPerPage]); - - const prevPage = () => { - if (currentPage > 1) { - setCurrentPage((prev) => prev - 1); - } - }; - - const nextPage = () => { - if (isMorePages) { - setCurrentPage((prev) => prev + 1); - } - }; + }, [token, currentPage, itemsPerPage, isChanged]); return (
@@ -70,37 +44,30 @@ function FreeCourses() { {/* table manage table courses table */} - {loading && ( - - - Loading........ - - - )} + {loading && } + {!loading && - courses.length > 0 && - courses.map((course, index) => ( + data.length > 0 && + data.map((course, index) => ( ))} - {!loading && courses.length === 0 && ( - - - No Courses Found - - + {!loading && data.length === 0 && ( + )} + {error && !loading && } setCurrentPage((prev) => prev - 1)} + onNextPage={() => setCurrentPage((prev) => prev + 1)} />
); diff --git a/src/pages/dashboard/PaiedCourses.jsx b/src/pages/dashboard/PaiedCourses.jsx index a3c8b36..2fab3f3 100644 --- a/src/pages/dashboard/PaiedCourses.jsx +++ b/src/pages/dashboard/PaiedCourses.jsx @@ -1,6 +1,5 @@ import { useState, useEffect } from "react"; import { useSelector } from "react-redux"; -import { customFetch } from "../../utils/customFetch"; import { PageIntro, @@ -8,55 +7,35 @@ import { CourseElement, CoursesHeader, Pagination, + LoadingWhile, + Error, + NotFoundData, } from "../../components"; +import useFetchData from "../../hooks/useFetchData"; function PaiedCourses() { const { token } = useSelector((state) => state.userReducers); - const [courses, setCourses] = useState(null); - const [isChanged, setIsChanged] = useState(false); - - const [isMorePages, setIsMorePages] = useState(false); - const [itemsPerPage, setItemsPerPage] = useState(15); + const [itemsPerPage, setItemsPerPage] = useState(6); const [currentPage, setCurrentPage] = useState(1); + const [isChanged, setIsChanged] = useState(false); + const { data, loading, error, isMorePages, fetchData } = useFetchData( + "/admin/paidCourses", + { + headers: { + Authorization: `Bearer ${token}`, + }, + params: { + page: currentPage, + limit: itemsPerPage, + sort: "-createdAt", + }, + requestedData: "courses", + } + ); useEffect(() => { - const fetchData = async () => { - setIsChanged(false); - try { - const response = await customFetch.get("/admin/paidCourses", { - headers: { - Authorization: `Bearer ${token}`, - }, - params: { - page: currentPage, - limit: itemsPerPage, - }, - }); - - if (response.data.data.courses) { - setCourses(response.data.data.courses); - setIsMorePages(true); - } - if (response.data.data.courses.length === 0) setIsMorePages(false); - } catch (error) { - console.log(error); - } - }; - fetchData(); - }, [token, isChanged, currentPage, itemsPerPage]); - - const prevPage = () => { - if (currentPage > 1) { - setCurrentPage((prev) => prev - 1); - } - }; - - const nextPage = () => { - if (currentPage) { - setCurrentPage((prev) => prev + 1); - } - }; + }, [token, currentPage, itemsPerPage, isChanged]); return (
@@ -64,8 +43,8 @@ function PaiedCourses() { {/* table manage table courses table */} - {courses && - courses.map((course, index) => ( + {data && + data.map((course, index) => ( ))} - {/* handel no courses case */} - {courses && courses.length === 0 && ( - - - No Courses Found - - + {loading && } + + {!loading && data.length === 0 && ( + )} + + {error && !loading && } - {courses && ( + {data && ( setCurrentPage((prev) => prev - 1)} + onNextPage={() => setCurrentPage((prev) => prev + 1)} /> )}
diff --git a/src/pages/userProfile/Wishlist.jsx b/src/pages/userProfile/Wishlist.jsx index 54d1258..2c1f39a 100644 --- a/src/pages/userProfile/Wishlist.jsx +++ b/src/pages/userProfile/Wishlist.jsx @@ -1,7 +1,6 @@ import { useEffect, useState } from "react"; import { useSelector } from "react-redux"; -import { customFetch } from "../../utils/customFetch"; -import { Link } from "react-router-dom"; + import { Pagination, WishlistCard, @@ -10,58 +9,38 @@ import { LoadingSpinner, ProfilePageContainer, } from "../../components"; - +import useFetchData from "../../hooks/useFetchData"; const Wishlist = () => { - const [wishlistItems, setWishlistItems] = useState([]); - const [isChanged, setIsChanged] = useState(false); - const [loading, setLoading] = useState(false); - const [error, setError] = useState(null); const { token } = useSelector((state) => state.userReducers); - - const [isMorePages, setIsMorePages] = useState(false); - const [itemsPerPage, setItemsPerPage] = useState(3); + const [itemsPerPage, setItemsPerPage] = useState(6); const [currentPage, setCurrentPage] = useState(1); + const [isChanged, setIsChanged] = useState(false); + const { data, loading, error, isMorePages, fetchData } = useFetchData( + "wishlist", + { + headers: { + Authorization: `Bearer ${token}`, + }, + params: { + page: currentPage, + limit: itemsPerPage, + sort: "-createdAt", + }, + requestedData: "wishlistItems", + } + ); useEffect(() => { setIsChanged(false); - const fetchWishlistItems = async () => { - try { - setLoading(true); - const response = await customFetch.get("wishlist", { - headers: { - Authorization: `Bearer ${token}`, - }, - params: { - page: currentPage, - limit: itemsPerPage, - sort: "-createdAt", - }, - }); - if (response.data.data.wishlistItems) { - setWishlistItems(response.data.data.wishlistItems); - setIsMorePages( - response.data.data.wishlistItems.length === itemsPerPage - ); - } else { - setWishlistItems([]); - setIsMorePages(false); - setLoading(false); - } - } catch (error) { - setLoading(false); - setError(error.response.data.message); - } - }; - fetchWishlistItems(); - setLoading(false); + fetchData(); }, [token, itemsPerPage, currentPage, isChanged]); return ( <> - {wishlistItems.length > 0 && ( + {data.length > 0 && !loading && (
- {wishlistItems && - wishlistItems.map((item) => ( + {data && + data.map((item) => ( { ))}
)} - {wishlistItems.length === 0 && ( + {data.length === 0 && !loading && ( { /> )} - {loading && ( -
-
-
- )} + {loading && } - {error && } + {error && !loading && }