[3798b7]: / client / src / components / DataInfo.jsx

Download this file

117 lines (109 with data), 3.6 kB

  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
import React, { useState, useEffect } from "react";
import { motion } from "framer-motion";
import * as Papa from "papaparse";
import _ from "lodash";
import Loader from "./Loader"; // Import the Loader component
function DataInfo() {
const [data, setData] = useState([]);
const [sortedColumn, setSortedColumn] = useState(null);
const [sortDirection, setSortDirection] = useState("asc");
useEffect(() => {
const fetchData = async () => {
try {
const response = await fetch("/diabetes.csv");
const csvData = await response.text();
Papa.parse(csvData, {
header: true,
dynamicTyping: true,
complete: (results) => {
setData(results.data);
},
});
} catch (error) {
console.error("Error fetching data:", error);
}
};
fetchData();
}, []);
const handleSort = (columnName) => {
if (sortedColumn === columnName) {
setSortDirection(sortDirection === "asc" ? "desc" : "asc");
} else {
setSortedColumn(columnName);
setSortDirection("asc");
}
};
const sortedData = _.orderBy(data, sortedColumn, sortDirection);
return (
<div className="container mx-auto my-8">
<motion.h1
initial={{ opacity: 0, y: 150 }}
whileInView={{ opacity: 1, y: 0 }}
viewport={{ once: true }}
transition={{
duration: 1,
type: "spring",
stiffness: 100,
delay: 0.5,
}}
className="text-3xl font-bold mb-4 text-purple-800 text-center p-2 border-b-2"
>
Data Information
</motion.h1>
{data.length > 0 ? (
<motion.div
initial={{ opacity: 0 }}
whileInView={{ opacity: 1 }}
viewport={{ once: true }}
transition={{
duration: 1,
type: "spring",
stiffness: 100,
delay: 0.5,
}}
className="overflow-x-auto overflow-y-auto rounded-lg shadow-lg mx-10"
style={{ maxHeight: "420px" }}
>
<div className="">
<table className="w-full border-collapse">
<thead>
<tr>
{Object.keys(data[0]).map((header, index) => (
<th
key={index}
onClick={() => handleSort(header)}
className="px-4 py-2 bg-purple-100 text-purple-800 sticky top-0 font-bold border cursor-pointer relative"
>
{header}
<span className="absolute right-0 top-1/2 transform -translate-y-1/2">
{sortedColumn === header
? sortDirection === "asc"
? " ▲"
: " ▼"
: "▼"}
</span>
</th>
))}
</tr>
</thead>
<tbody>
{sortedData.map((row, index) => (
<tr key={index}>
{Object.values(row).map((value, index) => (
<td key={index} className="px-4 py-2 border">
{value}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
</motion.div>
) : (
<Loader /> // Display the loader while data is being fetched
)}
</div>
);
}
export default DataInfo;