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';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { getCategories } from '../../store/actions';
|
|
|
|
|
|
|
|
import classes from './Bookmarks.module.css';
|
|
|
|
|
|
|
|
import { Container } from '../UI/Layout/Layout';
|
|
|
|
import Headline from '../UI/Headlines/Headline/Headline';
|
|
|
|
import ActionButton from '../UI/Buttons/ActionButton/ActionButton';
|
|
|
|
|
|
|
|
import BookmarkGrid from './BookmarkGrid/BookmarkGrid';
|
2021-05-28 13:41:27 +02:00
|
|
|
import { Category, GlobalState, Bookmark } from '../../interfaces';
|
2021-05-23 18:38:39 +02:00
|
|
|
import Spinner from '../UI/Spinner/Spinner';
|
2021-05-24 11:51:05 +02:00
|
|
|
import Modal from '../UI/Modal/Modal';
|
|
|
|
import BookmarkForm from './BookmarkForm/BookmarkForm';
|
2021-05-25 11:44:11 +02:00
|
|
|
import BookmarkTable from './BookmarkTable/BookmarkTable';
|
2021-05-23 18:38:39 +02:00
|
|
|
|
|
|
|
interface ComponentProps {
|
|
|
|
loading: boolean;
|
|
|
|
categories: Category[];
|
|
|
|
getCategories: () => void;
|
|
|
|
}
|
|
|
|
|
2021-05-25 11:44:11 +02:00
|
|
|
export enum ContentType {
|
2021-05-24 11:51:05 +02:00
|
|
|
category,
|
|
|
|
bookmark
|
|
|
|
}
|
|
|
|
|
2021-05-23 18:38:39 +02:00
|
|
|
const Bookmarks = (props: ComponentProps): JSX.Element => {
|
2021-06-13 01:06:42 +02:00
|
|
|
const {
|
|
|
|
getCategories,
|
|
|
|
categories,
|
|
|
|
loading
|
|
|
|
} = 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);
|
|
|
|
const [tableContentType, setTableContentType] = useState(ContentType.category);
|
2021-05-26 13:13:56 +02:00
|
|
|
const [isInUpdate, setIsInUpdate] = useState(false);
|
|
|
|
const [categoryInUpdate, setCategoryInUpdate] = useState<Category>({
|
|
|
|
name: '',
|
|
|
|
id: -1,
|
|
|
|
isPinned: false,
|
|
|
|
bookmarks: [],
|
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date()
|
|
|
|
})
|
2021-05-28 13:41:27 +02:00
|
|
|
const [bookmarkInUpdate, setBookmarkInUpdate] = useState<Bookmark>({
|
|
|
|
name: '',
|
|
|
|
url: '',
|
|
|
|
categoryId: -1,
|
2021-06-09 12:45:55 +02:00
|
|
|
icon: '',
|
2021-05-28 13:41:27 +02:00
|
|
|
id: -1,
|
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date()
|
|
|
|
})
|
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-06-13 01:06:42 +02:00
|
|
|
}, [getCategories, categories])
|
2021-05-23 18:38:39 +02:00
|
|
|
|
2021-05-24 11:51:05 +02:00
|
|
|
const toggleModal = (): void => {
|
|
|
|
setModalIsOpen(!modalIsOpen);
|
|
|
|
}
|
|
|
|
|
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-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-05-28 13:41:27 +02:00
|
|
|
const instanceOfCategory = (object: any): object is Category => {
|
|
|
|
return 'bookmarks' in object;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-23 18:38:39 +02:00
|
|
|
return (
|
|
|
|
<Container>
|
2021-05-24 11:51:05 +02:00
|
|
|
<Modal isOpen={modalIsOpen} setIsOpen={toggleModal}>
|
2021-05-26 13:13:56 +02:00
|
|
|
{!isInUpdate
|
|
|
|
? <BookmarkForm modalHandler={toggleModal} contentType={formContentType} />
|
2021-05-28 13:41:27 +02:00
|
|
|
: formContentType === ContentType.category
|
|
|
|
? <BookmarkForm modalHandler={toggleModal} contentType={formContentType} category={categoryInUpdate} />
|
|
|
|
: <BookmarkForm modalHandler={toggleModal} contentType={formContentType} bookmark={bookmarkInUpdate} />
|
2021-05-26 13:13:56 +02:00
|
|
|
}
|
2021-05-24 11:51:05 +02:00
|
|
|
</Modal>
|
|
|
|
|
2021-05-23 18:38:39 +02:00
|
|
|
<Headline
|
|
|
|
title='All Bookmarks'
|
|
|
|
subtitle={(<Link to='/'>Go back</Link>)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className={classes.ActionsContainer}>
|
|
|
|
<ActionButton
|
2021-05-24 11:51:05 +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
|
|
|
|
name='Add Bookmark'
|
2021-05-23 18:38:39 +02:00
|
|
|
icon='mdiPlusBox'
|
2021-05-25 11:44:11 +02:00
|
|
|
handler={() => addActionHandler(ContentType.bookmark)}
|
2021-05-24 11:51:05 +02:00
|
|
|
/>
|
|
|
|
<ActionButton
|
|
|
|
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-05-24 11:51:05 +02:00
|
|
|
name='Edit Bookmarks'
|
2021-05-23 18:38:39 +02:00
|
|
|
icon='mdiPencil'
|
2021-05-25 11:44:11 +02:00
|
|
|
handler={() => editActionHandler(ContentType.bookmark)}
|
2021-05-23 18:38:39 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2021-06-13 01:06:42 +02:00
|
|
|
{loading
|
2021-05-23 18:38:39 +02:00
|
|
|
? <Spinner />
|
2021-05-25 11:44:11 +02:00
|
|
|
: (!isInEdit
|
2021-06-13 01:06:42 +02:00
|
|
|
? <BookmarkGrid categories={categories} />
|
2021-06-08 14:02:19 +02:00
|
|
|
: <BookmarkTable
|
2021-05-26 13:13:56 +02:00
|
|
|
contentType={tableContentType}
|
2021-06-13 01:06:42 +02:00
|
|
|
categories={categories}
|
2021-05-28 13:41:27 +02:00
|
|
|
updateHandler={goToUpdateMode}
|
2021-06-08 14:02:19 +02:00
|
|
|
/>
|
2021-05-26 13:13:56 +02:00
|
|
|
)
|
2021-05-23 18:38:39 +02:00
|
|
|
}
|
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = (state: GlobalState) => {
|
|
|
|
return {
|
|
|
|
loading: state.bookmark.loading,
|
|
|
|
categories: state.bookmark.categories
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, { getCategories })(Bookmarks);
|