1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-23 13:29:35 +02:00
flame/client/src/components/Apps/Apps.tsx

164 lines
4.7 KiB
TypeScript
Raw Normal View History

import { useEffect, useState } from 'react';
2021-05-10 19:02:16 +02:00
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
2021-05-07 18:30:06 +02:00
import { App, Category, GlobalState } from '../../interfaces';
import { getAppCategories, getApps } from '../../store/actions';
import ActionButton from '../UI/Buttons/ActionButton/ActionButton';
import Headline from '../UI/Headlines/Headline/Headline';
import { Container } from '../UI/Layout/Layout';
import Modal from '../UI/Modal/Modal';
import Spinner from '../UI/Spinner/Spinner';
import AppForm from './AppForm/AppForm';
import AppGrid from './AppGrid/AppGrid';
import classes from './Apps.module.css';
import AppTable from './AppTable/AppTable';
2021-05-07 18:30:06 +02:00
interface ComponentProps {
2021-05-10 19:02:16 +02:00
loading: boolean;
categories: Category[];
getAppCategories: () => void;
apps: App[];
getApps: () => void;
2021-09-06 12:24:01 +02:00
searching: boolean;
2021-05-10 19:02:16 +02:00
}
export enum ContentType {
category,
app
}
2021-05-10 19:02:16 +02:00
const Apps = (props: ComponentProps): JSX.Element => {
const {
apps,
getApps,
getAppCategories,
categories,
loading,
searching = false
} = props;
const [modalIsOpen, setModalIsOpen] = useState(false);
const [formContentType, setFormContentType] = useState(ContentType.category);
const [isInEdit, setIsInEdit] = useState(false);
const [tableContentType, setTableContentType] = useState(ContentType.category);
2021-05-22 17:03:32 +02:00
const [isInUpdate, setIsInUpdate] = useState(false);
const [categoryInUpdate, setCategoryInUpdate] = useState<Category>({
name: "",
id: -1,
isPinned: false,
orderId: 0,
type: "apps",
bookmarks: [],
createdAt: new Date(),
updatedAt: new Date()
})
2021-05-22 17:03:32 +02:00
const [appInUpdate, setAppInUpdate] = useState<App>({
name: "string",
url: "string",
categoryId: -1,
icon: "string",
2021-05-22 17:03:32 +02:00
isPinned: false,
2021-06-17 10:56:27 +02:00
orderId: 0,
2021-05-22 17:03:32 +02:00
id: 0,
createdAt: new Date(),
2021-09-06 12:24:01 +02:00
updatedAt: new Date(),
});
2021-05-10 19:02:16 +02:00
useEffect(() => {
if (apps.length === 0) {
getApps();
}
2021-06-14 12:19:53 +02:00
}, [getApps]);
2021-05-10 19:02:16 +02:00
useEffect(() => {
if (categories.length === 0) {
getAppCategories();
}
}, [getAppCategories])
const toggleModal = (): void => {
setModalIsOpen(!modalIsOpen);
// setIsInUpdate(false);
}
const addActionHandler = (contentType: ContentType) => {
setFormContentType(contentType);
2021-05-22 17:03:32 +02:00
setIsInUpdate(false);
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);
}
}
2021-05-22 17:03:32 +02:00
const instanceOfCategory = (object: any): object is Category => {
return "apps" in object;
}
const goToUpdateMode = (data: Category | App): void => {
2021-05-22 17:03:32 +02:00
setIsInUpdate(true);
if (instanceOfCategory(data)) {
setFormContentType(ContentType.category);
setCategoryInUpdate(data);
} else {
setFormContentType(ContentType.app);
setAppInUpdate(data);
}
toggleModal();
}
2021-05-07 18:30:06 +02:00
return (
2021-05-10 19:02:16 +02:00
<Container>
<Modal isOpen={modalIsOpen} setIsOpen={toggleModal}>
2021-09-06 12:24:01 +02:00
{!isInUpdate ? (
<AppForm modalHandler={toggleModal} contentType={formContentType} />
2021-09-06 12:24:01 +02:00
) : (
formContentType === ContentType.category ? (
<AppForm modalHandler={toggleModal} contentType={formContentType} category={categoryInUpdate} />
) : (
<AppForm modalHandler={toggleModal} contentType={formContentType} app={appInUpdate} />
)
2021-09-06 12:24:01 +02:00
)}
</Modal>
2021-05-10 19:02:16 +02:00
<Headline
2021-09-06 12:24:01 +02:00
title="All Applications"
subtitle={(<Link to="/">Go back</Link>)}
2021-05-10 19:02:16 +02:00
/>
2021-09-06 12:24:01 +02:00
<div className={classes.ActionsContainer}>
<ActionButton name="Add Category" icon="mdiPlusBox" handler={() => addActionHandler(ContentType.category)} />
<ActionButton name="Add App" icon="mdiPlusBox" handler={() => addActionHandler(ContentType.app)} />
<ActionButton name="Edit Categories" icon="mdiPencil" handler={() => editActionHandler(ContentType.category)} />
<ActionButton name="Edit Apps" icon="mdiPencil" handler={() => editActionHandler(ContentType.app)} />
</div>
{loading ? (
<Spinner />
) : (!isInEdit ? (
<AppGrid categories={categories} apps={apps} searching />
) : (
<AppTable contentType={tableContentType} categories={categories} apps={apps} updateHandler={goToUpdateMode} />
)
)}
2021-05-10 19:02:16 +02:00
</Container>
2021-09-06 12:24:01 +02:00
);
};
2021-05-07 18:30:06 +02:00
const mapStateToProps = (state: GlobalState) => {
2021-05-10 19:02:16 +02:00
return {
2021-09-06 12:24:01 +02:00
loading: state.app.loading,
categories: state.app.categories,
apps: state.app.apps,
}
}
2021-05-10 19:02:16 +02:00
export default connect(mapStateToProps, { getApps, getAppCategories })(Apps);