2021-05-24 11:51:05 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
2021-05-23 18:38:39 +02:00
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
|
2021-11-09 14:33:51 +01:00
|
|
|
// Redux
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { State } from '../../store/reducers';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { actionCreators } from '../../store';
|
|
|
|
|
|
|
|
// Typescript
|
|
|
|
import { Category, Bookmark } from '../../interfaces';
|
|
|
|
|
|
|
|
// CSS
|
2021-05-23 18:38:39 +02:00
|
|
|
import classes from './Bookmarks.module.css';
|
|
|
|
|
2021-11-09 14:33:51 +01:00
|
|
|
// UI
|
|
|
|
import { Container, Headline, ActionButton, Spinner, Modal } from '../UI';
|
|
|
|
|
|
|
|
// Components
|
|
|
|
import { BookmarkGrid } from './BookmarkGrid/BookmarkGrid';
|
|
|
|
import { BookmarkForm } from './BookmarkForm/BookmarkForm';
|
|
|
|
import { BookmarkTable } from './BookmarkTable/BookmarkTable';
|
|
|
|
|
|
|
|
// Utils
|
|
|
|
import { bookmarkTemplate, categoryTemplate } from '../../utility';
|
|
|
|
|
|
|
|
interface Props {
|
2021-09-06 13:22:47 +02:00
|
|
|
searching: boolean;
|
2021-05-23 18:38:39 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 11:44:11 +02:00
|
|
|
export enum ContentType {
|
2021-05-24 11:51:05 +02:00
|
|
|
category,
|
2021-09-06 13:22:47 +02:00
|
|
|
bookmark,
|
2021-05-24 11:51:05 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:33:51 +01:00
|
|
|
export const Bookmarks = (props: Props): JSX.Element => {
|
|
|
|
const { loading, categories } = useSelector(
|
|
|
|
(state: State) => state.bookmarks
|
|
|
|
);
|
|
|
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const { getCategories } = bindActionCreators(actionCreators, dispatch);
|
|
|
|
|
|
|
|
const { searching = false } = props;
|
2021-06-13 01:06:42 +02:00
|
|
|
|
2021-05-24 11:51:05 +02:00
|
|
|
const [modalIsOpen, setModalIsOpen] = useState(false);
|
2021-05-25 11:44:11 +02:00
|
|
|
const [formContentType, setFormContentType] = useState(ContentType.category);
|
|
|
|
const [isInEdit, setIsInEdit] = useState(false);
|
2021-09-06 13:22:47 +02:00
|
|
|
const [tableContentType, setTableContentType] = useState(
|
|
|
|
ContentType.category
|
|
|
|
);
|
2021-05-26 13:13:56 +02:00
|
|
|
const [isInUpdate, setIsInUpdate] = useState(false);
|
2021-11-09 14:33:51 +01:00
|
|
|
const [categoryInUpdate, setCategoryInUpdate] =
|
|
|
|
useState<Category>(categoryTemplate);
|
|
|
|
const [bookmarkInUpdate, setBookmarkInUpdate] =
|
|
|
|
useState<Bookmark>(bookmarkTemplate);
|
2021-05-24 11:51:05 +02:00
|
|
|
|
2021-05-23 18:38:39 +02:00
|
|
|
useEffect(() => {
|
2021-06-13 01:06:42 +02:00
|
|
|
if (categories.length === 0) {
|
|
|
|
getCategories();
|
2021-05-23 18:38:39 +02:00
|
|
|
}
|
2021-09-06 13:22:47 +02:00
|
|
|
}, [getCategories]);
|
2021-05-23 18:38:39 +02:00
|
|
|
|
2021-05-24 11:51:05 +02:00
|
|
|
const toggleModal = (): void => {
|
|
|
|
setModalIsOpen(!modalIsOpen);
|
2021-09-06 13:22:47 +02:00
|
|
|
};
|
2021-05-24 11:51:05 +02:00
|
|
|
|
2021-05-25 11:44:11 +02:00
|
|
|
const addActionHandler = (contentType: ContentType) => {
|
2021-05-24 11:51:05 +02:00
|
|
|
setFormContentType(contentType);
|
2021-05-26 13:13:56 +02:00
|
|
|
setIsInUpdate(false);
|
2021-05-24 11:51:05 +02:00
|
|
|
toggleModal();
|
2021-09-06 13:22:47 +02:00
|
|
|
};
|
2021-05-24 11:51:05 +02:00
|
|
|
|
2021-05-25 11:44:11 +02:00
|
|
|
const editActionHandler = (contentType: ContentType) => {
|
|
|
|
// We're in the edit mode and the same button was clicked - go back to list
|
|
|
|
if (isInEdit && contentType === tableContentType) {
|
|
|
|
setIsInEdit(false);
|
|
|
|
} else {
|
|
|
|
setIsInEdit(true);
|
|
|
|
setTableContentType(contentType);
|
|
|
|
}
|
2021-09-06 13:22:47 +02:00
|
|
|
};
|
2021-05-25 11:44:11 +02:00
|
|
|
|
2021-05-28 13:41:27 +02:00
|
|
|
const instanceOfCategory = (object: any): object is Category => {
|
|
|
|
return 'bookmarks' in object;
|
2021-09-06 13:22:47 +02:00
|
|
|
};
|
2021-05-28 13:41:27 +02:00
|
|
|
|
|
|
|
const goToUpdateMode = (data: Category | Bookmark): void => {
|
2021-05-26 13:13:56 +02:00
|
|
|
setIsInUpdate(true);
|
2021-05-28 13:41:27 +02:00
|
|
|
if (instanceOfCategory(data)) {
|
|
|
|
setFormContentType(ContentType.category);
|
|
|
|
setCategoryInUpdate(data);
|
|
|
|
} else {
|
|
|
|
setFormContentType(ContentType.bookmark);
|
|
|
|
setBookmarkInUpdate(data);
|
|
|
|
}
|
2021-05-26 13:13:56 +02:00
|
|
|
toggleModal();
|
2021-09-06 13:22:47 +02:00
|
|
|
};
|
2021-05-26 13:13:56 +02:00
|
|
|
|
2021-05-23 18:38:39 +02:00
|
|
|
return (
|
|
|
|
<Container>
|
2021-05-24 11:51:05 +02:00
|
|
|
<Modal isOpen={modalIsOpen} setIsOpen={toggleModal}>
|
2021-09-06 13:22:47 +02:00
|
|
|
{!isInUpdate ? (
|
|
|
|
<BookmarkForm
|
|
|
|
modalHandler={toggleModal}
|
|
|
|
contentType={formContentType}
|
|
|
|
/>
|
|
|
|
) : formContentType === ContentType.category ? (
|
|
|
|
<BookmarkForm
|
|
|
|
modalHandler={toggleModal}
|
|
|
|
contentType={formContentType}
|
|
|
|
category={categoryInUpdate}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<BookmarkForm
|
|
|
|
modalHandler={toggleModal}
|
|
|
|
contentType={formContentType}
|
|
|
|
bookmark={bookmarkInUpdate}
|
|
|
|
/>
|
|
|
|
)}
|
2021-05-24 11:51:05 +02:00
|
|
|
</Modal>
|
|
|
|
|
2021-09-06 13:22:47 +02:00
|
|
|
<Headline title="All Bookmarks" subtitle={<Link to="/">Go back</Link>} />
|
|
|
|
|
2021-05-23 18:38:39 +02:00
|
|
|
<div className={classes.ActionsContainer}>
|
|
|
|
<ActionButton
|
2021-09-06 13:22:47 +02:00
|
|
|
name="Add Category"
|
|
|
|
icon="mdiPlusBox"
|
2021-05-25 11:44:11 +02:00
|
|
|
handler={() => addActionHandler(ContentType.category)}
|
2021-05-24 11:51:05 +02:00
|
|
|
/>
|
|
|
|
<ActionButton
|
2021-09-06 13:22:47 +02:00
|
|
|
name="Add Bookmark"
|
|
|
|
icon="mdiPlusBox"
|
2021-05-25 11:44:11 +02:00
|
|
|
handler={() => addActionHandler(ContentType.bookmark)}
|
2021-05-24 11:51:05 +02:00
|
|
|
/>
|
|
|
|
<ActionButton
|
2021-09-06 13:22:47 +02:00
|
|
|
name="Edit Categories"
|
|
|
|
icon="mdiPencil"
|
2021-05-25 11:44:11 +02:00
|
|
|
handler={() => editActionHandler(ContentType.category)}
|
2021-05-23 18:38:39 +02:00
|
|
|
/>
|
|
|
|
<ActionButton
|
2021-09-06 13:22:47 +02:00
|
|
|
name="Edit Bookmarks"
|
|
|
|
icon="mdiPencil"
|
2021-05-25 11:44:11 +02:00
|
|
|
handler={() => editActionHandler(ContentType.bookmark)}
|
2021-05-23 18:38:39 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2021-09-06 13:22:47 +02:00
|
|
|
{loading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : !isInEdit ? (
|
|
|
|
<BookmarkGrid categories={categories} searching />
|
|
|
|
) : (
|
|
|
|
<BookmarkTable
|
|
|
|
contentType={tableContentType}
|
|
|
|
categories={categories}
|
|
|
|
updateHandler={goToUpdateMode}
|
|
|
|
/>
|
|
|
|
)}
|
2021-05-23 18:38:39 +02:00
|
|
|
</Container>
|
2021-09-06 13:22:47 +02:00
|
|
|
);
|
|
|
|
};
|