Skip to content

Commit

Permalink
chart updated
Browse files Browse the repository at this point in the history
  • Loading branch information
thakiyudheen committed Jul 16, 2024
1 parent e135b44 commit aac0f70
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
45 changes: 42 additions & 3 deletions src/Components/admin/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const Dashboard = () => {
const [ payment,setPayment]=useState<any>([])
const [isLoading ,setLoading]= useState<boolean>(false)
const dispatch = useAppDispatch()
const [chartPayments, setChartPayments] = useState([]);
// useEffect(() => {

// const getData = async () => {
Expand Down Expand Up @@ -78,6 +79,7 @@ const Dashboard = () => {
console.log('Enrolled courses data:', responseEnrolledCourses.payload.data);
console.log('Students data:', responseStudents);

// Assuming `data` is defined elsewhere and contains `profit`
setProfit(data?.data.profit);

if (responseInstructors.payload && responseInstructors.payload.success) {
Expand All @@ -97,20 +99,57 @@ const Dashboard = () => {
}

if (responsePayments.payload && responsePayments.payload.success) {
setPayment(responsePayments.payload.data);
}
const paymentsData = responsePayments.payload.data;

// Filter payments for this week
const today = new Date();
const startOfWeek = new Date(today);
startOfWeek.setDate(today.getDate() - today.getDay()); // Start of current week (Sunday)
const endOfWeek = new Date(today);
endOfWeek.setDate(startOfWeek.getDate() + 6); // End of current week (Saturday)

const thisWeekPayments = paymentsData.filter((payment: any) => {
const paymentDate = new Date(payment.date); // Assuming payment.date is a date string
return paymentDate >= startOfWeek && paymentDate <= endOfWeek;
});

// Prepare data for chart (amount per date)
const amountsByDate: any = {};
thisWeekPayments.forEach((payment: any) => {
const paymentDate = new Date(payment.date).toISOString().split('T')[0]; // Extract YYYY-MM-DD
if (!amountsByDate[paymentDate]) {
amountsByDate[paymentDate] = 0;
}
amountsByDate[paymentDate] += payment.amount;
});

// Format data for chart (array of objects with date and amount)
const chartData: any = Object.keys(amountsByDate).map(date => ({
date,
amount: amountsByDate[date]
}));

console.log('This week payments:', thisWeekPayments);
console.log('Amounts by date:', amountsByDate);
console.log('Chart data:', chartData);

// Update state with the chart data
setChartPayments(chartData);
console.log('this payment data for week',chartPayments )
}
setLoading(false);

} catch (error) {
console.error('Error fetching data:', error);
setLoading(false);
// Handle error if needed
}
}
};

getData();

}, [dispatch]);

const formatDate = (dateString : any ) => {
const options :any = { year: 'numeric', month: 'long', day: 'numeric' };
return new Date(dateString).toLocaleDateString(undefined, options);
Expand Down
6 changes: 3 additions & 3 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
}

.dark {
/* --background: var(--background-dark);
--foreground: 210 40% 98%; */
--background: 200 9% 9%;
--background: var(--background-dark);
--foreground: 210 40% 98%;
/* --background: 200 9% 9%;
--foreground: 210 40% 98%; */


--card: 222.2 84% 4.9%;
Expand Down

0 comments on commit aac0f70

Please sign in to comment.