2021-06-13 01:06:42 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
2021-05-10 19:02:16 +02:00
|
|
|
import { Link } from 'react-router-dom';
|
2021-05-07 18:30:06 +02:00
|
|
|
|
2021-05-10 19:02:16 +02:00
|
|
|
// Redux
|
|
|
|
import { connect } from 'react-redux';
|
2021-05-24 11:51:05 +02:00
|
|
|
import { getApps } from '../../store/actions';
|
2021-05-10 19:02:16 +02:00
|
|
|
|
|
|
|
// Typescript
|
2021-05-24 11:51:05 +02:00
|
|
|
import { App, GlobalState } from '../../interfaces';
|
2021-05-10 19:02:16 +02:00
|
|
|
|
|
|
|
// CSS
|
2021-05-07 18:30:06 +02:00
|
|
|
import classes from './Apps.module.css';
|
|
|
|
|
2021-05-10 19:02:16 +02:00
|
|
|
// UI
|
2021-05-07 18:30:06 +02:00
|
|
|
import { Container } from '../UI/Layout/Layout';
|
2021-05-09 18:36:55 +02:00
|
|
|
import Headline from '../UI/Headlines/Headline/Headline';
|
2021-05-10 19:02:16 +02:00
|
|
|
import Spinner from '../UI/Spinner/Spinner';
|
2021-05-13 17:21:52 +02:00
|
|
|
import ActionButton from '../UI/Buttons/ActionButton/ActionButton';
|
|
|
|
import Modal from '../UI/Modal/Modal';
|
2021-05-10 19:02:16 +02:00
|
|
|
|
|
|
|
// Subcomponents
|
2021-05-13 17:21:52 +02:00
|
|
|
import AppGrid from './AppGrid/AppGrid';
|
|
|
|
import AppForm from './AppForm/AppForm';
|
|
|
|
import AppTable from './AppTable/AppTable';
|
2021-05-07 18:30:06 +02:00
|
|
|
|
2021-05-10 19:02:16 +02:00
|
|
|
interface ComponentProps {
|
|
|
|
getApps: Function;
|
|
|
|
apps: App[];
|
|
|
|
loading: boolean;
|
2021-09-06 12:24:01 +02:00
|
|
|
searching: boolean;
|
2021-05-10 19:02:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Apps = (props: ComponentProps): JSX.Element => {
|
2021-09-06 12:24:01 +02:00
|
|
|
const { getApps, apps, loading, searching = false } = props;
|
2021-06-13 01:06:42 +02:00
|
|
|
|
2021-05-13 17:21:52 +02:00
|
|
|
const [modalIsOpen, setModalIsOpen] = useState(false);
|
|
|
|
const [isInEdit, setIsInEdit] = useState(false);
|
2021-05-22 17:03:32 +02:00
|
|
|
const [isInUpdate, setIsInUpdate] = useState(false);
|
|
|
|
const [appInUpdate, setAppInUpdate] = useState<App>({
|
|
|
|
name: 'string',
|
|
|
|
url: 'string',
|
|
|
|
icon: 'string',
|
|
|
|
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-13 17:21:52 +02:00
|
|
|
|
2021-05-10 19:02:16 +02:00
|
|
|
useEffect(() => {
|
2021-06-13 01:06:42 +02:00
|
|
|
if (apps.length === 0) {
|
|
|
|
getApps();
|
2021-05-21 18:55:21 +02:00
|
|
|
}
|
2021-06-14 12:19:53 +02:00
|
|
|
}, [getApps]);
|
2021-05-10 19:02:16 +02:00
|
|
|
|
2021-05-13 17:21:52 +02:00
|
|
|
const toggleModal = (): void => {
|
|
|
|
setModalIsOpen(!modalIsOpen);
|
2021-05-22 17:03:32 +02:00
|
|
|
setIsInUpdate(false);
|
2021-09-06 12:24:01 +02:00
|
|
|
};
|
2021-05-13 17:21:52 +02:00
|
|
|
|
|
|
|
const toggleEdit = (): void => {
|
|
|
|
setIsInEdit(!isInEdit);
|
2021-05-22 17:03:32 +02:00
|
|
|
setIsInUpdate(false);
|
2021-09-06 12:24:01 +02:00
|
|
|
};
|
2021-05-22 17:03:32 +02:00
|
|
|
|
|
|
|
const toggleUpdate = (app: App): void => {
|
|
|
|
setAppInUpdate(app);
|
|
|
|
setIsInUpdate(true);
|
|
|
|
setModalIsOpen(true);
|
2021-09-06 12:24:01 +02:00
|
|
|
};
|
2021-05-13 17:21:52 +02:00
|
|
|
|
2021-05-07 18:30:06 +02:00
|
|
|
return (
|
2021-05-10 19:02:16 +02:00
|
|
|
<Container>
|
2021-05-21 18:55:21 +02:00
|
|
|
<Modal isOpen={modalIsOpen} setIsOpen={setModalIsOpen}>
|
2021-09-06 12:24:01 +02:00
|
|
|
{!isInUpdate ? (
|
|
|
|
<AppForm modalHandler={toggleModal} />
|
|
|
|
) : (
|
|
|
|
<AppForm modalHandler={toggleModal} app={appInUpdate} />
|
|
|
|
)}
|
2021-05-13 17:21:52 +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
|
|
|
|
2021-05-13 17:21:52 +02:00
|
|
|
<div className={classes.ActionsContainer}>
|
2021-09-06 12:24:01 +02:00
|
|
|
<ActionButton name="Add" icon="mdiPlusBox" handler={toggleModal} />
|
|
|
|
<ActionButton name="Edit" icon="mdiPencil" handler={toggleEdit} />
|
2021-05-13 17:21:52 +02:00
|
|
|
</div>
|
|
|
|
|
2021-05-10 19:02:16 +02:00
|
|
|
<div className={classes.Apps}>
|
2021-09-06 12:24:01 +02:00
|
|
|
{loading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : !isInEdit ? (
|
|
|
|
<AppGrid apps={apps} searching />
|
|
|
|
) : (
|
|
|
|
<AppTable updateAppHandler={toggleUpdate} />
|
|
|
|
)}
|
2021-05-10 19:02:16 +02:00
|
|
|
</div>
|
|
|
|
</Container>
|
2021-09-06 12:24:01 +02:00
|
|
|
);
|
|
|
|
};
|
2021-05-07 18:30:06 +02:00
|
|
|
|
2021-05-11 16:44:05 +02:00
|
|
|
const mapStateToProps = (state: GlobalState) => {
|
2021-05-10 19:02:16 +02:00
|
|
|
return {
|
|
|
|
apps: state.app.apps,
|
2021-09-06 12:24:01 +02:00
|
|
|
loading: state.app.loading,
|
|
|
|
};
|
|
|
|
};
|
2021-05-10 19:02:16 +02:00
|
|
|
|
2021-09-06 12:24:01 +02:00
|
|
|
export default connect(mapStateToProps, { getApps })(Apps);
|