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';
|
|
|
|
|
2021-11-09 14:33:51 +01:00
|
|
|
// Redux
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { State } from '../../store/reducers';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { actionCreators } from '../../store';
|
|
|
|
|
2022-02-06 08:21:01 -05:00
|
|
|
import { importBookmark } from '../../store/action-creators';
|
|
|
|
|
2021-11-09 14:33:51 +01:00
|
|
|
// Typescript
|
|
|
|
import { Category, Bookmark } from '../../interfaces';
|
|
|
|
|
|
|
|
// CSS
|
2021-05-23 18:38:39 +02:00
|
|
|
import classes from './Bookmarks.module.css';
|
|
|
|
|
2021-11-09 14:33:51 +01:00
|
|
|
// UI
|
2021-12-02 14:12:23 +01:00
|
|
|
import {
|
|
|
|
Container,
|
|
|
|
Headline,
|
|
|
|
ActionButton,
|
|
|
|
Spinner,
|
|
|
|
Modal,
|
|
|
|
Message,
|
2022-02-06 08:21:01 -05:00
|
|
|
FileButton,
|
2021-12-02 14:12:23 +01:00
|
|
|
} from '../UI';
|
2021-11-09 14:33:51 +01:00
|
|
|
|
|
|
|
// Components
|
|
|
|
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 => {
|
2021-11-20 14:51:47 +01:00
|
|
|
// Get Redux state
|
2021-11-12 12:38:01 +01:00
|
|
|
const {
|
2021-11-22 14:36:00 +01:00
|
|
|
bookmarks: { loading, categories, categoryInEdit },
|
2021-11-12 12:38:01 +01:00
|
|
|
auth: { isAuthenticated },
|
|
|
|
} = useSelector((state: State) => state);
|
2021-11-09 14:33:51 +01:00
|
|
|
|
2021-11-20 14:51:47 +01:00
|
|
|
// Get Redux action creators
|
2021-11-09 14:33:51 +01:00
|
|
|
const dispatch = useDispatch();
|
2021-11-22 14:36:00 +01:00
|
|
|
const { getCategories, setEditCategory, setEditBookmark } =
|
|
|
|
bindActionCreators(actionCreators, dispatch);
|
2021-11-09 14:33:51 +01:00
|
|
|
|
2022-02-06 08:21:01 -05:00
|
|
|
const { importBookmark } = bindActionCreators(
|
|
|
|
actionCreators,
|
|
|
|
dispatch
|
|
|
|
);
|
|
|
|
|
2021-11-20 14:51:47 +01:00
|
|
|
// Load categories if array is empty
|
|
|
|
useEffect(() => {
|
|
|
|
if (!categories.length) {
|
|
|
|
getCategories();
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
setEditCategory(null);
|
|
|
|
}, []);
|
|
|
|
|
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);
|
2021-11-22 14:36:00 +01:00
|
|
|
setEditCategory(data);
|
2021-05-28 13:41:27 +02:00
|
|
|
} else {
|
|
|
|
setFormContentType(ContentType.bookmark);
|
2021-11-22 14:36:00 +01:00
|
|
|
setEditBookmark(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) {
|
2021-11-25 16:54:27 +01:00
|
|
|
setEditCategory(null);
|
2021-11-20 14:51:47 +01:00
|
|
|
setShowTable(false);
|
|
|
|
} else {
|
|
|
|
setShowTable(true);
|
|
|
|
setTableContentType(contentType);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-06 08:21:01 -05:00
|
|
|
const importFromFile = async (file: File) => {
|
|
|
|
await importBookmark({file});
|
|
|
|
};
|
|
|
|
|
2021-12-02 14:12:23 +01:00
|
|
|
const finishEditing = () => {
|
|
|
|
setShowTable(false);
|
|
|
|
setEditCategory(null);
|
|
|
|
};
|
|
|
|
|
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}
|
|
|
|
/>
|
|
|
|
)}
|
2022-02-06 08:21:01 -05:00
|
|
|
<FileButton
|
|
|
|
name="Import From File"
|
|
|
|
icon="mdiPlusBox"
|
|
|
|
handler={async (f) => await importFromFile(f)}
|
|
|
|
/>
|
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>
|
|
|
|
) : (
|
|
|
|
<></>
|
|
|
|
)}
|
|
|
|
|
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
|
|
|
);
|
|
|
|
};
|