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';
|
2022-11-30 20:46:28 +00:00
|
|
|
import { Bookmark, Category } from '../../interfaces';
|
2021-12-02 14:12:23 +01:00
|
|
|
import {
|
2022-11-30 20:46:28 +00:00
|
|
|
ActionButton,
|
2021-12-02 14:12:23 +01:00
|
|
|
Container,
|
|
|
|
Headline,
|
|
|
|
Message,
|
2022-11-30 20:46:28 +00:00
|
|
|
Modal,
|
|
|
|
Spinner,
|
2021-12-02 14:12:23 +01:00
|
|
|
} from '../UI';
|
2022-11-30 20:46:28 +00:00
|
|
|
import classes from './Bookmarks.module.css';
|
2021-11-09 14:33:51 +01:00
|
|
|
|
|
|
|
// Components
|
2022-11-30 20:46:28 +00:00
|
|
|
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
|
|
|
|
import { authAtom } from '../../state/auth';
|
|
|
|
import {
|
|
|
|
bookmarkInEditAtom,
|
|
|
|
bookmarksLoadingAtom,
|
|
|
|
categoriesAtom,
|
|
|
|
categoryInEditAtom,
|
|
|
|
useFetchCategories,
|
|
|
|
} from '../../state/bookmark';
|
2021-11-09 14:33:51 +01:00
|
|
|
import { BookmarkGrid } from './BookmarkGrid/BookmarkGrid';
|
2021-11-09 23:40:58 +01:00
|
|
|
import { Form } from './Form/Form';
|
2021-11-20 14:51:47 +01:00
|
|
|
import { Table } from './Table/Table';
|
2021-11-09 14:33:51 +01:00
|
|
|
|
|
|
|
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 => {
|
2022-11-30 20:46:28 +00:00
|
|
|
const { isAuthenticated } = useAtomValue(authAtom);
|
|
|
|
|
|
|
|
const loading = useAtomValue(bookmarksLoadingAtom);
|
|
|
|
const categories = useAtomValue(categoriesAtom);
|
|
|
|
const [categoryInEdit, setCategoryInEdit] = useAtom(categoryInEditAtom);
|
|
|
|
const setBookmarkInEdit = useSetAtom(bookmarkInEditAtom);
|
2021-11-09 14:33:51 +01:00
|
|
|
|
2022-11-30 20:46:28 +00:00
|
|
|
const fetchCategories = useFetchCategories();
|
2021-11-09 14:33:51 +01:00
|
|
|
|
2021-11-20 14:51:47 +01:00
|
|
|
// Load categories if array is empty
|
|
|
|
useEffect(() => {
|
|
|
|
if (!categories.length) {
|
2022-11-30 20:46:28 +00:00
|
|
|
fetchCategories();
|
2021-11-20 14:51:47 +01:00
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Form
|
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);
|
2021-05-26 13:13:56 +02:00
|
|
|
const [isInUpdate, setIsInUpdate] = useState(false);
|
2021-05-24 11:51:05 +02:00
|
|
|
|
2021-11-20 14:51:47 +01:00
|
|
|
// Table
|
|
|
|
const [showTable, setShowTable] = useState(false);
|
|
|
|
const [tableContentType, setTableContentType] = useState(
|
|
|
|
ContentType.category
|
|
|
|
);
|
2021-05-23 18:38:39 +02:00
|
|
|
|
2021-11-20 14:51:47 +01:00
|
|
|
// Observe if user is authenticated -> set default view (grid) if not
|
2021-11-14 23:20:37 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (!isAuthenticated) {
|
2021-11-20 14:51:47 +01:00
|
|
|
setShowTable(false);
|
2021-11-14 23:20:37 +01:00
|
|
|
setModalIsOpen(false);
|
|
|
|
}
|
|
|
|
}, [isAuthenticated]);
|
|
|
|
|
2021-11-22 14:36:00 +01:00
|
|
|
useEffect(() => {
|
2021-11-26 14:04:46 +01:00
|
|
|
if (categoryInEdit && !modalIsOpen) {
|
2021-11-22 14:36:00 +01:00
|
|
|
setTableContentType(ContentType.bookmark);
|
|
|
|
setShowTable(true);
|
|
|
|
}
|
|
|
|
}, [categoryInEdit]);
|
|
|
|
|
2021-11-25 16:54:27 +01:00
|
|
|
useEffect(() => {
|
|
|
|
setShowTable(false);
|
2022-11-30 20:46:28 +00:00
|
|
|
setCategoryInEdit(null);
|
2021-11-25 16:54:27 +01:00
|
|
|
}, []);
|
|
|
|
|
2021-11-20 14:51:47 +01:00
|
|
|
// Form actions
|
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-11-20 14:51:47 +01:00
|
|
|
const openFormForAdding = (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-11-20 14:51:47 +01:00
|
|
|
const openFormForUpdating = (data: Category | Bookmark): void => {
|
|
|
|
setIsInUpdate(true);
|
2021-05-25 11:44:11 +02:00
|
|
|
|
2021-11-20 14:51:47 +01:00
|
|
|
const instanceOfCategory = (object: any): object is Category => {
|
|
|
|
return 'bookmarks' in object;
|
|
|
|
};
|
2021-05-28 13:41:27 +02:00
|
|
|
|
|
|
|
if (instanceOfCategory(data)) {
|
|
|
|
setFormContentType(ContentType.category);
|
2022-11-30 20:46:28 +00:00
|
|
|
setCategoryInEdit(data);
|
2021-05-28 13:41:27 +02:00
|
|
|
} else {
|
|
|
|
setFormContentType(ContentType.bookmark);
|
2022-11-30 20:46:28 +00:00
|
|
|
setBookmarkInEdit(data);
|
2021-05-28 13:41:27 +02:00
|
|
|
}
|
2021-11-20 14:51:47 +01:00
|
|
|
|
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-11-20 14:51:47 +01:00
|
|
|
// Table actions
|
|
|
|
const showTableForEditing = (contentType: ContentType) => {
|
|
|
|
// We're in the edit mode and the same button was clicked - go back to list
|
|
|
|
if (showTable && contentType === tableContentType) {
|
2022-11-30 20:46:28 +00:00
|
|
|
setBookmarkInEdit(null);
|
2021-11-20 14:51:47 +01:00
|
|
|
setShowTable(false);
|
|
|
|
} else {
|
|
|
|
setShowTable(true);
|
|
|
|
setTableContentType(contentType);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-02 14:12:23 +01:00
|
|
|
const finishEditing = () => {
|
|
|
|
setShowTable(false);
|
2022-11-30 20:46:28 +00:00
|
|
|
setBookmarkInEdit(null);
|
2021-12-02 14:12:23 +01: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-11-09 23:40:58 +01:00
|
|
|
<Form
|
|
|
|
modalHandler={toggleModal}
|
|
|
|
contentType={formContentType}
|
|
|
|
inUpdate={isInUpdate}
|
|
|
|
/>
|
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"
|
2021-11-20 14:51:47 +01:00
|
|
|
handler={() => openFormForAdding(ContentType.category)}
|
2021-11-12 12:38:01 +01:00
|
|
|
/>
|
|
|
|
<ActionButton
|
|
|
|
name="Add Bookmark"
|
|
|
|
icon="mdiPlusBox"
|
2021-11-20 14:51:47 +01:00
|
|
|
handler={() => openFormForAdding(ContentType.bookmark)}
|
2021-11-12 12:38:01 +01:00
|
|
|
/>
|
|
|
|
<ActionButton
|
|
|
|
name="Edit Categories"
|
|
|
|
icon="mdiPencil"
|
2021-11-20 14:51:47 +01:00
|
|
|
handler={() => showTableForEditing(ContentType.category)}
|
2021-11-12 12:38:01 +01:00
|
|
|
/>
|
2021-12-02 14:12:23 +01:00
|
|
|
{showTable && tableContentType === ContentType.bookmark && (
|
|
|
|
<ActionButton
|
|
|
|
name="Finish Editing"
|
|
|
|
icon="mdiPencil"
|
|
|
|
handler={finishEditing}
|
|
|
|
/>
|
|
|
|
)}
|
2021-11-12 12:38:01 +01:00
|
|
|
</div>
|
|
|
|
)}
|
2021-05-23 18:38:39 +02:00
|
|
|
|
2021-12-02 14:12:23 +01:00
|
|
|
{categories.length && isAuthenticated && !showTable ? (
|
|
|
|
<Message isPrimary={false}>
|
|
|
|
Click on category name to edit its bookmarks
|
|
|
|
</Message>
|
2022-11-30 20:46:28 +00:00
|
|
|
) : null}
|
2021-12-02 14:12:23 +01:00
|
|
|
|
2021-09-06 13:22:47 +02:00
|
|
|
{loading ? (
|
|
|
|
<Spinner />
|
2021-11-20 14:51:47 +01:00
|
|
|
) : !showTable ? (
|
2021-11-09 15:51:50 +01:00
|
|
|
<BookmarkGrid categories={categories} searching={props.searching} />
|
2021-09-06 13:22:47 +02:00
|
|
|
) : (
|
2021-11-20 14:51:47 +01:00
|
|
|
<Table
|
2021-09-06 13:22:47 +02:00
|
|
|
contentType={tableContentType}
|
2021-11-20 14:51:47 +01:00
|
|
|
openFormForUpdating={openFormForUpdating}
|
2021-09-06 13:22:47 +02:00
|
|
|
/>
|
|
|
|
)}
|
2021-05-23 18:38:39 +02:00
|
|
|
</Container>
|
2021-09-06 13:22:47 +02:00
|
|
|
);
|
|
|
|
};
|