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

120 lines
2.7 KiB
TypeScript
Raw Normal View History

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';
import ActionButton from '../UI/Buttons/ActionButton/ActionButton';
import Modal from '../UI/Modal/Modal';
2021-05-10 19:02:16 +02:00
// Subcomponents
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;
}
const Apps = (props: ComponentProps): JSX.Element => {
const {
getApps,
apps,
loading
} = props;
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,
id: 0,
createdAt: new Date(),
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
const toggleModal = (): void => {
setModalIsOpen(!modalIsOpen);
2021-05-22 17:03:32 +02:00
setIsInUpdate(false);
}
const toggleEdit = (): void => {
setIsInEdit(!isInEdit);
2021-05-22 17:03:32 +02:00
setIsInUpdate(false);
}
const toggleUpdate = (app: App): void => {
setAppInUpdate(app);
setIsInUpdate(true);
setModalIsOpen(true);
}
2021-05-07 18:30:06 +02:00
return (
2021-05-10 19:02:16 +02:00
<Container>
<Modal isOpen={modalIsOpen} setIsOpen={setModalIsOpen}>
2021-05-22 17:03:32 +02:00
{!isInUpdate
? <AppForm modalHandler={toggleModal} />
: <AppForm modalHandler={toggleModal} app={appInUpdate} />
}
</Modal>
2021-05-10 19:02:16 +02:00
<Headline
title='All Applications'
subtitle={(<Link to='/'>Go back</Link>)}
2021-05-10 19:02:16 +02:00
/>
<div className={classes.ActionsContainer}>
<ActionButton
name='Add'
icon='mdiPlusBox'
handler={toggleModal}
/>
<ActionButton
name='Edit'
icon='mdiPencil'
handler={toggleEdit}
/>
</div>
2021-05-10 19:02:16 +02:00
<div className={classes.Apps}>
{loading
? <Spinner />
: (!isInEdit
? <AppGrid apps={apps} />
2021-05-22 17:03:32 +02:00
: <AppTable updateAppHandler={toggleUpdate} />)
2021-05-10 19:02:16 +02:00
}
</div>
</Container>
2021-05-07 18:30:06 +02:00
)
}
const mapStateToProps = (state: GlobalState) => {
2021-05-10 19:02:16 +02:00
return {
apps: state.app.apps,
loading: state.app.loading
}
}
2021-05-24 11:51:05 +02:00
export default connect(mapStateToProps, { getApps })(Apps);