1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-21 12:29:36 +02:00
flame/client/src/components/Bookmarks/Bookmarks.tsx

162 lines
4.4 KiB
TypeScript
Raw Normal View History

2021-05-24 11:51:05 +02:00
import { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
// 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
import classes from './Bookmarks.module.css';
// 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-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
}
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-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);
const [categoryInUpdate, setCategoryInUpdate] =
useState<Category>(categoryTemplate);
const [bookmarkInUpdate, setBookmarkInUpdate] =
useState<Bookmark>(bookmarkTemplate);
2021-05-24 11:51:05 +02:00
useEffect(() => {
if (categories.length === 0) {
getCategories();
}
2021-09-06 13:22:47 +02:00
}, [getCategories]);
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
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>} />
<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)}
/>
<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)}
/>
</div>
2021-09-06 13:22:47 +02:00
{loading ? (
<Spinner />
) : !isInEdit ? (
<BookmarkGrid categories={categories} searching />
) : (
<BookmarkTable
contentType={tableContentType}
categories={categories}
updateHandler={goToUpdateMode}
/>
)}
</Container>
2021-09-06 13:22:47 +02:00
);
};