Skip to content

Commit

Permalink
count set
Browse files Browse the repository at this point in the history
  • Loading branch information
lksnjw committed Sep 29, 2024
1 parent 9952309 commit 15c88d3
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 115 deletions.
19 changes: 18 additions & 1 deletion backend/controllers/DLDeliveryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,21 @@ export const getDeliveryById = asyncHandler(async (req, res) => {
res.status(404);
throw new Error('Delivery not found');
}
});
});



// Get total deliveries count
export const getTotalDeliveries = asyncHandler(async (req, res) => {
const totalDeliveries = await DLDelivery.countDocuments(); // Count all documents
res.json({ count: totalDeliveries });
});

// Get ongoing deliveries count
export const getOngoingDeliveries = asyncHandler(async (req, res) => {
const ongoingDeliveries = await DLDelivery.countDocuments({
deliveryStatus: { $ne: 'Delivered' } // Count where status is not "Delivered"
});
res.json({ count: ongoingDeliveries });
});

19 changes: 19 additions & 0 deletions backend/controllers/DLDriverController.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,25 @@ const verifyPassword = async (req, res) => {
}
};




// Example: Endpoint to get total drivers count
const getDriversCount = asyncHandler(async (req, res) => {
const count = await DLDriver.countDocuments({});
res.json({ count });
});

// Example: Endpoint to get available drivers count
const getAvailableDriversCount = asyncHandler(async (req, res) => {
const count = await DLDriver.countDocuments({ isAvailable: true });
res.json({ count });
});

export { getDriversCount, getAvailableDriversCount };



export { addDriver,
deleteDriverAccount,
updateDriverPassword,
Expand Down
10 changes: 9 additions & 1 deletion backend/routes/DLDeliveryRoute.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import { getAllDeliveries ,getDeliveryById} from '../controllers/DLDeliveryController.js';
import { getAllDeliveries ,getDeliveryById,getTotalDeliveries,getOngoingDeliveries} from '../controllers/DLDeliveryController.js';

const router = express.Router();

Expand All @@ -11,4 +11,12 @@ router.get('/deliveries', getAllDeliveries);
// Route to get a single delivery by ID
router.get('/d/:id', getDeliveryById);



// Route to get the total deliveries count
router.get('/total/count', getTotalDeliveries);

// Route to get the ongoing deliveries count
router.get('/ongoing/count', getOngoingDeliveries);

export default router;
7 changes: 7 additions & 0 deletions backend/routes/DLDriverRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
deleteDriverAccount,
getAllDrivers,
verifyPassword,
getDriversCount,
getAvailableDriversCount,

// Add logout driver function
} from '../controllers/DLDriverController.js';
Expand Down Expand Up @@ -60,6 +62,11 @@ router.delete('/verifyPass', protectDriver, verifyPassword);

router.get('/drivers', getAllDrivers);

// Route to get the total number of drivers
router.get('/count', getDriversCount);

// Route to get the number of available drivers
router.get('/available/count', getAvailableDriversCount);



Expand Down
175 changes: 62 additions & 113 deletions frontend/src/Pages/delivery/DLmangeDash.jsx
Original file line number Diff line number Diff line change
@@ -1,146 +1,95 @@
import React, { useState, useEffect } from 'react';
import DLmanageSidebar from '../../Components/delivery/DLmanageSidebar';
import axios from '../../axios'; // Update the axios path as per your project structure
import axios from 'axios'; // Ensure this path is correct
import DLmanageSidebar from '../../Components/delivery/DLmanageSidebar'; // Sidebar component

