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

189 lines
4.8 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';
2022-11-30 20:46:28 +00:00
import { Bookmark, Category } from '../../interfaces';
import {
2022-11-30 20:46:28 +00:00
ActionButton,
Container,
Headline,
Message,
2022-11-30 20:46:28 +00:00
Modal,
Spinner,
} from '../UI';
2022-11-30 20:46:28 +00:00
import classes from './Bookmarks.module.css';
// 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';
import { BookmarkGrid } from './BookmarkGrid/BookmarkGrid';
import { Form } from './Form/Form';
import { Table } from './Table/Table';
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 => {
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);
2022-11-30 20:46:28 +00:00
const fetchCategories = useFetchCategories();
// Load categories if array is empty
useEffect(() => {
if (!categories.length) {
2022-11-30 20:46:28 +00:00
fetchCategories();
}
}, []);
// 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
// Table
const [showTable, setShowTable] = useState(false);
const [tableContentType, setTableContentType] = useState(
ContentType.category
);
// Observe if user is authenticated -> set default view (grid) if not
useEffect(() => {
if (!isAuthenticated) {
setShowTable(false);
setModalIsOpen(false);
}
}, [isAuthenticated]);
useEffect(() => {
if (categoryInEdit && !modalIsOpen) {
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
}, []);
// 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
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
const openFormForUpdating = (data: Category | Bookmark): void => {
setIsInUpdate(true);
2021-05-25 11:44:11 +02: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-05-26 13:13:56 +02:00
toggleModal();
2021-09-06 13:22:47 +02:00
};
2021-05-26 13:13:56 +02: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);
setShowTable(false);
} else {
setShowTable(true);
setTableContentType(contentType);
}
};
const finishEditing = () => {
setShowTable(false);
2022-11-30 20:46:28 +00:00
setBookmarkInEdit(null);
};
return (
<Container>
2021-05-24 11:51:05 +02:00
<Modal isOpen={modalIsOpen} setIsOpen={toggleModal}>
<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"
handler={() => openFormForAdding(ContentType.category)}
2021-11-12 12:38:01 +01:00
/>
<ActionButton
name="Add Bookmark"
icon="mdiPlusBox"
handler={() => openFormForAdding(ContentType.bookmark)}
2021-11-12 12:38:01 +01:00
/>
<ActionButton
name="Edit Categories"
icon="mdiPencil"
handler={() => showTableForEditing(ContentType.category)}
2021-11-12 12:38:01 +01:00
/>
{showTable && tableContentType === ContentType.bookmark && (
<ActionButton
name="Finish Editing"
icon="mdiPencil"
handler={finishEditing}
/>
)}
2021-11-12 12:38:01 +01:00
</div>
)}
{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-09-06 13:22:47 +02:00
{loading ? (
<Spinner />
) : !showTable ? (
<BookmarkGrid categories={categories} searching={props.searching} />
2021-09-06 13:22:47 +02:00
) : (
<Table
2021-09-06 13:22:47 +02:00
contentType={tableContentType}
openFormForUpdating={openFormForUpdating}
2021-09-06 13:22:47 +02:00
/>
)}
</Container>
2021-09-06 13:22:47 +02:00
);
};