BookmarkTable.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { ContentType } from '../Bookmarks';
  2. import classes from './BookmarkTable.module.css';
  3. import { connect } from 'react-redux';
  4. import { pinCategory, deleteCategory, deleteBookmark } from '../../../store/actions';
  5. import { KeyboardEvent } from 'react';
  6. import Table from '../../UI/Table/Table';
  7. import { Bookmark, Category } from '../../../interfaces';
  8. import Icon from '../../UI/Icons/Icon/Icon';
  9. interface ComponentProps {
  10. contentType: ContentType;
  11. categories: Category[];
  12. pinCategory: (category: Category) => void;
  13. deleteCategory: (id: number) => void;
  14. updateHandler: (data: Category | Bookmark) => void;
  15. deleteBookmark: (bookmarkId: number, categoryId: number) => void;
  16. }
  17. const BookmarkTable = (props: ComponentProps): JSX.Element => {
  18. const deleteCategoryHandler = (category: Category): void => {
  19. const proceed = window.confirm(`Are you sure you want to delete ${category.name}? It will delete ALL assigned bookmarks`);
  20. if (proceed) {
  21. props.deleteCategory(category.id);
  22. }
  23. }
  24. const deleteBookmarkHandler = (bookmark: Bookmark): void => {
  25. const proceed = window.confirm(`Are you sure you want to delete ${bookmark.name}?`);
  26. if (proceed) {
  27. props.deleteBookmark(bookmark.id, bookmark.categoryId);
  28. }
  29. }
  30. const keyboardActionHandler = (e: KeyboardEvent, category: Category, handler: Function) => {
  31. if (e.key === 'Enter') {
  32. handler(category);
  33. }
  34. }
  35. if (props.contentType === ContentType.category) {
  36. return (
  37. <Table headers={[
  38. 'Name',
  39. 'Actions'
  40. ]}>
  41. {props.categories.map((category: Category) => {
  42. return (
  43. <tr key={category.id}>
  44. <td>{category.name}</td>
  45. <td className={classes.TableActions}>
  46. <div
  47. className={classes.TableAction}
  48. onClick={() => deleteCategoryHandler(category)}
  49. onKeyDown={(e) => keyboardActionHandler(e, category, deleteCategoryHandler)}
  50. tabIndex={0}>
  51. <Icon icon='mdiDelete' />
  52. </div>
  53. <div
  54. className={classes.TableAction}
  55. onClick={() => props.updateHandler(category)}
  56. // onKeyDown={(e) => keyboardActionHandler(e, app, props.updateAppHandler)}
  57. tabIndex={0}>
  58. <Icon icon='mdiPencil' />
  59. </div>
  60. <div
  61. className={classes.TableAction}
  62. onClick={() => props.pinCategory(category)}
  63. onKeyDown={(e) => keyboardActionHandler(e, category, props.pinCategory)}
  64. tabIndex={0}>
  65. {category.isPinned
  66. ? <Icon icon='mdiPinOff' color='var(--color-accent)' />
  67. : <Icon icon='mdiPin' />
  68. }
  69. </div>
  70. </td>
  71. </tr>
  72. )
  73. })}
  74. </Table>
  75. )
  76. } else {
  77. const bookmarks: {bookmark: Bookmark, categoryName: string}[] = [];
  78. props.categories.forEach((category: Category) => {
  79. category.bookmarks.forEach((bookmark: Bookmark) => {
  80. bookmarks.push({
  81. bookmark,
  82. categoryName: category.name
  83. });
  84. })
  85. })
  86. return (
  87. <Table headers={[
  88. 'Name',
  89. 'URL',
  90. 'Icon',
  91. 'Category',
  92. 'Actions'
  93. ]}>
  94. {bookmarks.map((bookmark: {bookmark: Bookmark, categoryName: string}) => {
  95. return (
  96. <tr key={bookmark.bookmark.id}>
  97. <td>{bookmark.bookmark.name}</td>
  98. <td>{bookmark.bookmark.url}</td>
  99. <td>{bookmark.bookmark.icon}</td>
  100. <td>{bookmark.categoryName}</td>
  101. <td className={classes.TableActions}>
  102. <div
  103. className={classes.TableAction}
  104. onClick={() => deleteBookmarkHandler(bookmark.bookmark)}
  105. // onKeyDown={(e) => keyboardActionHandler(e, app, deleteAppHandler)}
  106. tabIndex={0}>
  107. <Icon icon='mdiDelete' />
  108. </div>
  109. <div
  110. className={classes.TableAction}
  111. onClick={() => props.updateHandler(bookmark.bookmark)}
  112. // onKeyDown={(e) => keyboardActionHandler(e, app, props.updateAppHandler)}
  113. tabIndex={0}>
  114. <Icon icon='mdiPencil' />
  115. </div>
  116. </td>
  117. </tr>
  118. )
  119. })}
  120. </Table>
  121. )
  122. }
  123. }
  124. export default connect(null, { pinCategory, deleteCategory, deleteBookmark })(BookmarkTable);