import React, { useEffect } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, TableSortLabel, Paper } from '@material-ui/core'; import PropTypes from 'prop-types'; const channelName = window.location.href.split("/")[4] const adminToken = window.location.href.split("/")[5] const dataURL = "/topsongs/"+ channelName + "/" + adminToken const headCells = [ { id: 'songName', numeric: false, disablePadding: true, label: 'Song Name' }, { id: 'singCount', numeric: true, disablePadding: false, label: 'Count' }, ]; function descendingComparator(a, b, orderBy) { if (b[orderBy] < a[orderBy]) { return -1; } if (b[orderBy] > a[orderBy]) { return 1; } return 0; } function stableSort(array, comparator) { const stabilizedThis = array.map((el, index) => [el, index]); stabilizedThis.sort((a, b) => { const order = comparator(a[0], b[0]); if (order !== 0) return order; return a[1] - b[1]; }); return stabilizedThis.map((el) => el[0]); } function getComparator(order, orderBy) { return order === 'desc' ? (a, b) => descendingComparator(a, b, orderBy) : (a, b) => -descendingComparator(a, b, orderBy); } function EnhancedTableHead(props) { const { classes, order, orderBy, onRequestSort } = props; const createSortHandler = (property) => (event) => { onRequestSort(event, property); }; return ( {headCells.map((headCell) => ( {headCell.label} {orderBy === headCell.id ? ( {order === 'desc' ? 'sorted descending' : 'sorted ascending'} ) : null} ))} ); } EnhancedTableHead.propTypes = { classes: PropTypes.object.isRequired, numSelected: PropTypes.number.isRequired, onRequestSort: PropTypes.func.isRequired, onSelectAllClick: PropTypes.func.isRequired, order: PropTypes.oneOf(['asc', 'desc']).isRequired, orderBy: PropTypes.string.isRequired, rowCount: PropTypes.number.isRequired, }; function TopTenSongs() { const useStyles = makeStyles({ table: { minWidth: 650, }, }); const [order, setOrder] = React.useState('asc'); const [orderBy, setOrderBy] = React.useState('calories'); const [songState, setSongState] = React.useState({loading: true, duetData: []}) const classes = useStyles(); const handleRequestSort = (event, property) => { const isAsc = orderBy === property && order === 'asc'; setOrder(isAsc ? 'desc' : 'asc'); setOrderBy(property); }; useEffect(() => { // We should only fetch once! if (songState.loading) { fetch(dataURL).then((res) => res.json().then((data)=>{ setSongState({duetData: data, loading: false }) })); } }, [setSongState]); if (songState.loading) { return

Sorry, still loading...

} let dd = songState.duetData return ( Song Name Sings {stableSort(dd, getComparator(order, orderBy)) .map((row, index) => { const labelId = `enhanced-table-${index}`; return ( {row.songName} {row.singCount} ); })}
); } export default TopTenSongs;