|
@@ -18,11 +18,11 @@ import { Container, Headline, ActionButton, Spinner, Modal } from '../UI';
|
|
|
|
|
|
// Components
|
|
// Components
|
|
import { BookmarkGrid } from './BookmarkGrid/BookmarkGrid';
|
|
import { BookmarkGrid } from './BookmarkGrid/BookmarkGrid';
|
|
-import { BookmarkTable } from './BookmarkTable/BookmarkTable';
|
|
|
|
import { Form } from './Form/Form';
|
|
import { Form } from './Form/Form';
|
|
|
|
|
|
// Utils
|
|
// Utils
|
|
import { bookmarkTemplate, categoryTemplate } from '../../utility';
|
|
import { bookmarkTemplate, categoryTemplate } from '../../utility';
|
|
|
|
+import { Table } from './Table/Table';
|
|
|
|
|
|
interface Props {
|
|
interface Props {
|
|
searching: boolean;
|
|
searching: boolean;
|
|
@@ -34,66 +34,64 @@ export enum ContentType {
|
|
}
|
|
}
|
|
|
|
|
|
export const Bookmarks = (props: Props): JSX.Element => {
|
|
export const Bookmarks = (props: Props): JSX.Element => {
|
|
|
|
+ // Get Redux state
|
|
const {
|
|
const {
|
|
bookmarks: { loading, categories },
|
|
bookmarks: { loading, categories },
|
|
auth: { isAuthenticated },
|
|
auth: { isAuthenticated },
|
|
} = useSelector((state: State) => state);
|
|
} = useSelector((state: State) => state);
|
|
|
|
|
|
|
|
+ // Get Redux action creators
|
|
const dispatch = useDispatch();
|
|
const dispatch = useDispatch();
|
|
const { getCategories } = bindActionCreators(actionCreators, dispatch);
|
|
const { getCategories } = bindActionCreators(actionCreators, dispatch);
|
|
|
|
|
|
|
|
+ // Load categories if array is empty
|
|
|
|
+ useEffect(() => {
|
|
|
|
+ if (!categories.length) {
|
|
|
|
+ getCategories();
|
|
|
|
+ }
|
|
|
|
+ }, []);
|
|
|
|
+
|
|
|
|
+ // Form
|
|
const [modalIsOpen, setModalIsOpen] = useState(false);
|
|
const [modalIsOpen, setModalIsOpen] = useState(false);
|
|
const [formContentType, setFormContentType] = useState(ContentType.category);
|
|
const [formContentType, setFormContentType] = useState(ContentType.category);
|
|
- const [isInEdit, setIsInEdit] = useState(false);
|
|
|
|
- const [tableContentType, setTableContentType] = useState(
|
|
|
|
- ContentType.category
|
|
|
|
- );
|
|
|
|
const [isInUpdate, setIsInUpdate] = useState(false);
|
|
const [isInUpdate, setIsInUpdate] = useState(false);
|
|
const [categoryInUpdate, setCategoryInUpdate] =
|
|
const [categoryInUpdate, setCategoryInUpdate] =
|
|
useState<Category>(categoryTemplate);
|
|
useState<Category>(categoryTemplate);
|
|
const [bookmarkInUpdate, setBookmarkInUpdate] =
|
|
const [bookmarkInUpdate, setBookmarkInUpdate] =
|
|
useState<Bookmark>(bookmarkTemplate);
|
|
useState<Bookmark>(bookmarkTemplate);
|
|
|
|
|
|
- useEffect(() => {
|
|
|
|
- if (!categories.length) {
|
|
|
|
- getCategories();
|
|
|
|
- }
|
|
|
|
- }, []);
|
|
|
|
|
|
+ // Table
|
|
|
|
+ const [showTable, setShowTable] = useState(false);
|
|
|
|
+ const [tableContentType, setTableContentType] = useState(
|
|
|
|
+ ContentType.category
|
|
|
|
+ );
|
|
|
|
|
|
- // observe if user is authenticated -> set default view if not
|
|
|
|
|
|
+ // Observe if user is authenticated -> set default view (grid) if not
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
if (!isAuthenticated) {
|
|
if (!isAuthenticated) {
|
|
- setIsInEdit(false);
|
|
|
|
|
|
+ setShowTable(false);
|
|
setModalIsOpen(false);
|
|
setModalIsOpen(false);
|
|
}
|
|
}
|
|
}, [isAuthenticated]);
|
|
}, [isAuthenticated]);
|
|
|
|
|
|
|
|
+ // Form actions
|
|
const toggleModal = (): void => {
|
|
const toggleModal = (): void => {
|
|
setModalIsOpen(!modalIsOpen);
|
|
setModalIsOpen(!modalIsOpen);
|
|
};
|
|
};
|
|
|
|
|
|
- const addActionHandler = (contentType: ContentType) => {
|
|
|
|
|
|
+ const openFormForAdding = (contentType: ContentType) => {
|
|
setFormContentType(contentType);
|
|
setFormContentType(contentType);
|
|
setIsInUpdate(false);
|
|
setIsInUpdate(false);
|
|
toggleModal();
|
|
toggleModal();
|
|
};
|
|
};
|
|
|
|
|
|
- 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);
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
|
|
+ const openFormForUpdating = (data: Category | Bookmark): void => {
|
|
|
|
+ setIsInUpdate(true);
|
|
|
|
|
|
- const instanceOfCategory = (object: any): object is Category => {
|
|
|
|
- return 'bookmarks' in object;
|
|
|
|
- };
|
|
|
|
|
|
+ const instanceOfCategory = (object: any): object is Category => {
|
|
|
|
+ return 'bookmarks' in object;
|
|
|
|
+ };
|
|
|
|
|
|
- const goToUpdateMode = (data: Category | Bookmark): void => {
|
|
|
|
- setIsInUpdate(true);
|
|
|
|
if (instanceOfCategory(data)) {
|
|
if (instanceOfCategory(data)) {
|
|
setFormContentType(ContentType.category);
|
|
setFormContentType(ContentType.category);
|
|
setCategoryInUpdate(data);
|
|
setCategoryInUpdate(data);
|
|
@@ -101,9 +99,21 @@ export const Bookmarks = (props: Props): JSX.Element => {
|
|
setFormContentType(ContentType.bookmark);
|
|
setFormContentType(ContentType.bookmark);
|
|
setBookmarkInUpdate(data);
|
|
setBookmarkInUpdate(data);
|
|
}
|
|
}
|
|
|
|
+
|
|
toggleModal();
|
|
toggleModal();
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+ // 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) {
|
|
|
|
+ setShowTable(false);
|
|
|
|
+ } else {
|
|
|
|
+ setShowTable(true);
|
|
|
|
+ setTableContentType(contentType);
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
return (
|
|
return (
|
|
<Container>
|
|
<Container>
|
|
<Modal isOpen={modalIsOpen} setIsOpen={toggleModal}>
|
|
<Modal isOpen={modalIsOpen} setIsOpen={toggleModal}>
|
|
@@ -123,35 +133,34 @@ export const Bookmarks = (props: Props): JSX.Element => {
|
|
<ActionButton
|
|
<ActionButton
|
|
name="Add Category"
|
|
name="Add Category"
|
|
icon="mdiPlusBox"
|
|
icon="mdiPlusBox"
|
|
- handler={() => addActionHandler(ContentType.category)}
|
|
|
|
|
|
+ handler={() => openFormForAdding(ContentType.category)}
|
|
/>
|
|
/>
|
|
<ActionButton
|
|
<ActionButton
|
|
name="Add Bookmark"
|
|
name="Add Bookmark"
|
|
icon="mdiPlusBox"
|
|
icon="mdiPlusBox"
|
|
- handler={() => addActionHandler(ContentType.bookmark)}
|
|
|
|
|
|
+ handler={() => openFormForAdding(ContentType.bookmark)}
|
|
/>
|
|
/>
|
|
<ActionButton
|
|
<ActionButton
|
|
name="Edit Categories"
|
|
name="Edit Categories"
|
|
icon="mdiPencil"
|
|
icon="mdiPencil"
|
|
- handler={() => editActionHandler(ContentType.category)}
|
|
|
|
|
|
+ handler={() => showTableForEditing(ContentType.category)}
|
|
/>
|
|
/>
|
|
- <ActionButton
|
|
|
|
|
|
+ {/* <ActionButton
|
|
name="Edit Bookmarks"
|
|
name="Edit Bookmarks"
|
|
icon="mdiPencil"
|
|
icon="mdiPencil"
|
|
- handler={() => editActionHandler(ContentType.bookmark)}
|
|
|
|
- />
|
|
|
|
|
|
+ handler={() => showTableForEditing(ContentType.bookmark)}
|
|
|
|
+ /> */}
|
|
</div>
|
|
</div>
|
|
)}
|
|
)}
|
|
|
|
|
|
{loading ? (
|
|
{loading ? (
|
|
<Spinner />
|
|
<Spinner />
|
|
- ) : !isInEdit ? (
|
|
|
|
|
|
+ ) : !showTable ? (
|
|
<BookmarkGrid categories={categories} searching={props.searching} />
|
|
<BookmarkGrid categories={categories} searching={props.searching} />
|
|
) : (
|
|
) : (
|
|
- <BookmarkTable
|
|
|
|
|
|
+ <Table
|
|
contentType={tableContentType}
|
|
contentType={tableContentType}
|
|
- categories={categories}
|
|
|
|
- updateHandler={goToUpdateMode}
|
|
|
|
|
|
+ openFormForUpdating={openFormForUpdating}
|
|
/>
|
|
/>
|
|
)}
|
|
)}
|
|
</Container>
|
|
</Container>
|