wisemapping-frontend/packages/webapp/src/components/maps-page/maps-list/index.tsx

447 lines
15 KiB
TypeScript
Raw Normal View History

2021-02-01 03:04:50 +01:00
import React, { useEffect } from 'react'
2021-01-28 02:27:19 +01:00
import { useStyles } from './styled';
2021-01-25 19:39:53 +01:00
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableContainer from '@material-ui/core/TableContainer';
import TableHead from '@material-ui/core/TableHead';
import TablePagination from '@material-ui/core/TablePagination';
import TableRow from '@material-ui/core/TableRow';
import TableSortLabel from '@material-ui/core/TableSortLabel';
import Toolbar from '@material-ui/core/Toolbar';
import Paper from '@material-ui/core/Paper';
import Checkbox from '@material-ui/core/Checkbox';
import IconButton from '@material-ui/core/IconButton';
import Tooltip from '@material-ui/core/Tooltip';
import DeleteIcon from '@material-ui/icons/Delete';
import StarRateRoundedIcon from '@material-ui/icons/StarRateRounded';
import MoreHorizIcon from '@material-ui/icons/MoreHoriz';
import { CSSProperties } from 'react';
import { useSelector } from 'react-redux';
import { activeInstance } from '../../../reducers/serviceSlice';
2021-01-28 02:27:19 +01:00
import { useMutation, useQuery, useQueryClient } from 'react-query';
2021-02-01 03:04:50 +01:00
import { ErrorInfo, MapInfo } from '../../../services';
import Service from '../../../services';
2021-01-25 19:39:53 +01:00
import ActionChooser, { ActionType } from '../action-chooser';
2021-01-30 09:51:02 +01:00
import ActionDispatcher from '../action-dispatcher';
import { InputBase, Link } from '@material-ui/core';
import SearchIcon from '@material-ui/icons/Search';
2021-01-31 09:29:36 +01:00
import moment from 'moment'
2021-02-01 03:04:50 +01:00
import { Filter } from '..';
2021-01-25 19:39:53 +01:00
function descendingComparator<T>(a: T, b: T, orderBy: keyof T) {
if (b[orderBy] < a[orderBy]) {
return -1;
}
if (b[orderBy] > a[orderBy]) {
return 1;
}
return 0;
}
type Order = 'asc' | 'desc';
function getComparator<Key extends keyof any>(
order: Order,
orderBy: Key,
): (a: { [key in Key]: number | string | boolean | string[] | undefined }, b: { [key in Key]: number | string | string[] | boolean }) => number {
return order === 'desc'
? (a, b) => descendingComparator(a, b, orderBy)
: (a, b) => -descendingComparator(a, b, orderBy);
}
function stableSort<T>(array: T[], comparator: (a: T, b: T) => number) {
const stabilizedThis = array.map((el, index) => [el, index] as [T, number]);
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]);
}
interface HeadCell {
id: keyof MapInfo;
2021-01-28 02:27:19 +01:00
label?: string;
2021-01-25 19:39:53 +01:00
numeric: boolean;
2021-01-31 09:29:36 +01:00
style?: CSSProperties;
2021-01-25 19:39:53 +01:00
}
const headCells: HeadCell[] = [
2021-01-31 09:29:36 +01:00
{ id: 'name', numeric: false, label: 'Name' },
{ id: 'labels', numeric: false },
{ id: 'creator', numeric: false, label: 'Creator', style: { width: '60px' } },
{ id: 'modified', numeric: true, label: 'Modified', style: { width: '30px' } }
2021-01-25 19:39:53 +01:00
];
interface EnhancedTableProps {
classes: ReturnType<typeof useStyles>;
numSelected: number;
onRequestSort: (event: React.MouseEvent<unknown>, property: keyof MapInfo) => void;
onSelectAllClick: (event: React.ChangeEvent<HTMLInputElement>) => void;
order: Order;
orderBy: string;
rowCount: number;
}
function EnhancedTableHead(props: EnhancedTableProps) {
const { classes, onSelectAllClick, order, orderBy, numSelected, rowCount, onRequestSort } = props;
const createSortHandler = (property: keyof MapInfo) => (event: React.MouseEvent<unknown>) => {
onRequestSort(event, property);
};
return (
<TableHead>
<TableRow>
2021-01-28 02:27:19 +01:00
<TableCell padding='checkbox' key='select' style={{ width: '20px' }} className={classes.headerCell}>
2021-01-25 19:39:53 +01:00
<Checkbox
indeterminate={numSelected > 0 && numSelected < rowCount}
checked={rowCount > 0 && numSelected === rowCount}
onChange={onSelectAllClick}
size='small'
inputProps={{ 'aria-label': 'select all desserts' }}
/>
</TableCell>
2021-01-31 08:42:16 +01:00
<TableCell padding='checkbox' key='starred' className={classes.headerCell}></TableCell>
2021-01-28 02:27:19 +01:00
{headCells.map((headCell) => {
2021-01-31 08:42:16 +01:00
return (<TableCell
2021-01-25 19:39:53 +01:00
key={headCell.id}
sortDirection={orderBy === headCell.id ? order : false}
style={headCell.style}
2021-01-28 02:27:19 +01:00
className={classes.headerCell}
2021-01-25 19:39:53 +01:00
>
<TableSortLabel
active={orderBy === headCell.id}
direction={orderBy === headCell.id ? order : 'asc'}
onClick={createSortHandler(headCell.id)}>
{headCell.label}
{orderBy === headCell.id ? (
<span className={classes.visuallyHidden}>
{order === 'desc' ? 'sorted descending' : 'sorted ascending'}
</span>
) : null}
</TableSortLabel>
2021-01-31 08:42:16 +01:00
</TableCell>)
})}
<TableCell padding='checkbox' key='action' className={classes.headerCell}></TableCell>
2021-01-25 19:39:53 +01:00
</TableRow>
</TableHead>
);
}
type ActionPanelState = {
el: HTMLElement | undefined,
mapId: number
}
2021-02-01 03:04:50 +01:00
interface MapsListProps {
filter: Filter
}
const mapsFilter = (filter: Filter, search: string): ((mapInfo: MapInfo) => boolean) => {
return (mapInfo: MapInfo) => {
// Check for filter condition
let result = false;
switch (filter.type) {
case 'all':
result = true;
break;
case 'public':
result = mapInfo.isPublic;
break;
case 'starred':
result = mapInfo.starred;
break;
default:
result = false;
}
// Does it match search filter criteria...
if (search && result) {
result = mapInfo.name.toLowerCase().indexOf(search.toLowerCase()) != -1;
}
return result;
}
}
export const MapsList = (props: MapsListProps) => {
2021-01-25 19:39:53 +01:00
const classes = useStyles();
const [order, setOrder] = React.useState<Order>('asc');
2021-02-01 03:04:50 +01:00
const [filter, setFilter] = React.useState<Filter>({ type: 'all' });
2021-01-25 19:39:53 +01:00
const [orderBy, setOrderBy] = React.useState<keyof MapInfo>('modified');
const [selected, setSelected] = React.useState<number[]>([]);
2021-02-01 03:04:50 +01:00
const [searchCondition, setSearchCondition] = React.useState<string>('');
2021-01-25 19:39:53 +01:00
const [page, setPage] = React.useState(0);
2021-02-01 03:04:50 +01:00
const [rowsPerPage, setRowsPerPage] = React.useState(10);
2021-01-25 19:39:53 +01:00
const service: Service = useSelector(activeInstance);
2021-02-01 03:04:50 +01:00
useEffect(() => {
console.log("Update maps state.")
setSelected([]);
setSearchCondition('');
setPage(0);
setFilter(props.filter)
queryClient.invalidateQueries('maps');
2021-01-25 19:39:53 +01:00
2021-02-01 03:04:50 +01:00
}, [props.filter.type]);
const { isLoading, error, data } = useQuery<unknown, ErrorInfo, MapInfo[]>('maps', async () => {
return await service.fetchAllMaps();
2021-01-25 19:39:53 +01:00
});
2021-02-01 03:04:50 +01:00
const mapsInfo: MapInfo[] = data ? data.filter(mapsFilter(filter, searchCondition)) : [];
2021-01-25 19:39:53 +01:00
const [activeRowAction, setActiveRowAction] = React.useState<ActionPanelState | undefined>(undefined);
type ActiveDialog = {
actionType: ActionType;
mapId: number
};
const [activeDialog, setActiveDialog] = React.useState<ActiveDialog | undefined>(undefined);
const handleRequestSort = (event: React.MouseEvent<unknown>, property: keyof MapInfo) => {
const isAsc = orderBy === property && order === 'asc';
setOrder(isAsc ? 'desc' : 'asc');
setOrderBy(property);
};
const handleSelectAllClick = (event: React.ChangeEvent<HTMLInputElement>): void => {
if (event.target.checked) {
const newSelecteds = mapsInfo.map((n) => n.id);
setSelected(newSelecteds);
return;
}
setSelected([]);
};
const handleRowClick = (event: React.MouseEvent<unknown>, id: number): void => {
const selectedIndex = selected.indexOf(id);
let newSelected: number[] = [];
if (selectedIndex === -1) {
newSelected = newSelected.concat(selected, id);
} else if (selectedIndex === 0) {
newSelected = newSelected.concat(selected.slice(1));
} else if (selectedIndex === selected.length - 1) {
newSelected = newSelected.concat(selected.slice(0, -1));
} else if (selectedIndex > 0) {
newSelected = newSelected.concat(
selected.slice(0, selectedIndex),
selected.slice(selectedIndex + 1),
);
}
setSelected(newSelected);
};
const handleChangePage = (event: unknown, newPage: number) => {
setPage(newPage);
};
const handleChangeRowsPerPage = (event: React.ChangeEvent<HTMLInputElement>) => {
setRowsPerPage(parseInt(event.target.value, 10));
setPage(0);
};
const handleActionClick = (mapId: number): ((event: any) => void) => {
return (event: any): void => {
setActiveRowAction(
{
mapId: mapId,
el: event.currentTarget
}
);
event.stopPropagation();
};
};
2021-01-28 02:27:19 +01:00
const queryClient = useQueryClient();
const starredMultation = useMutation<void, ErrorInfo, number>((id: number) => {
return service.changeStarred(id);
},
{
onSuccess: () => {
queryClient.invalidateQueries('maps');
},
onError: (error) => {
// setError(error);
}
}
);
const handleStarred = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>, id: number) => {
event.stopPropagation();
starredMultation.mutate(id);
}
2021-01-25 19:39:53 +01:00
const handleActionMenuClose = (action: ActionType): void => {
if (action) {
const mapId = activeRowAction?.mapId;
setActiveDialog({
actionType: action as ActionType,
mapId: mapId as number
});
}
2021-01-28 02:27:19 +01:00
setActiveRowAction(undefined);
2021-01-25 19:39:53 +01:00
};
2021-02-01 03:04:50 +01:00
const handleOnSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchCondition(e.target.value);
}
2021-01-25 19:39:53 +01:00
2021-02-01 03:04:50 +01:00
const isSelected = (id: number) => selected.indexOf(id) !== -1;
2021-01-25 19:39:53 +01:00
return (
<div className={classes.root}>
2021-01-28 02:27:19 +01:00
<Paper className={classes.paper} elevation={0}>
2021-01-30 09:51:02 +01:00
<Toolbar className={classes.toolbar} variant="dense">
<div className={classes.toolbarActions}>
{selected.length > 0 ? (
<Tooltip title="Delete">
<IconButton aria-label="delete">
<DeleteIcon />
</IconButton>
</Tooltip>
) : null}
</div>
<div className={classes.toolbarListActions}>
<TablePagination
style={{ float: 'right', border: "0", paddingBottom: "5px" }}
count={mapsInfo.length}
rowsPerPage={rowsPerPage}
page={page}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
component="div"
/>
<div className={classes.search}>
<div className={classes.searchIcon}>
<SearchIcon />
</div>
<InputBase
placeholder="Search…"
classes={{
root: classes.searchInputRoot,
input: classes.searchInputInput,
}}
inputProps={{ 'aria-label': 'search' }}
2021-02-01 03:04:50 +01:00
onChange={handleOnSearchChange}
2021-01-30 09:51:02 +01:00
/>
</div>
</div>
</Toolbar>
2021-01-28 02:27:19 +01:00
2021-01-25 19:39:53 +01:00
<TableContainer>
<Table
className={classes.table}
2021-01-28 02:27:19 +01:00
size="small"
2021-01-25 19:39:53 +01:00
stickyHeader
>
<EnhancedTableHead
classes={classes}
numSelected={selected.length}
order={order}
orderBy={orderBy}
onSelectAllClick={handleSelectAllClick}
onRequestSort={handleRequestSort}
rowCount={mapsInfo.length}
/>
2021-01-28 02:27:19 +01:00
2021-01-25 19:39:53 +01:00
<TableBody>
{isLoading ? (<TableRow></TableRow>) : stableSort(mapsInfo, getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row: MapInfo) => {
const isItemSelected = isSelected(row.id);
const labelId = row.id;
return (
<TableRow
hover
onClick={(event: any) => handleRowClick(event, row.id)}
role="checkbox"
aria-checked={isItemSelected}
tabIndex={-1}
key={row.id}
selected={isItemSelected}
2021-01-30 09:51:02 +01:00
style={{ border: "0" }}
2021-01-25 19:39:53 +01:00
>
2021-01-31 08:42:16 +01:00
<TableCell
padding="checkbox"
className={classes.bodyCell}>
2021-01-25 19:39:53 +01:00
<Checkbox
checked={isItemSelected}
inputProps={{ 'aria-labelledby': String(labelId) }}
2021-01-28 02:27:19 +01:00
size="small" />
</TableCell>
2021-01-25 19:39:53 +01:00
2021-01-31 08:42:16 +01:00
<TableCell
padding="checkbox"
className={classes.bodyCell}>
2021-01-25 19:39:53 +01:00
<Tooltip title="Starred">
2021-01-28 02:27:19 +01:00
<IconButton aria-label="Starred" size="small" onClick={(e) => handleStarred(e, row.id)}>
2021-01-25 19:39:53 +01:00
<StarRateRoundedIcon color="action" style={{ color: row.starred ? 'yellow' : 'gray' }} />
</IconButton>
</Tooltip>
2021-01-28 02:27:19 +01:00
</TableCell>
2021-01-31 08:42:16 +01:00
<TableCell className={classes.bodyCell}>
2021-01-30 09:51:02 +01:00
<Tooltip title="Open for edition" placement="bottom-start">
2021-01-28 02:27:19 +01:00
<Link href={`c/maps/${row.id}/edit`} color="textPrimary" underline="always">
{row.name}
</Link>
2021-01-30 09:51:02 +01:00
</Tooltip>
</TableCell>
2021-01-25 19:39:53 +01:00
2021-01-31 08:42:16 +01:00
<TableCell className={classes.bodyCell}>
{row.labels}
</TableCell>
2021-01-31 09:29:36 +01:00
2021-01-31 08:42:16 +01:00
<TableCell className={classes.bodyCell}>
{row.creator}
</TableCell>
<TableCell className={classes.bodyCell}>
2021-01-31 11:40:21 +01:00
<Tooltip title={moment(row.modified).format("lll")} placement="bottom-start">
<span>{moment(row.modified).fromNow()}</span>
2021-01-31 09:29:36 +01:00
</Tooltip>
2021-01-31 08:42:16 +01:00
</TableCell>
2021-01-25 19:39:53 +01:00
2021-01-31 08:42:16 +01:00
<TableCell className={classes.bodyCell}>
2021-01-25 19:39:53 +01:00
<Tooltip title="Others">
<IconButton aria-label="Others" size="small" onClick={handleActionClick(row.id)}>
<MoreHorizIcon color="action" />
</IconButton>
</Tooltip>
<ActionChooser anchor={activeRowAction?.el} onClose={handleActionMenuClose} />
2021-01-28 02:27:19 +01:00
</TableCell>
2021-01-25 19:39:53 +01:00
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
2021-01-30 09:51:02 +01:00
2021-01-25 19:39:53 +01:00
</Paper>
{/* Action Dialog */}
<ActionDispatcher action={activeDialog?.actionType} onClose={() => setActiveDialog(undefined)} mapId={activeDialog ? activeDialog.mapId : -1} />
2021-01-28 02:27:19 +01:00
</div >
2021-01-25 19:39:53 +01:00
);
}