add dashboard data hooks and global repository for API interactions
This commit is contained in:
parent
f4a09c7f53
commit
51190b0d53
122
src/hooks/useDashboard_Data.jsx
Normal file
122
src/hooks/useDashboard_Data.jsx
Normal file
@ -0,0 +1,122 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import GlobalRepository from "../repositories/GlobalRepository";
|
||||
|
||||
// 🔹 Dashboard Progression Data Hook
|
||||
export const useDashboard_Data = ({ days, FromDate, projectId }) => {
|
||||
const [dashboard_data, setDashboard_Data] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
if (!days) return;
|
||||
|
||||
const fetchData = async () => {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
|
||||
try {
|
||||
const payload = {
|
||||
days,
|
||||
FromDate: FromDate || '',
|
||||
projectId: projectId || 0,
|
||||
};
|
||||
|
||||
const response = await GlobalRepository.getDashboardProgressionData(payload);
|
||||
setDashboard_Data(response.data);
|
||||
} catch (err) {
|
||||
setError("Failed to fetch dashboard data.");
|
||||
console.error(err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchData();
|
||||
}, [days, FromDate, projectId]);
|
||||
|
||||
return { dashboard_data, loading, error };
|
||||
};
|
||||
|
||||
// 🔹 Dashboard Projects Card Data Hook
|
||||
export const useDashboardProjectsCardData = () => {
|
||||
const [projectsCardData, setProjectsData] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
const fetchProjectsData = async () => {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
|
||||
try {
|
||||
const response = await GlobalRepository.getDashboardProjectsCardData();
|
||||
setProjectsData(response.data);
|
||||
} catch (err) {
|
||||
setError("Failed to fetch projects card data.");
|
||||
console.error(err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchProjectsData();
|
||||
}, []);
|
||||
|
||||
return { projectsCardData, loading, error };
|
||||
};
|
||||
|
||||
// 🔹 Dashboard Teams Card Data Hook
|
||||
export const useDashboardTeamsCardData = () => {
|
||||
const [teamsCardData, setTeamsData] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
const fetchTeamsData = async () => {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
|
||||
try {
|
||||
const response = await GlobalRepository.getDashboardTeamsCardData();
|
||||
setTeamsData(response.data);
|
||||
} catch (err) {
|
||||
setError("Failed to fetch teams card data.");
|
||||
console.error(err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchTeamsData();
|
||||
}, []);
|
||||
|
||||
return { teamsCardData, loading, error };
|
||||
};
|
||||
|
||||
// 🔹 Dashboard Tasks Card Data Hook
|
||||
export const useDashboardTasksCardData = () => {
|
||||
const [tasksCardData, setTasksData] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
const fetchTasksData = async () => {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
|
||||
try {
|
||||
const response = await GlobalRepository.getDashboardTasksCardData();
|
||||
setTasksData(response.data);
|
||||
} catch (err) {
|
||||
setError("Failed to fetch tasks card data.");
|
||||
console.error(err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchTasksData();
|
||||
}, []);
|
||||
|
||||
return { tasksCardData, loading, error };
|
||||
};
|
25
src/repositories/GlobalRepository.jsx
Normal file
25
src/repositories/GlobalRepository.jsx
Normal file
@ -0,0 +1,25 @@
|
||||
import { api } from "../utils/axiosClient";
|
||||
|
||||
const GlobalRepository = {
|
||||
getDashboardProgressionData: ({ days = '', FromDate = '', projectId = '' }) => {
|
||||
const params = new URLSearchParams({
|
||||
days: days.toString(),
|
||||
FromDate,
|
||||
projectId,
|
||||
});
|
||||
|
||||
return api.get(`/api/Dashboard/Progression?${params.toString()}`);
|
||||
},
|
||||
getDashboardProjectsCardData: () => {
|
||||
return api.get(`/api/Dashboard/projects`);
|
||||
},
|
||||
getDashboardTeamsCardData: () => {
|
||||
return api.get(`/api/Dashboard/teams`);
|
||||
},
|
||||
getDashboardTasksCardData: () => {
|
||||
return api.get(`/api/Dashboard/tasks`);
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
export default GlobalRepository;
|
Loading…
x
Reference in New Issue
Block a user