import { useEffect, useState } from 'react'; import config from '../config'; import instance from '../security/http'; import { Table, TableHeader, TableColumn, TableBody, TableRow, TableCell, SortDescriptor, Button } from '@nextui-org/react'; import { EmailIcon, TrashDeleteIcon } from '../icons'; interface FormData { id: number; electricalBill: number; waterBill: number; totalBill: number; noOfDependents: number; avgBill: number; ebPicture: string; wbPicture: string; userId: string; } export default function Ranking() { const [formData, setFormData] = useState([]); const [sortDescriptor, setSortDescriptor] = useState({ column: 'avgBill', direction: 'ascending', }); useEffect(() => { const getFormData = async () => { try { const response = await instance.get(`${config.serverAddress}/hbcform`); const processedData = response.data.map((data: FormData) => ({ ...data, electricalBill: Number(data.electricalBill), waterBill: Number(data.waterBill), totalBill: Number(data.totalBill), avgBill: Number(data.avgBill), })); setFormData(processedData); } catch (error) { console.log("Failed to fetch form data"); } }; getFormData(); }, []); const sortFormData = (list: FormData[], descriptor: SortDescriptor) => { const { column, direction } = descriptor; if (column === 'avgBill') { return [...list].sort((a, b) => direction === 'ascending' ? a.avgBill - b.avgBill : b.avgBill - a.avgBill ); } return list; // No sorting if the column is not 'avgBill' }; const handleSort = () => { const { direction } = sortDescriptor; const newDirection = direction === 'ascending' ? 'descending' : 'ascending'; setSortDescriptor({ column: 'avgBill', direction: newDirection }); }; const renderSortIndicator = () => { if (sortDescriptor.column === 'avgBill') { return sortDescriptor.direction === 'ascending' ? : ; } return null; }; const sortedFormData = sortFormData(formData, sortDescriptor); return (

Form Data

User ID Electrical Bill Water Bill Total Bill Number of Dependents Average Bill {renderSortIndicator()} Electrical Bill Picture Water Bill Picture Actions {sortedFormData.map((data) => ( {data.userId} ${data.electricalBill.toFixed(2)} ${data.waterBill.toFixed(2)} ${data.totalBill.toFixed(2)} {data.noOfDependents} ${data.avgBill.toFixed(2)} {data.ebPicture && Electrical Bill} {data.wbPicture && Water Bill} ))}
); }