fleet/frontend/components/TableContainer/DataTable/DataTable.jsx
Gabe Hernandez 1f97aebf61
implement user table with new table and hook up create and edit and delete users (#587)
* hook up user and invite data together for data table

* added client derived data to user table

* hooked up action dropdown to table

* hooked up edit modal and password reset

* started adding editing user functiaonlity

* add query params to /invite call

* clean up editing teams

* update select team from to handle existing users with teams

* update closes modal now

* reuse getUser to clean up code in userManagementpage

* pass form data to updating user that is not the current User

* add dynamic userform submit text and fix tests

* fix lint error in table component

* added empty state for user table

* clean up unused data table props

* added delete modal

* add delete user functionality

* add delete option for invite

* Add styles for rows in user table and action dropdown cell

* hook up user and invite data together for data table

* added client derived data to user table

* hooked up action dropdown to table

* hooked up edit modal and password reset

* started adding editing user functiaonlity

* add query params to /invite call

* clean up editing teams

* update select team from to handle existing users with teams

* update closes modal now

* reuse getUser to clean up code in userManagementpage

* pass form data to updating user that is not the current User

* add dynamic userform submit text and fix tests

* fix lint error in table component

* added empty state for user table

* clean up unused data table props

* added delete modal

* add delete user functionality

* add delete option for invite

* Merge in generateClassTag

* Refactor table styles

* Add newline to DropdownCell style sheet

Co-authored-by: Gabriel Hernandez <ghernandez345@gmail.com>

* update checking for admin through global_role in nav

* added defaultGloablRole for creat user modal

* remove unused comment

* fix broken tests

* reenabled search for users tests

Co-authored-by: noahtalerman <47070608+noahtalerman@users.noreply.github.com>
2021-04-09 11:44:57 +01:00

116 lines
3 KiB
JavaScript

import React, { useMemo, useEffect } from 'react';
import PropTypes from 'prop-types';
import { useTable, useSortBy } from 'react-table';
import Spinner from 'components/loaders/Spinner';
const baseClass = 'data-table-container';
// This data table uses react-table for implementation. The relevant documentation of the library
// can be found here https://react-table.tanstack.com/docs/api/useTable
const DataTable = (props) => {
const {
columns: tableColumns,
data: tableData,
isLoading,
sortHeader,
sortDirection,
onSort,
} = props;
const columns = useMemo(() => {
return tableColumns;
}, [tableColumns]);
// The table data needs to be ordered by the order we received from the API.
const data = useMemo(() => {
return tableData;
}, [tableData]);
const {
headerGroups,
rows,
prepareRow,
state: tableState,
} = useTable(
{
columns,
data,
initialState: {
sortBy: useMemo(() => {
return [{ id: sortHeader, desc: sortDirection === 'desc' }];
}, [sortHeader, sortDirection]),
},
disableMultiSort: true,
},
useSortBy,
);
const { sortBy } = tableState;
useEffect(() => {
const column = sortBy[0];
if (column !== undefined) {
if (column.id !== sortHeader || column.desc !== (sortDirection === 'desc')) {
onSort(column.id, column.desc);
}
} else {
onSort(undefined);
}
}, [sortBy, sortHeader, sortDirection]);
return (
<div className={baseClass}>
<div className={'data-table data-table__wrapper'}>
{isLoading &&
<div className={'loading-overlay'}>
<Spinner />
</div>
}
{/* TODO: can this be memoized? seems performance heavy */}
<table className={'data-table__table'}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps(column.getSortByToggleProps())}>
{column.render('Header')}
</th>
))}
</tr>
))}
</thead>
<tbody>
{rows.map((row) => {
prepareRow(row);
return (
<tr {...row.getRowProps()}>
{row.cells.map((cell) => {
return (
<td {...cell.getCellProps()}>
{cell.render('Cell')}
</td>
);
})}
</tr>
);
})
}
</tbody>
</table>
</div>
</div>
);
};
DataTable.propTypes = {
columns: PropTypes.arrayOf(PropTypes.object), // TODO: create proper interface for this
data: PropTypes.arrayOf(PropTypes.object), // TODO: create proper interface for this
isLoading: PropTypes.bool,
sortHeader: PropTypes.string,
sortDirection: PropTypes.string,
onSort: PropTypes.func,
};
export default DataTable;