228 lines
6.1 KiB
JavaScript
228 lines
6.1 KiB
JavaScript
import { useState, useEffect } from "react";
|
|
import GlobalRepository from "../repositories/GlobalRepository";
|
|
import { keepPreviousData, useQuery } from "@tanstack/react-query";
|
|
|
|
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 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 };
|
|
};
|
|
|
|
// -------------------Query----------------------------
|
|
|
|
// export const useDashboard_Data = (days, FromDate, projectId)=>{
|
|
// return useQuery({
|
|
// queryKey:["dashboardProjectProgress"],
|
|
// queryFn:async()=> {
|
|
// const payload = {
|
|
// days,
|
|
// FromDate: FromDate || '',
|
|
// projectId: projectId || null,
|
|
// };
|
|
// const resp = await GlobalRepository.getDashboardProgressionData(payload);
|
|
// return resp.data;
|
|
// }
|
|
// })
|
|
// }
|
|
export const useProjectCompletionStatus = () => {
|
|
return useQuery({
|
|
queryKey: ["projectCompletionStatus"],
|
|
queryFn: async () => {
|
|
const resp = await await GlobalRepository.getProjectCompletionStatus();
|
|
return resp.data;
|
|
},
|
|
});
|
|
};
|
|
export const useDashboard_AttendanceData = (date, projectId) => {
|
|
return useQuery({
|
|
queryKey: ["dashboardAttendances", date, projectId],
|
|
queryFn: async () => {
|
|
const resp = await await GlobalRepository.getDashboardAttendanceData(
|
|
date,
|
|
projectId
|
|
);
|
|
return resp.data;
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useDashboardTeamsCardData = (projectId) => {
|
|
return useQuery({
|
|
queryKey: ["dashboardTeams", projectId],
|
|
queryFn: async () => {
|
|
const resp = await GlobalRepository.getDashboardTeamsCardData(projectId);
|
|
return resp.data;
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useDashboardTasksCardData = (projectId) => {
|
|
return useQuery({
|
|
queryKey: ["dashboardTasks", projectId],
|
|
queryFn: async () => {
|
|
const resp = await GlobalRepository.getDashboardTasksCardData(projectId);
|
|
return resp.data;
|
|
},
|
|
});
|
|
};
|
|
// export const useAttendanceOverviewData = (projectId, days) => {
|
|
// return useQuery({
|
|
// queryKey:["dashboardAttendanceOverView",projectId],
|
|
// queryFn:async()=> {
|
|
|
|
// const resp = await GlobalRepository.getAttendanceOverview(projectId, days);
|
|
// return resp.data;
|
|
// }
|
|
// })
|
|
// }
|
|
|
|
export const useDashboardProjectsCardData = () => {
|
|
return useQuery({
|
|
queryKey: ["dashboardProjects"],
|
|
queryFn: async () => {
|
|
const resp = await GlobalRepository.getDashboardProjectsCardData();
|
|
return resp.data;
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useExpenseAnalysis = (projectId, startDate, endDate) => {
|
|
const hasBothDates = !!startDate && !!endDate;
|
|
const noDatesSelected = !startDate && !endDate;
|
|
|
|
const shouldFetch = noDatesSelected || hasBothDates;
|
|
return useQuery({
|
|
queryKey: ["expenseAnalysis", projectId, startDate, endDate],
|
|
queryFn: async () => {
|
|
const resp = await GlobalRepository.getExpenseData(
|
|
projectId,
|
|
startDate,
|
|
endDate
|
|
);
|
|
return resp.data;
|
|
},
|
|
enabled: shouldFetch,
|
|
refetchOnWindowFocus: true, // refetch when you come back
|
|
refetchOnMount: "always", // always refetch on remount
|
|
staleTime: 0,
|
|
});
|
|
};
|
|
|
|
export const useExpenseStatus = (projectId) => {
|
|
return useQuery({
|
|
queryKey: ["expense_stauts", projectId],
|
|
queryFn: async () => {
|
|
const resp = await GlobalRepository.getExpenseStatus(projectId);
|
|
return resp.data;
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useExpenseDataByProject = (projectId, categoryId, months) => {
|
|
return useQuery({
|
|
queryKey: ["expenseByProject", projectId, categoryId, months],
|
|
queryFn: async () => {
|
|
const resp = await GlobalRepository.getExpenseDataByProject(
|
|
projectId,
|
|
categoryId,
|
|
months
|
|
);
|
|
return resp.data;
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useGetCollectionOverview = (projectId) => {
|
|
return useQuery({
|
|
queryKey: ["collection_overview", projectId],
|
|
queryFn: async () => {
|
|
const resp = await GlobalRepository.getCollectionOverview(projectId);
|
|
return resp.data;
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useJobsProgression = (projectId) => {
|
|
return useQuery({
|
|
queryKey: ["serviceProjectJobs", projectId],
|
|
queryFn: async () => {
|
|
const resp = await GlobalRepository.getJobsProgression(projectId);
|
|
return resp.data;
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useAttendaceProjectWiseOveriew = (date) => {
|
|
return useQuery({
|
|
queryKey: ["attendaceOverview", date],
|
|
queryFn: async () => {
|
|
const resp = await GlobalRepository.getAttendanceProjectWiseOverview(
|
|
date
|
|
);
|
|
return resp.data;
|
|
},
|
|
keepPreviousData: true,
|
|
});
|
|
};
|