Creating invert/clear buttons for SelectableTable.

This commit is contained in:
Roland 2022-10-24 17:15:15 -06:00
parent b81e00ff3d
commit 797bdc431c
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
7 changed files with 87 additions and 42 deletions

View file

@ -320,29 +320,32 @@ function Archives() {
</>);
const onSelect = (path, checked) => {
let archiveId;
if (archives) {
archives.forEach(i => {
if (i && i['archive'] && i['path'] === path) {
archiveId = i['archive']['id'];
}
});
}
if (checked && archiveId) {
setSelectedArchives([...selectedArchives, archiveId]);
} else {
setSelectedArchives(selectedArchives.filter(i => i !== archiveId));
if (checked && path) {
setSelectedArchives([...selectedArchives, path]);
} else if (path) {
setSelectedArchives(selectedArchives.filter(i => i !== path));
}
}
const onDelete = async (e) => {
e.preventDefault();
setDeleteOpen(false);
await deleteArchives(selectedArchives);
const archiveIds = archives.filter(i => selectedArchives.indexOf(i['path']) >= 0).map(i => i['archive']['id']);
await deleteArchives(archiveIds);
await fetchArchives();
setSelectedArchives([]);
}
const invertSelection = async () => {
const newSelectedArchives = archives.map(archive => archive['key']).filter(i => selectedArchives.indexOf(i) < 0);
setSelectedArchives(newSelectedArchives);
}
const clearSelection = async (e) => {
if (e) e.preventDefault();
setSelectedArchives([]);
}
const selectElm = <div style={{marginTop: '0.5em'}}>
<Button
color='red'
@ -356,6 +359,20 @@ function Archives() {
onCancel={() => setDeleteOpen(false)}
onConfirm={onDelete}
/>
<Button
color='grey'
onClick={() => invertSelection()}
disabled={!archives || archives.length === 0}
>
Invert
</Button>
<Button
color='yellow'
onClick={() => clearSelection()}
disabled={(archives && archives.length === 0) || selectedArchives.length === 0}
>
Clear
</Button>
</div>;
return <FilesView
@ -368,6 +385,7 @@ function Archives() {
showSelect={true}
onSelect={onSelect}
selectElem={selectElm}
selectedKeys={selectedArchives}
view={view}
setView={setView}
setPage={setPage}

View file

@ -320,7 +320,7 @@ export function secondsToTimestamp(seconds) {
return `${d.getFullYear()}-${d.getMonth() + 1}-${day} ${hours}:${minutes}:${sec}`;
}
export function humanFileSize(bytes, si = false, dp = 1) {
export function humanFileSize(bytes, dp = 1) {
const thresh = 1024;
if (Math.abs(bytes) < thresh) {

View file

@ -175,6 +175,8 @@ function FileCard({file}) {
const url = `/media/${encodeURIComponent(file.path)}`;
const color = mimetypeColor(file.mimetype);
const size = file.size !== null && file.size !== undefined ? humanFileSize(file.size) : null;
return <Card color={color}>
<ExternalCardLink to={url}>
<CardIcon>
@ -187,6 +189,8 @@ function FileCard({file}) {
{textEllipsis(file.title || file.stem || file.path)}
</ExternalCardLink>
</Card.Header>
<p>{uploadDate(file.modified / 1000)}</p>
<p>{size}</p>
</Card.Content>
</Card>
}
@ -248,7 +252,7 @@ function FileRow({file}) {
</React.Fragment>
}
export function FileTable({files, selectOn, onSelect, footer}) {
export function FileTable({files, selectOn, onSelect, footer, selectedKeys}) {
if (files && files.length > 0) {
const headerContents = ['Poster', 'Title'];
const rows = files.map(i => <FileRow key={i['key']} file={i}/>);
@ -256,6 +260,7 @@ export function FileTable({files, selectOn, onSelect, footer}) {
headerContents={headerContents}
selectOn={selectOn}
onSelect={onSelect}
selectedKeys={selectedKeys}
footer={footer}
rows={rows}
/>;
@ -282,6 +287,7 @@ export function FilesView({
showView = true,
showSelect = false,
selectElem,
selectedKeys,
onSelect,
setPage,
menuColumnsCount,
@ -343,7 +349,8 @@ export function FilesView({
<TableHeaderCell/>
<TableHeaderCell>{selectElem}</TableHeaderCell>
</> : null;
body = <FileTable files={files} selectOn={selectOn} onSelect={onSelect} footer={footer}/>;
body = <FileTable files={files} selectOn={selectOn}
onSelect={onSelect} footer={footer} selectedKeys={selectedKeys}/>;
} else {
body = <FileCards files={files}/>;
}

View file

@ -2,7 +2,7 @@ import {Table} from "./Theme";
import {Checkbox, TableBody, TableCell, TableFooter, TableHeader, TableHeaderCell, TableRow} from "semantic-ui-react";
import React from "react";
export function SelectableTable({headerContents, selectOn, onSelect, footer, rows}) {
export function SelectableTable({headerContents, selectOn, onSelect, selectedKeys, footer, rows}) {
footer = footer ? <TableFooter>
<TableRow>
{selectOn && <TableHeaderCell/>}
@ -19,28 +19,32 @@ export function SelectableTable({headerContents, selectOn, onSelect, footer, row
</TableHeader>
<TableBody>
{rows.map(row =>
<SelectableRow key={row.key} selectOn={selectOn} onSelect={onSelect}>{row}</SelectableRow>)}
<SelectableRow key={row.key} selectOn={selectOn} onSelect={onSelect} selectedKeys={selectedKeys}>
{row}
</SelectableRow>)}
</TableBody>
{footer}
</Table>
}
function SelectableRow(props) {
const {selectOn, onSelect} = props;
const {selectOn, onSelect, selectedKeys} = props;
const key = props.children.key;
const c = selectedKeys && selectedKeys.indexOf(key) >= 0;
let selectCell;
if (selectOn) {
const localOnSelect = (e, {checked}) => {
try {
onSelect(props.children.key, checked);
onSelect(key, checked);
} catch (e) {
console.error('No onSelect declared');
}
};
selectCell = <TableCell>
<Checkbox onChange={localOnSelect}/>
<Checkbox onChange={localOnSelect} checked={c}/>
</TableCell>;
}
return <TableRow key={props.children.key}>
return <TableRow key={key}>
{selectCell}
{props.children}
</TableRow>

View file

@ -112,29 +112,32 @@ function Videos({filter}) {
}
const onSelect = (path, checked) => {
let videoId;
if (videos) {
videos.forEach(i => {
if (i && i['video'] && i['path'] === path) {
videoId = i['video']['id'];
}
});
}
if (checked && videoId) {
setSelectedVideos([...selectedVideos, videoId]);
} else {
setSelectedVideos(selectedVideos.filter(i => i !== videoId));
if (checked && path) {
setSelectedVideos([...selectedVideos, path]);
} else if (path) {
setSelectedVideos(selectedVideos.filter(i => i !== path));
}
}
const onDelete = async (e) => {
e.preventDefault();
setDeleteOpen(false);
await deleteVideos(selectedVideos);
const videoIds = videos.filter(i => selectedVideos.indexOf(i['path']) >= 0).map(i => i['video']['id']);
await deleteVideos(videoIds);
await fetchVideos();
setSelectedVideos([]);
}
const invertSelection = async () => {
const newSelectedVideos = videos.map(video => video['key']).filter(i => selectedVideos.indexOf(i) < 0);
setSelectedVideos(newSelectedVideos);
}
const clearSelection = async (e) => {
if (e) e.preventDefault();
setSelectedVideos([]);
}
const selectElm = <div style={{marginTop: '0.5em'}}>
<Button
color='red'
@ -148,6 +151,20 @@ function Videos({filter}) {
onCancel={() => setDeleteOpen(false)}
onConfirm={onDelete}
/>
<Button
color='grey'
onClick={() => invertSelection()}
disabled={!videos || videos.length === 0}
>
Invert
</Button>
<Button
color='yellow'
onClick={() => clearSelection()}
disabled={(videos && videos.length === 0) || selectedVideos.length === 0}
>
Clear
</Button>
</div>;
return <>
@ -162,6 +179,7 @@ function Videos({filter}) {
showLimit={true}
showSelect={true}
onSelect={onSelect}
selectedKeys={selectedVideos}
selectElem={selectElm}
setView={setView}
setLimit={setLimit}