2022-03-25 12:13:19 +01:00
|
|
|
import { useState, useEffect } from 'react';
|
2022-03-24 16:07:14 +01:00
|
|
|
|
|
|
|
// Redux
|
2022-03-25 12:13:19 +01:00
|
|
|
import { useSelector, useDispatch } from 'react-redux';
|
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { actionCreators } from '../../../../store';
|
2022-03-24 16:07:14 +01:00
|
|
|
import { State } from '../../../../store/reducers';
|
|
|
|
|
|
|
|
// Other
|
2022-03-23 16:13:34 +01:00
|
|
|
import { Theme } from '../../../../interfaces';
|
2022-03-24 16:07:14 +01:00
|
|
|
|
|
|
|
// UI
|
|
|
|
import { Button, Modal } from '../../../UI';
|
2022-03-23 16:13:34 +01:00
|
|
|
import { ThemeGrid } from '../ThemeGrid/ThemeGrid';
|
|
|
|
import classes from './ThemeBuilder.module.css';
|
2022-03-24 16:07:14 +01:00
|
|
|
import { ThemeCreator } from './ThemeCreator';
|
|
|
|
import { ThemeEditor } from './ThemeEditor';
|
2022-03-23 16:13:34 +01:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
themes: Theme[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ThemeBuilder = ({ themes }: Props): JSX.Element => {
|
2022-03-24 16:07:14 +01:00
|
|
|
const {
|
|
|
|
auth: { isAuthenticated },
|
2022-03-25 12:13:19 +01:00
|
|
|
theme: { themeInEdit },
|
2022-03-24 16:07:14 +01:00
|
|
|
} = useSelector((state: State) => state);
|
|
|
|
|
2022-03-25 12:13:19 +01:00
|
|
|
const { editTheme } = bindActionCreators(actionCreators, useDispatch());
|
|
|
|
|
2022-03-24 16:07:14 +01:00
|
|
|
const [showModal, toggleShowModal] = useState(false);
|
|
|
|
const [isInEdit, toggleIsInEdit] = useState(false);
|
|
|
|
|
2022-03-25 12:13:19 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (themeInEdit) {
|
|
|
|
toggleIsInEdit(false);
|
|
|
|
toggleShowModal(true);
|
|
|
|
}
|
|
|
|
}, [themeInEdit]);
|
|
|
|
|
2022-03-23 16:13:34 +01:00
|
|
|
return (
|
|
|
|
<div className={classes.ThemeBuilder}>
|
2022-03-24 16:07:14 +01:00
|
|
|
{/* MODALS */}
|
2022-03-25 14:07:53 +01:00
|
|
|
<Modal
|
|
|
|
isOpen={showModal}
|
|
|
|
setIsOpen={() => toggleShowModal(!showModal)}
|
|
|
|
cb={() => editTheme(null)}
|
|
|
|
>
|
2022-03-24 16:07:14 +01:00
|
|
|
{isInEdit ? (
|
|
|
|
<ThemeEditor modalHandler={() => toggleShowModal(!showModal)} />
|
|
|
|
) : (
|
|
|
|
<ThemeCreator modalHandler={() => toggleShowModal(!showModal)} />
|
|
|
|
)}
|
|
|
|
</Modal>
|
|
|
|
|
|
|
|
{/* USER THEMES */}
|
2022-03-23 16:13:34 +01:00
|
|
|
<ThemeGrid themes={themes} />
|
|
|
|
|
2022-03-24 16:07:14 +01:00
|
|
|
{/* BUTTONS */}
|
|
|
|
{isAuthenticated && (
|
|
|
|
<div className={classes.Buttons}>
|
|
|
|
<Button
|
|
|
|
click={() => {
|
2022-03-25 12:13:19 +01:00
|
|
|
editTheme(null);
|
2022-03-24 16:07:14 +01:00
|
|
|
toggleIsInEdit(false);
|
|
|
|
toggleShowModal(!showModal);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Create new theme
|
|
|
|
</Button>
|
|
|
|
|
2022-03-25 14:07:53 +01:00
|
|
|
{themes.length ? (
|
2022-03-24 16:07:14 +01:00
|
|
|
<Button
|
|
|
|
click={() => {
|
|
|
|
toggleIsInEdit(true);
|
|
|
|
toggleShowModal(!showModal);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Edit user themes
|
|
|
|
</Button>
|
2022-03-25 14:07:53 +01:00
|
|
|
) : (
|
|
|
|
<></>
|
2022-03-24 16:07:14 +01:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
2022-03-23 16:13:34 +01:00
|
|
|
</div>
|
|
|
|
);
|
2022-03-23 13:02:32 +01:00
|
|
|
};
|