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

144 lines
4.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';
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';
import { Category, GlobalState } from '../../interfaces';
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';
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
}
const Bookmarks = (props: ComponentProps): JSX.Element => {
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-24 11:51:05 +02:00
useEffect(() => {
if (props.categories.length === 0) {
props.getCategories();
}
}, [props.getCategories])
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 toggleEdit = (): void => {
setIsInEdit(!isInEdit);
}
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-26 13:13:56 +02:00
const goToUpdateMode = (category: Category): void => {
setIsInUpdate(true);
setCategoryInUpdate(category);
toggleModal();
}
let modalForm: JSX.Element;
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} />
: <BookmarkForm modalHandler={toggleModal} contentType={formContentType} category={categoryInUpdate} />
}
2021-05-24 11:51:05 +02:00
</Modal>
<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'
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)}
/>
<ActionButton
2021-05-24 11:51:05 +02:00
name='Edit Bookmarks'
icon='mdiPencil'
2021-05-25 11:44:11 +02:00
handler={() => editActionHandler(ContentType.bookmark)}
/>
</div>
{props.loading
? <Spinner />
2021-05-25 11:44:11 +02:00
: (!isInEdit
2021-05-25 14:05:53 +02:00
? props.categories.length > 0
? <BookmarkGrid categories={props.categories} />
2021-05-26 13:13:56 +02:00
: <p className={classes.BookmarksMessage}>You don't have any bookmarks. You can add a new one from <Link to='/bookmarks'>/bookmarks</Link> menu</p>
: (<BookmarkTable
contentType={tableContentType}
categories={props.categories}
updateCategoryHandler={goToUpdateMode}
/>)
)
}
</Container>
)
}
const mapStateToProps = (state: GlobalState) => {
return {
loading: state.bookmark.loading,
categories: state.bookmark.categories
}
}
export default connect(mapStateToProps, { getCategories })(Bookmarks);