const DLmanageDash = () => {
const [stats, setStats] = useState({
totalDrivers: 0,
availableDrivers: 0,
ongoingOrders: 0,
totalDeliveries: 0,
ongoingDeliveries: 0,
pendingOrders: 0,
});

// Fetch the counts for total drivers, available drivers, ongoing orders, and pending orders
const [loading, setLoading] = useState(true); // Loading state
const [error, setError] = useState(null); // Error state

// Fetch statistics when the component mounts
useEffect(() => {
const fetchStats = async () => {
try {
// Fetch total drivers count
const totalDriversRes = await axios.get('/drivers/count');
const availableDriversRes = await axios.get('/drivers/available/count');
const ongoingOrdersRes = await axios.get('/orders/ongoing/count');
const pendingOrdersRes = await axios.get('/orders/pending/count');
const totalDriversRes = await axios.get('/api/drivers/count');
// Fetch available drivers count
const availableDriversRes = await axios.get('/api/drivers/available/count');
// Fetch total deliveries count
const totalDeliveriesRes = await axios.get('/api/delivery/total/count');
// Fetch ongoing deliveries count (where status != "Delivered")
const ongoingDeliveriesRes = await axios.get('/api/delivery/ongoing/count');

// Update the stats state with the fetched data
setStats({
totalDrivers: totalDriversRes.data.count,
availableDrivers: availableDriversRes.data.count,
ongoingOrders: ongoingOrdersRes.data.count,
pendingOrders: pendingOrdersRes.data.count,
totalDeliveries: totalDeliveriesRes.data.count,
ongoingDeliveries: ongoingDeliveriesRes.data.count,
});
} catch (error) {
console.error('Error fetching statistics:', error);
setError('Failed to fetch statistics'); // Set error message
} finally {
setLoading(false); // Set loading to false
}
};

fetchStats();
}, []);

return (
if (loading) {
return <div>Loading...</div>; // Display a loading message while fetching data
}


if (error) {
return <div className="text-red-500">{error}</div>; // Display error message if any
}

return (
<div className="flex min-h-screen bg-gray-50">
{/* Sidebar */}
<aside className="fixed top-0 left-0 bottom-0 w-64 bg-gray-50 shadow-md pl-8 pt-16 mt-16">
<DLmanageSidebar />
</aside>

{/* Main content */}
<main className="flex-1 ml-64 p-16 overflow-y-auto">
<div className="max-w-7xl mx-auto p-6 bg-white shadow-md rounded-md">
<h1 className="text-3xl font-bold mb-8 text-gray-800">Delivery Manager Dashboard</h1>

{/* Statistics Section */}
<div className="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
<div className="bg-green-100 p-6 rounded-lg shadow-md">
<h2 className="text-lg font-semibold text-green-800">Total Drivers</h2>
<p className="text-4xl font-bold text-green-800 mt-4">{stats.totalDrivers}</p>
</div>
<div className="bg-blue-100 p-6 rounded-lg shadow-md">
<h2 className="text-lg font-semibold text-blue-800">Available Drivers</h2>
<p className="text-4xl font-bold text-blue-800 mt-4">{stats.availableDrivers}</p>
</div>
<div className="bg-yellow-100 p-6 rounded-lg shadow-md">
<h2 className="text-lg font-semibold text-yellow-800">Ongoing Orders</h2>
<p className="text-4xl font-bold text-yellow-800 mt-4">{stats.ongoingOrders}</p>
</div>
<div className="bg-red-100 p-6 rounded-lg shadow-md">
<h2 className="text-lg font-semibold text-red-800">Pending Orders</h2>
<p className="text-4xl font-bold text-red-800 mt-4">{stats.pendingOrders}</p>
</div>
</div>

{/* Recent Driver Requests */}
<div className="bg-gray-100 p-6 rounded-lg shadow-md mb-8">
<h2 className="text-2xl font-bold mb-4">Recent Driver Requests</h2>
<div className="overflow-x-auto">
<table className="min-w-full bg-white border border-gray-200">
<thead>
<tr className="bg-gray-200">
<th className="px-4 py-2 text-left border">Driver Name</th>
<th className="px-4 py-2 text-left border">Vehicle Type</th>
<th className="px-4 py-2 text-left border">Request Date</th>
<th className="px-4 py-2 text-left border">Status</th>
</tr>
</thead>
<tbody>
<tr className="bg-white">
<td className="px-4 py-2 border">Nimendra</td>
<td className="px-4 py-2 border">Bike</td>
<td className="px-4 py-2 border">12 Sep, 2023</td>
<td className="px-4 py-2 border">
<span className="bg-yellow-500 text-white px-2 py-1 rounded">Pending</span>
</td>
</tr>
<tr className="bg-white">
<td className="px-4 py-2 border">Sanjeewa</td>
<td className="px-4 py-2 border">Lorry</td>
<td className="px-4 py-2 border">10 Sep, 2023</td>
<td className="px-4 py-2 border">
<span className="bg-green-500 text-white px-2 py-1 rounded">Approved</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>

{/* Ongoing Deliveries Section */}
<div className="bg-gray-100 p-6 rounded-lg shadow-md">
<h2 className="text-2xl font-bold mb-4">Ongoing Deliveries</h2>
<div className="overflow-x-auto">
<table className="min-w-full bg-white border border-gray-200">
<thead>
<tr className="bg-gray-200">
<th className="px-4 py-2 text-left border">Delivery ID</th>
<th className="px-4 py-2 text-left border">Driver Name</th>
<th className="px-4 py-2 text-left border">Vehicle Type</th>
<th className="px-4 py-2 text-left border">Status</th>
</tr>
</thead>
<tbody>
<tr className="bg-white">
<td className="px-4 py-2 border">#1234</td>
<td className="px-4 py-2 border">Dasun</td>
<td className="px-4 py-2 border">Bike</td>
<td className="px-4 py-2 border">
<span className="bg-blue-500 text-white px-2 py-1 rounded">In Progress</span>
</td>
</tr>
<tr className="bg-white">
<td className="px-4 py-2 border">#5678</td>
<td className="px-4 py-2 border">Ishara</td>
<td className="px-4 py-2 border">Lorry</td>
<td className="px-4 py-2 border">
<span className="bg-green-500 text-white px-2 py-1 rounded">Completed</span>
</td>
</tr>
</tbody>
</table>
{/* Sidebar */}
<aside className="fixed top-0 left-0 bottom-0 w-64 bg-gray-50 shadow-md pl-8 pt-16 mt-16">
<DLmanageSidebar />
</aside>

{/* Main content */}
<main className="flex-1 ml-64 p-16 overflow-y-auto">
<div className="max-w-7xl mx-auto p-6 bg-white shadow-md rounded-md">
<h1 className="text-3xl font-bold mb-8 text-gray-800">Delivery Manager Dashboard</h1>

{/* Statistics Section */}
<div className="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
<div className="bg-green-100 p-6 rounded-lg shadow-md">
<h2 className="text-lg font-semibold text-green-800">Total Drivers</h2>
<p className="text-4xl font-bold text-green-800 mt-4">{stats.totalDrivers}</p>
</div>
<div className="bg-blue-100 p-6 rounded-lg shadow-md">
<h2 className="text-lg font-semibold text-blue-800">Available Drivers</h2>
<p className="text-4xl font-bold text-blue-800 mt-4">{stats.availableDrivers}</p>
</div>
<div className="bg-yellow-100 p-6 rounded-lg shadow-md">
<h2 className="text-lg font-semibold text-yellow-800">Ongoing Deliveries</h2>
<p className="text-4xl font-bold text-yellow-800 mt-4">{stats.ongoingDeliveries}</p>
</div>
<div className="bg-red-100 p-6 rounded-lg shadow-md">
<h2 className="text-lg font-semibold text-red-800">Total Deliveries</h2>
<p className="text-4xl font-bold text-red-800 mt-4">{stats.totalDeliveries}</p>
</div>
</div>

{/* You can add additional sections here */}

</div>
</div>
</main>
</div>

</main>
</div>
);
};

Expand Down

0 comments on commit 15c88d3

Please sign in to comment.