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

97 lines
2.5 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';
import { getApps, pinApp, addApp } from '../../store/actions';
2021-05-10 19:02:16 +02:00
// Typescript
import { App, GlobalState, NewApp } 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;
pinApp: (id: number, isPinned: boolean) => void;
addApp: (formData: NewApp) => void;
2021-05-10 19:02:16 +02:00
apps: App[];
loading: boolean;
}
const Apps = (props: ComponentProps): JSX.Element => {
const [modalIsOpen, setModalIsOpen] = useState(false);
const [isInEdit, setIsInEdit] = useState(false);
2021-05-10 19:02:16 +02:00
useEffect(() => {
if (props.apps.length === 0) {
props.getApps();
}
2021-05-10 19:02:16 +02:00
}, [props.getApps]);
const toggleModal = (): void => {
setModalIsOpen(!modalIsOpen);
}
const toggleEdit = (): void => {
setIsInEdit(!isInEdit);
}
2021-05-07 18:30:06 +02:00
return (
2021-05-10 19:02:16 +02:00
<Container>
<Modal isOpen={modalIsOpen} setIsOpen={setModalIsOpen}>
<AppForm modalHandler={toggleModal} />
</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}>
{props.loading
? <Spinner />
: (!isInEdit
? props.apps.length > 0
? <AppGrid apps={props.apps} />
: <p className={classes.AppsMessage}>You don't have any applications. You can a new one from <Link to='/applications'>/application</Link> menu</p>
: <AppTable />)
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
}
}
export default connect(mapStateToProps, { getApps, pinApp, addApp })(Apps);