Skip to content

Commit

Permalink
Refactor code to start using custom hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed-Ramadan1 committed May 28, 2024
1 parent 929e68e commit 38d357a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 53 deletions.
9 changes: 6 additions & 3 deletions src/hooks/useFetchData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const useFetchData = (endpoint, options = {}) => {
params = {},
headers = {},
initialData = [],
requestedData = "",
} = options;

const [data, setData] = useState(initialData);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
Expand All @@ -17,9 +19,10 @@ const useFetchData = (endpoint, options = {}) => {
try {
setLoading(true);
const response = await customFetch[method](endpoint, { headers, params });
if (response.data.data) {
setData(response.data.data.usersNotes);
setIsMorePages(response.data.data.usersNotes.length === params.limit);
const responseData = response.data.data[requestedData];
if (responseData) {
setData(responseData);
setIsMorePages(responseData.length === params.limit);
} else {
setData([]);
setIsMorePages(false);
Expand Down
66 changes: 24 additions & 42 deletions src/pages/userProfile/MyEnrolledCourses.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { customFetch } from "../../utils/customFetch";
import {
EnrolledCourseCard,
Pagination,
Expand All @@ -9,62 +8,46 @@ import {
LoadingSpinner,
ProfilePageContainer,
} from "../../components";
import { Link } from "react-router-dom";
import useFetchData from "../../hooks/useFetchData";

const MyEnrolledCourses = () => {
const { token } = useSelector((state) => state.userReducers);
const [courses, setCourses] = useState([]);
const [isMorePages, setIsMorePages] = useState(false);
const [itemsPerPage, setItemsPerPage] = useState(6);
const [currentPage, setCurrentPage] = useState(1);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);

useEffect(() => {
const fetchMyCourses = async () => {
try {
setLoading(true);
const response = await customFetch.get("users/me/courses", {
headers: {
Authorization: `Bearer ${token}`,
},
params: {
page: currentPage,
limit: itemsPerPage,
},
});
const { data, loading, error, isMorePages, fetchData } = useFetchData(
"users/me/courses",
{
headers: {
Authorization: `Bearer ${token}`,
},
params: {
page: currentPage,
limit: itemsPerPage,
sort: "-createdAt",
},
requestedData: "courses",
}
);

// setCourses(response.data.data.courses);
if (response.data.data.courses) {
setCourses(response.data.data.courses);
setIsMorePages(response.data.data.courses.length === itemsPerPage);
} else {
setCourses([]);
setIsMorePages(false);
setLoading(false);
}
} catch (error) {
setLoading(false);
setError(error.response.data.message);
}
};
fetchMyCourses();
setLoading(false);
useEffect(() => {
fetchData();
}, [currentPage, itemsPerPage, token]);

console.log(data);
return (
<>
<ProfilePageContainer>
{courses.length > 0 && (
{data.length > 0 && (
<div className="grid w-full sm:grid-cols-3 sm:gap-4 md:grid-cols-3 ">
{courses &&
courses.map((course) => (
{data &&
data.map((course) => (
<EnrolledCourseCard course={course} key={course._id} />
))}
</div>
)}
{/* No courses display centerd text */}
{courses.length === 0 && (

{data.length === 0 && !loading && (
<EmptyItems
headerText={"No courses enrolled yet"}
linkText={" Start Explore"}
Expand All @@ -73,10 +56,9 @@ const MyEnrolledCourses = () => {
)}

{/* Loading spinner */}

{loading && <LoadingSpinner />}

{error && !error && <ErrorMessage errorMessage={error} />}
{error && !loading && <ErrorMessage errorMessage={error} />}
</ProfilePageContainer>
<div className="flex items-center justify-center w-full">
<div className="flex justify-end w-[50%]">
Expand Down
6 changes: 3 additions & 3 deletions src/store/adminSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import { cleareStatus } from "./authHandler";

// const userUrl = "http://localhost:3000/api/v1/admin";
const userUrl = "http://localhost:3000/api/v1/admin";

const userUrl =
"https://e-learning-backend-application.onrender.com/api/v1/admin";
// const userUrl =
// "https://e-learning-backend-application.onrender.com/api/v1/admin";

const initialState = {
loading: false,
Expand Down
8 changes: 4 additions & 4 deletions src/store/userSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { toast } from "react-toastify";
import { cleareStatus } from "./authHandler";

//Bas URL
// const authUrl = `http://localhost:3000/api/v1/auth`;
// const userUrl = `http://localhost:3000/api/v1/users`;
const authUrl = `http://localhost:3000/api/v1/auth`;
const userUrl = `http://localhost:3000/api/v1/users`;

const authUrl = `https://e-learning-backend-application.onrender.com/api/v1/auth`;
const userUrl = `https://e-learning-backend-application.onrender.com/api/v1/users`;
// const authUrl = `https://e-learning-backend-application.onrender.com/api/v1/auth`;
// const userUrl = `https://e-learning-backend-application.onrender.com/api/v1/users`;

//SignUp new user to the application
export const signUp = createAsyncThunk(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/customFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const productionUrl =
"https://e-learning-backend-application.onrender.com/api/v1";

export const customFetch = axios.create({
baseURL: productionUrl,
baseURL: devURL,
});

0 comments on commit 38d357a

Please sign in to comment.