From cbde13314b29c0001e2598e760c35361bb60a983 Mon Sep 17 00:00:00 2001 From: Pijus Kamandulis Date: Sun, 6 Oct 2024 17:50:44 +0300 Subject: [PATCH] Create new sessions using Drawer form instead of separate page --- src/components/Drawer.tsx | 52 +++++++++++++++ src/components/Input.tsx | 4 +- .../forms/CreateEstimationSessionForm.tsx} | 28 +++++--- src/components/index.ts | 12 +++- src/main.tsx | 8 --- src/pages/Home.tsx | 22 ++++--- src/pages/Login.tsx | 64 +++++++++---------- 7 files changed, 128 insertions(+), 62 deletions(-) create mode 100644 src/components/Drawer.tsx rename src/{pages/CreateEstimationSession.tsx => components/forms/CreateEstimationSessionForm.tsx} (56%) diff --git a/src/components/Drawer.tsx b/src/components/Drawer.tsx new file mode 100644 index 0000000..5f3609f --- /dev/null +++ b/src/components/Drawer.tsx @@ -0,0 +1,52 @@ +import React, { useEffect } from 'react'; +import classNames from 'classnames'; +import Button, { ButtonColor } from './Button'; + +interface DrawerProps { + isOpen: boolean; + onClose: () => void; + children: React.ReactNode; +} + +const Drawer: React.FC = ({ isOpen, onClose, children }) => { + useEffect(() => { + const handleEsc = (event: KeyboardEvent) => { + if (event.key === 'Escape') { + onClose(); + } + }; + window.addEventListener('keydown', handleEsc); + return () => { + window.removeEventListener('keydown', handleEsc); + }; + }, [onClose]); + + if (!isOpen) return null; + + return ( +
+
+ +
+ {children} + + +
+
+ ); +}; + +export default Drawer; diff --git a/src/components/Input.tsx b/src/components/Input.tsx index 3e58e38..d7a30db 100644 --- a/src/components/Input.tsx +++ b/src/components/Input.tsx @@ -8,7 +8,7 @@ const Input = ({ label?: string; }) => { return ( - <> +
{label && ( )} @@ -18,7 +18,7 @@ const Input = ({ {...props} />
- +
); }; diff --git a/src/pages/CreateEstimationSession.tsx b/src/components/forms/CreateEstimationSessionForm.tsx similarity index 56% rename from src/pages/CreateEstimationSession.tsx rename to src/components/forms/CreateEstimationSessionForm.tsx index e19866b..9faaf0d 100644 --- a/src/pages/CreateEstimationSession.tsx +++ b/src/components/forms/CreateEstimationSessionForm.tsx @@ -1,8 +1,16 @@ import { useForm } from '@tanstack/react-form'; -import { useEstimationSessions } from '../lib/context/estimationSession'; -import { useUser } from '../lib/context/user'; +import { useEstimationSessions } from '../../lib/context/estimationSession'; +import { useUser } from '../../lib/context/user'; +import Input from '../Input'; +import Button from '../Button'; -const CreateEstimationSession = () => { +interface CreateEstimationSessionFormProps { + onCreated: () => void; +} + +const CreateEstimationSessionForm: React.FC< + CreateEstimationSessionFormProps +> = ({ onCreated }) => { const user = useUser(); const estimationSessions = useEstimationSessions(); const form = useForm({ @@ -14,13 +22,17 @@ const CreateEstimationSession = () => { Name: value.name, UserId: user.current?.$id, }); + onCreated(); }, }); return ( <> -

Create Estimation Session

+

+ Create a New Estimation Session +

{ e.preventDefault(); e.stopPropagation(); @@ -30,8 +42,8 @@ const CreateEstimationSession = () => { ( - { /> )} /> - + ); }; -export default CreateEstimationSession; +export default CreateEstimationSessionForm; diff --git a/src/components/index.ts b/src/components/index.ts index 614e837..74f6990 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -2,5 +2,15 @@ import Input from './Input'; import Button, { ButtonColor } from './Button'; import GridList from './GridList'; import Card from './Card'; +import Drawer from './Drawer'; +import CreateEstimationSessionForm from './forms/CreateEstimationSessionForm'; -export { Input, Button, ButtonColor, GridList, Card }; +export { + Input, + Button, + ButtonColor, + GridList, + Card, + Drawer, + CreateEstimationSessionForm, +}; diff --git a/src/main.tsx b/src/main.tsx index aa72c55..5343a71 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -11,7 +11,6 @@ import Home from './pages/Home'; import { UserProvider } from './lib/context/user'; import Login from './pages/Login'; import EstimationSession from './pages/EstimationSession'; -import CreateEstimationSession from './pages/CreateEstimationSession'; import { EstimationSessionProvider } from './lib/context/estimationSession'; const rootRoute = createRootRoute(); @@ -28,12 +27,6 @@ const loginRoute = createRoute({ getParentRoute: () => rootRoute, }); -const createEstimationSessionRoute = createRoute({ - path: 'estimate/new', - component: CreateEstimationSession, - getParentRoute: () => rootRoute, -}); - const estimationSessionRoute = createRoute({ path: 'estimate/session/$sessionId', component: EstimationSession, @@ -44,7 +37,6 @@ const router = createRouter({ routeTree: rootRoute.addChildren([ indexRoute, loginRoute, - createEstimationSessionRoute, estimationSessionRoute, ]), }); diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index 8276677..08ac1bc 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -4,7 +4,13 @@ import './Home.css'; import { getRouteApi, Link } from '@tanstack/react-router'; import { useUser } from '../lib/context/user'; import { useEstimationSessions } from '../lib/context/estimationSession'; -import { Card, GridList } from '../components'; +import { + Card, + CreateEstimationSessionForm, + Drawer, + GridList, +} from '../components'; +import { useState } from 'react'; const route = getRouteApi('/'); @@ -12,6 +18,7 @@ function Home() { const user = useUser(); const navigate = route.useNavigate(); const estimationSessions = useEstimationSessions(); + const [isDrawerOpen, setDrawerOpen] = useState(false); return ( <> @@ -29,9 +36,6 @@ function Home() {
  • Login
  • -
  • - Create Estimation Session -
  • User Id: {user.current?.$id}
    @@ -53,13 +57,13 @@ function Home() { }} /> )} - onAddItem={() => - navigate({ - to: '/estimate/new', - }) - } + onAddItem={() => setDrawerOpen(true)} /> + + setDrawerOpen(false)}> + setDrawerOpen(false)} /> + ); } diff --git a/src/pages/Login.tsx b/src/pages/Login.tsx index aa0e116..d43987d 100644 --- a/src/pages/Login.tsx +++ b/src/pages/Login.tsx @@ -25,41 +25,37 @@ const Login = () => {
    -
    - ( - field.handleChange(e.target.value)} - /> - )} - /> -
    + ( + field.handleChange(e.target.value)} + /> + )} + /> -
    - ( - field.handleChange(e.target.value)} - /> - )} - /> -
    + ( + field.handleChange(e.target.value)} + /> + )} + />