2021-06-26 15:06:47 -04:00
|
|
|
import { Fragment, KeyboardEvent, useEffect, useState } from 'react';
|
|
|
|
import { DragDropContext, Draggable, Droppable, DropResult } from 'react-beautiful-dnd';
|
2021-06-18 13:42:55 +02:00
|
|
|
import { connect } from 'react-redux';
|
2021-06-26 15:06:47 -04:00
|
|
|
import { Link } from 'react-router-dom';
|
2021-06-18 13:42:55 +02:00
|
|
|
|
|
|
|
import { Bookmark, Category, NewNotification } from '../../../interfaces';
|
2021-06-26 15:06:47 -04:00
|
|
|
import {
|
|
|
|
createNotification,
|
|
|
|
deleteBookmark,
|
|
|
|
deleteBookmarkCategory,
|
|
|
|
pinBookmarkCategory,
|
|
|
|
reorderBookmarkCategories,
|
|
|
|
} from '../../../store/actions';
|
|
|
|
import { searchConfig } from '../../../utility';
|
|
|
|
import Icon from '../../UI/Icons/Icon/Icon';
|
|
|
|
import Table from '../../UI/Table/Table';
|
2021-05-25 11:44:11 +02:00
|
|
|
import { ContentType } from '../Bookmarks';
|
|
|
|
import classes from './BookmarkTable.module.css';
|
|
|
|
|
|
|
|
interface ComponentProps {
|
|
|
|
contentType: ContentType;
|
|
|
|
categories: Category[];
|
2021-06-26 15:06:47 -04:00
|
|
|
pinBookmarkCategory: (category: Category) => void;
|
|
|
|
deleteBookmarkCategory: (id: number) => void;
|
2021-05-28 13:41:27 +02:00
|
|
|
updateHandler: (data: Category | Bookmark) => void;
|
|
|
|
deleteBookmark: (bookmarkId: number, categoryId: number) => void;
|
2021-06-18 13:42:55 +02:00
|
|
|
createNotification: (notification: NewNotification) => void;
|
2021-06-26 15:06:47 -04:00
|
|
|
reorderBookmarkCategories: (categories: Category[]) => void;
|
2021-05-25 11:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const BookmarkTable = (props: ComponentProps): JSX.Element => {
|
2021-06-18 13:42:55 +02:00
|
|
|
const [localCategories, setLocalCategories] = useState<Category[]>([]);
|
|
|
|
const [isCustomOrder, setIsCustomOrder] = useState<boolean>(false);
|
|
|
|
|
|
|
|
// Copy categories array
|
|
|
|
useEffect(() => {
|
|
|
|
setLocalCategories([...props.categories]);
|
2021-06-26 15:06:47 -04:00
|
|
|
}, [props.categories]);
|
2021-06-18 13:42:55 +02:00
|
|
|
|
|
|
|
// Check ordering
|
|
|
|
useEffect(() => {
|
2021-06-26 15:06:47 -04:00
|
|
|
const order = searchConfig("useOrdering", "");
|
2021-06-18 13:42:55 +02:00
|
|
|
|
2021-06-26 15:06:47 -04:00
|
|
|
if (order === "orderId") {
|
2021-06-18 13:42:55 +02:00
|
|
|
setIsCustomOrder(true);
|
|
|
|
}
|
2021-06-26 15:06:47 -04:00
|
|
|
}, []);
|
2021-06-18 13:42:55 +02:00
|
|
|
|
2021-05-25 14:05:53 +02:00
|
|
|
const deleteCategoryHandler = (category: Category): void => {
|
2021-06-26 15:06:47 -04:00
|
|
|
const proceed = window.confirm(
|
|
|
|
`Are you sure you want to delete ${category.name}? It will delete ALL assigned bookmarks`
|
|
|
|
);
|
2021-05-25 14:05:53 +02:00
|
|
|
|
|
|
|
if (proceed) {
|
2021-06-26 15:06:47 -04:00
|
|
|
props.deleteBookmarkCategory(category.id);
|
2021-05-25 14:05:53 +02:00
|
|
|
}
|
2021-06-26 15:06:47 -04:00
|
|
|
};
|
2021-05-25 14:05:53 +02:00
|
|
|
|
2021-05-28 13:41:27 +02:00
|
|
|
const deleteBookmarkHandler = (bookmark: Bookmark): void => {
|
2021-06-26 15:06:47 -04:00
|
|
|
const proceed = window.confirm(
|
|
|
|
`Are you sure you want to delete ${bookmark.name}?`
|
|
|
|
);
|
2021-05-28 13:41:27 +02:00
|
|
|
|
|
|
|
if (proceed) {
|
|
|
|
props.deleteBookmark(bookmark.id, bookmark.categoryId);
|
|
|
|
}
|
2021-06-26 15:06:47 -04:00
|
|
|
};
|
2021-05-28 13:41:27 +02:00
|
|
|
|
2021-06-26 15:06:47 -04:00
|
|
|
const keyboardActionHandler = (
|
|
|
|
e: KeyboardEvent,
|
|
|
|
category: Category,
|
|
|
|
handler: Function
|
|
|
|
) => {
|
|
|
|
if (e.key === "Enter") {
|
2021-05-25 14:05:53 +02:00
|
|
|
handler(category);
|
|
|
|
}
|
2021-06-26 15:06:47 -04:00
|
|
|
};
|
2021-05-25 14:05:53 +02:00
|
|
|
|
2021-06-26 15:06:47 -04:00
|
|
|
const dragEndHandler = (result: DropResult): void => {
|
2021-06-18 13:42:55 +02:00
|
|
|
if (!isCustomOrder) {
|
|
|
|
props.createNotification({
|
2021-06-26 15:06:47 -04:00
|
|
|
title: "Error",
|
|
|
|
message: "Custom order is disabled",
|
|
|
|
});
|
2021-06-18 13:42:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.destination) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tmpCategories = [...localCategories];
|
2021-06-26 15:06:47 -04:00
|
|
|
const [movedCategory] = tmpCategories.splice(result.source.index, 1);
|
|
|
|
tmpCategories.splice(result.destination.index, 0, movedCategory);
|
2021-06-18 13:42:55 +02:00
|
|
|
|
|
|
|
setLocalCategories(tmpCategories);
|
2021-06-26 15:06:47 -04:00
|
|
|
props.reorderBookmarkCategories(tmpCategories);
|
|
|
|
};
|
2021-06-18 13:42:55 +02:00
|
|
|
|
2021-05-25 11:44:11 +02:00
|
|
|
if (props.contentType === ContentType.category) {
|
|
|
|
return (
|
2021-06-18 13:42:55 +02:00
|
|
|
<Fragment>
|
|
|
|
<div className={classes.Message}>
|
2021-06-26 15:06:47 -04:00
|
|
|
{isCustomOrder ? (
|
|
|
|
<p>You can drag and drop single rows to reorder categories</p>
|
|
|
|
) : (
|
|
|
|
<p>
|
|
|
|
Custom order is disabled. You can change it in{" "}
|
|
|
|
<Link to="/settings/other">settings</Link>
|
|
|
|
</p>
|
|
|
|
)}
|
2021-06-18 13:42:55 +02:00
|
|
|
</div>
|
2021-06-26 15:06:47 -04:00
|
|
|
<DragDropContext onDragEnd={dragEndHandler}>
|
|
|
|
<Droppable droppableId="categories">
|
2021-06-18 13:42:55 +02:00
|
|
|
{(provided) => (
|
2021-06-26 15:06:47 -04:00
|
|
|
<Table headers={["Name", "Actions"]} innerRef={provided.innerRef}>
|
|
|
|
{localCategories.map(
|
|
|
|
(category: Category, index): JSX.Element => {
|
|
|
|
return (
|
|
|
|
<Draggable
|
|
|
|
key={category.id}
|
|
|
|
draggableId={category.id.toString()}
|
|
|
|
index={index}
|
|
|
|
>
|
|
|
|
{(provided, snapshot) => {
|
|
|
|
const style = {
|
|
|
|
border: snapshot.isDragging
|
|
|
|
? "1px solid var(--color-accent)"
|
|
|
|
: "none",
|
|
|
|
borderRadius: "4px",
|
|
|
|
...provided.draggableProps.style,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<tr
|
|
|
|
{...provided.draggableProps}
|
|
|
|
{...provided.dragHandleProps}
|
|
|
|
ref={provided.innerRef}
|
|
|
|
style={style}
|
|
|
|
>
|
|
|
|
<td>{category.name}</td>
|
|
|
|
{!snapshot.isDragging && (
|
|
|
|
<td className={classes.TableActions}>
|
|
|
|
<div
|
|
|
|
className={classes.TableAction}
|
|
|
|
onClick={() =>
|
|
|
|
deleteCategoryHandler(category)
|
|
|
|
}
|
|
|
|
onKeyDown={(e) =>
|
|
|
|
keyboardActionHandler(
|
|
|
|
e,
|
|
|
|
category,
|
|
|
|
deleteCategoryHandler
|
|
|
|
)
|
|
|
|
}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
|
|
|
<Icon icon="mdiDelete" />
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={classes.TableAction}
|
|
|
|
onClick={() =>
|
|
|
|
props.updateHandler(category)
|
|
|
|
}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
|
|
|
<Icon icon="mdiPencil" />
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={classes.TableAction}
|
|
|
|
onClick={() => props.pinBookmarkCategory(category)}
|
|
|
|
onKeyDown={(e) =>
|
|
|
|
keyboardActionHandler(
|
|
|
|
e,
|
|
|
|
category,
|
|
|
|
props.pinBookmarkCategory
|
|
|
|
)
|
|
|
|
}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
|
|
|
{category.isPinned ? (
|
|
|
|
<Icon
|
|
|
|
icon="mdiPinOff"
|
|
|
|
color="var(--color-accent)"
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Icon icon="mdiPin" />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
)}
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</Draggable>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)}
|
2021-06-18 13:42:55 +02:00
|
|
|
</Table>
|
|
|
|
)}
|
|
|
|
</Droppable>
|
|
|
|
</DragDropContext>
|
|
|
|
</Fragment>
|
2021-06-26 15:06:47 -04:00
|
|
|
);
|
2021-05-25 11:44:11 +02:00
|
|
|
} else {
|
2021-06-26 15:06:47 -04:00
|
|
|
const bookmarks: { bookmark: Bookmark; categoryName: string }[] = [];
|
2021-05-25 11:44:11 +02:00
|
|
|
props.categories.forEach((category: Category) => {
|
|
|
|
category.bookmarks.forEach((bookmark: Bookmark) => {
|
|
|
|
bookmarks.push({
|
|
|
|
bookmark,
|
2021-06-26 15:06:47 -04:00
|
|
|
categoryName: category.name,
|
2021-05-25 11:44:11 +02:00
|
|
|
});
|
2021-06-26 15:06:47 -04:00
|
|
|
});
|
|
|
|
});
|
2021-05-25 11:44:11 +02:00
|
|
|
|
|
|
|
return (
|
2021-06-26 15:06:47 -04:00
|
|
|
<Table headers={["Name", "URL", "Icon", "Category", "Actions"]}>
|
|
|
|
{bookmarks.map(
|
|
|
|
(bookmark: { bookmark: Bookmark; categoryName: string }) => {
|
|
|
|
return (
|
|
|
|
<tr key={bookmark.bookmark.id}>
|
|
|
|
<td>{bookmark.bookmark.name}</td>
|
|
|
|
<td>{bookmark.bookmark.url}</td>
|
|
|
|
<td>{bookmark.bookmark.icon}</td>
|
|
|
|
<td>{bookmark.categoryName}</td>
|
|
|
|
<td className={classes.TableActions}>
|
|
|
|
<div
|
|
|
|
className={classes.TableAction}
|
|
|
|
onClick={() => deleteBookmarkHandler(bookmark.bookmark)}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
|
|
|
<Icon icon="mdiDelete" />
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={classes.TableAction}
|
|
|
|
onClick={() => props.updateHandler(bookmark.bookmark)}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
|
|
|
<Icon icon="mdiPencil" />
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)}
|
2021-05-25 11:44:11 +02:00
|
|
|
</Table>
|
2021-06-26 15:06:47 -04:00
|
|
|
);
|
2021-05-25 11:44:11 +02:00
|
|
|
}
|
2021-06-26 15:06:47 -04:00
|
|
|
};
|
2021-05-25 11:44:11 +02:00
|
|
|
|
2021-06-18 13:42:55 +02:00
|
|
|
const actions = {
|
2021-06-26 15:06:47 -04:00
|
|
|
pinBookmarkCategory,
|
|
|
|
deleteBookmarkCategory,
|
2021-06-18 13:42:55 +02:00
|
|
|
deleteBookmark,
|
|
|
|
createNotification,
|
2021-06-26 15:06:47 -04:00
|
|
|
reorderBookmarkCategories,
|
|
|
|
};
|
2021-06-18 13:42:55 +02:00
|
|
|
|
2021-06-26 15:06:47 -04:00
|
|
|
export default connect(null, actions)(BookmarkTable);
|