Spaces:
Running
Running
File size: 5,269 Bytes
b967c2c 9f63aed b967c2c 9f63aed e74f850 9f63aed e74f850 9f63aed 1bcf29b e74f850 1bcf29b 9f63aed b967c2c 9f63aed b967c2c f0d329b b967c2c e860c42 b967c2c 9f63aed b967c2c 9f63aed b967c2c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
import React, { useState, useEffect } from 'react';
import { Table, TableHeader, TableBody, TableHead, TableRow, TableCell, TableCaption } from './ui/table';
import { Button } from './ui/button';
const DataTable = () => {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [sortConfig, setSortConfig] = useState({ key: null, direction: 'asc' });
// Mock data for demonstration - replace with actual MongoDB data
const mockData = [
{ id: 1, name: 'John Doe', email: 'john@example.com', role: 'Developer' },
{ id: 2, name: 'Jane Smith', email: 'jane@example.com', role: 'Designer' },
{ id: 3, name: 'Bob Johnson', email: 'bob@example.com', role: 'Manager' },
{ id: 4, name: 'Alice Brown', email: 'alice@example.com', role: 'Developer' },
{ id: 5, name: 'Charlie Wilson', email: 'charlie@example.com', role: 'Designer' },
];
useEffect(() => {
// Simulate loading data
setTimeout(() => {
setData(mockData);
setLoading(false);
}, 1000);
}, []);
// Sorting function
const sortData = (key) => {
let direction = 'asc';
// Three-state sorting: asc β desc β none
if (sortConfig.key === key) {
if (sortConfig.direction === 'asc') {
direction = 'desc';
} else if (sortConfig.direction === 'desc') {
// Reset to no sorting
setSortConfig({ key: null, direction: 'none' });
return;
}
}
setSortConfig({ key, direction });
};
// Get sorted data
const getSortedData = () => {
if (!sortConfig.key || sortConfig.direction === 'none') return data;
return [...data].sort((a, b) => {
let aValue = a[sortConfig.key];
let bValue = b[sortConfig.key];
// Handle string comparison (case-insensitive)
if (typeof aValue === 'string') {
aValue = aValue.toLowerCase();
bValue = bValue.toLowerCase();
}
if (aValue < bValue) {
return sortConfig.direction === 'asc' ? -1 : 1;
}
if (aValue > bValue) {
return sortConfig.direction === 'asc' ? 1 : -1;
}
return 0;
});
};
// Get sort indicator for column headers
const getSortIndicator = (key) => {
if (sortConfig.key !== key) return 'β΅';
if (sortConfig.direction === 'asc') return 'β';
if (sortConfig.direction === 'desc') return 'β';
return 'β΅';
};
const fetchFromMongoDB = async () => {
try {
setLoading(true);
// Replace this with your actual MongoDB connection logic
// const response = await fetch('/api/data');
// const result = await response.json();
// setData(result);
// For now, using mock data
setData(mockData);
} catch (err) {
setError('Failed to fetch data');
} finally {
setLoading(false);
}
};
if (loading) {
return (
<div className="flex items-center justify-center p-8">
<div className="text-lg text-muted-foreground">Loading...</div>
</div>
);
}
if (error) {
return (
<div className="flex items-center justify-center p-8">
<div className="text-red-500">{error}</div>
</div>
);
}
const sortedData = getSortedData();
return (
<div className="w-full mx-auto p-6">
<div className="flex justify-between items-center mb-6">
<h2 className="text-2xl font-bold text-foreground">Interesting Mock Data</h2>
<Button onClick={fetchFromMongoDB}>
Refresh Data
</Button>
</div>
<div className="border rounded-lg bg-card">
<Table>
<TableCaption>A list of users from the database.</TableCaption>
<TableHeader>
<TableRow>
<TableHead
className="cursor-pointer hover:bg-muted/50 transition-colors"
onClick={() => sortData('id')}
>
ID {getSortIndicator('id')}
</TableHead>
<TableHead
className="cursor-pointer hover:bg-muted/50 transition-colors"
onClick={() => sortData('name')}
>
Name {getSortIndicator('name')}
</TableHead>
<TableHead
className="cursor-pointer hover:bg-muted/50 transition-colors"
onClick={() => sortData('email')}
>
Email {getSortIndicator('email')}
</TableHead>
<TableHead
className="cursor-pointer hover:bg-muted/50 transition-colors"
onClick={() => sortData('role')}
>
Role {getSortIndicator('role')}
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{sortedData.map((user) => (
<TableRow key={user.id}>
<TableCell className="font-medium">{user.id}</TableCell>
<TableCell>{user.name}</TableCell>
<TableCell>{user.email}</TableCell>
<TableCell>{user.role}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
</div>
);
};
export default DataTable;
|