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

160 lines
4.3 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 { BookmarkTable } from './BookmarkTable/BookmarkTable';
import { Form } from './Form/Form';
// 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 => {
2021-11-12 12:38:01 +01:00
const {
bookmarks: { loading, categories },
auth: { isAuthenticated },
} = useSelector((state: State) => state);
const dispatch = useDispatch();
const { getCategories } = bindActionCreators(actionCreators, dispatch);
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) {
getCategories();
}
}, []);
// observe if user is authenticated -> set default view if not
useEffect(() => {
if (!isAuthenticated) {
setIsInEdit(false);
setModalIsOpen(false);
}
}, [isAuthenticated]);
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}>
<Form
modalHandler={toggleModal}
contentType={formContentType}
inUpdate={isInUpdate}
category={categoryInUpdate}
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-11-12 12:38:01 +01:00
{isAuthenticated && (
<div className={classes.ActionsContainer}>
<ActionButton
name="Add Category"
icon="mdiPlusBox"
handler={() => addActionHandler(ContentType.category)}
/>
<ActionButton
name="Add Bookmark"
icon="mdiPlusBox"
handler={() => addActionHandler(ContentType.bookmark)}
/>
<ActionButton
name="Edit Categories"
icon="mdiPencil"
handler={() => editActionHandler(ContentType.category)}
/>
<ActionButton
name="Edit Bookmarks"
icon="mdiPencil"
handler={() => editActionHandler(ContentType.bookmark)}
/>
</div>
)}
2021-09-06 13:22:47 +02:00
{loading ? (
<Spinner />
) : !isInEdit ? (
<BookmarkGrid categories={categories} searching={props.searching} />
2021-09-06 13:22:47 +02:00
) : (
<BookmarkTable
contentType={tableContentType}
categories={categories}
updateHandler={goToUpdateMode}
/>
)}
</Container>
2021-09-06 13:22:47 +02:00
);
};