1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-19 19:49:37 +02:00
flame/client/src/components/Bookmarks/Bookmarks.tsx

213 lines
5.2 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';
2022-02-06 08:21:01 -05:00
import { importBookmark } from '../../store/action-creators';
// Typescript
import { Category, Bookmark } from '../../interfaces';
// CSS
import classes from './Bookmarks.module.css';
// UI
import {
Container,
Headline,
ActionButton,
Spinner,
Modal,
Message,
2022-02-06 08:21:01 -05:00
FileButton,
} from '../UI';
// Components
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 => {
// Get Redux state
2021-11-12 12:38:01 +01:00
const {
bookmarks: { loading, categories, categoryInEdit },
2021-11-12 12:38:01 +01:00
auth: { isAuthenticated },
} = useSelector((state: State) => state);
// Get Redux action creators
const dispatch = useDispatch();
const { getCategories, setEditCategory, setEditBookmark } =
bindActionCreators(actionCreators, dispatch);
2022-02-06 08:21:01 -05:00
const { importBookmark } = bindActionCreators(
actionCreators,
dispatch
);
// 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
// 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);
setEditCategory(null);
}, []);
// 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);
setEditCategory(data);
2021-05-28 13:41:27 +02:00
} else {
setFormContentType(ContentType.bookmark);
setEditBookmark(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) {
2021-11-25 16:54:27 +01:00
setEditCategory(null);
setShowTable(false);
} else {
setShowTable(true);
setTableContentType(contentType);
}
};
2022-02-06 08:21:01 -05:00
const importFromFile = async (file: File) => {
await importBookmark({file});
};
const finishEditing = () => {
setShowTable(false);
setEditCategory(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}
/>
)}
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>
)}
{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 />
) : !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
);
};