import { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; // Redux import { useDispatch, useSelector } from 'react-redux'; // Typescript import { App } from '../../interfaces'; // CSS import classes from './Apps.module.css'; // UI import { Headline, Spinner, ActionButton, Modal, Container } from '../UI'; // Subcomponents import { AppGrid } from './AppGrid/AppGrid'; import { AppForm } from './AppForm/AppForm'; import { AppTable } from './AppTable/AppTable'; // Utils import { appTemplate } from '../../utility'; import { State } from '../../store/reducers'; import { bindActionCreators } from 'redux'; import { actionCreators } from '../../store'; interface Props { searching: boolean; } export const Apps = (props: Props): JSX.Element => { const { apps: { apps, loading }, auth: { isAuthenticated }, } = useSelector((state: State) => state); const dispatch = useDispatch(); const { getApps } = bindActionCreators(actionCreators, dispatch); const [modalIsOpen, setModalIsOpen] = useState(false); const [isInEdit, setIsInEdit] = useState(false); const [isInUpdate, setIsInUpdate] = useState(false); const [appInUpdate, setAppInUpdate] = useState(appTemplate); useEffect(() => { if (!apps.length) { getApps(); } }, []); // observe if user is authenticated -> set default view if not useEffect(() => { if (!isAuthenticated) { setIsInEdit(false); setModalIsOpen(false); } }, [isAuthenticated]); const toggleModal = (): void => { setModalIsOpen(!modalIsOpen); setIsInUpdate(false); }; const toggleEdit = (): void => { setIsInEdit(!isInEdit); setIsInUpdate(false); }; const toggleUpdate = (app: App): void => { setAppInUpdate(app); setIsInUpdate(true); setModalIsOpen(true); }; return ( {!isInUpdate ? ( ) : ( )} Go back} /> {isAuthenticated && (
)}
{loading ? ( ) : !isInEdit ? ( ) : ( )}
); };