185 lines
4.9 KiB
JavaScript
185 lines
4.9 KiB
JavaScript
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 [isLineChartLoading, 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 || null,
|
|
};
|
|
|
|
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: isLineChartLoading, error };
|
|
};
|
|
|
|
|
|
export const useDashboard_AttendanceData = (date, projectId) => {
|
|
const [dashboard_Attendancedata, setDashboard_AttendanceData] = useState([]);
|
|
const [isLineChartLoading, setLoading] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
useEffect(() => {
|
|
const fetchData = async () => {
|
|
setLoading(true);
|
|
setError("");
|
|
|
|
try {
|
|
const response = await GlobalRepository.getDashboardAttendanceData(date, projectId); // date in 2nd param
|
|
setDashboard_AttendanceData(response.data);
|
|
} catch (err) {
|
|
setError("Failed to fetch dashboard data.");
|
|
console.error(err);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
if (date && projectId !== null) {
|
|
fetchData();
|
|
}
|
|
}, [date, projectId]);
|
|
|
|
return { dashboard_Attendancedata, isLineChartLoading: isLineChartLoading, 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 = (projectId) => {
|
|
const [teamsCardData, setTeamsData] = useState({});
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
useEffect(() => {
|
|
const fetchTeamsData = async () => {
|
|
if (!projectId) return; // ✅ Skip if projectId is not provided
|
|
|
|
setLoading(true);
|
|
setError("");
|
|
|
|
try {
|
|
const response = await GlobalRepository.getDashboardTeamsCardData(projectId);
|
|
setTeamsData(response.data); // ✅ Handle undefined/null
|
|
} catch (err) {
|
|
setError("Failed to fetch teams card data.");
|
|
console.error(err);
|
|
setTeamsData({});
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchTeamsData();
|
|
}, [projectId]);
|
|
|
|
return { teamsCardData, loading, error };
|
|
};
|
|
|
|
export const useDashboardTasksCardData = (projectId) => {
|
|
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(projectId);
|
|
setTasksData(response.data);
|
|
} catch (err) {
|
|
setError("Failed to fetch tasks card data.");
|
|
console.error(err);
|
|
setTasksData({});
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchTasksData();
|
|
}, [projectId]);
|
|
|
|
return { tasksCardData, loading, error };
|
|
};
|
|
|
|
|
|
export const useAttendanceOverviewData = (projectId, days) => {
|
|
const [attendanceOverviewData, setAttendanceOverviewData] = useState([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
useEffect(() => {
|
|
if (!projectId || !days) return;
|
|
const fetchAttendanceOverview = async () => {
|
|
setLoading(true);
|
|
setError("");
|
|
|
|
try {
|
|
const response = await GlobalRepository.getAttendanceOverview(projectId, days);
|
|
setAttendanceOverviewData(response.data);
|
|
} catch (err) {
|
|
setError("Failed to fetch attendance overview data.");
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchAttendanceOverview();
|
|
}, [projectId, days]);
|
|
|
|
return { attendanceOverviewData, loading, error };
|
|
};
|