Create new sessions using Drawer form instead of separate page

This commit is contained in:
Pijus Kamandulis 2024-10-06 17:50:44 +03:00
parent 0c158a4b22
commit cbde13314b
7 changed files with 128 additions and 62 deletions

52
src/components/Drawer.tsx Normal file
View File

@ -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<DrawerProps> = ({ 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 (
<div className="fixed inset-0 z-50 flex items-center justify-end">
<div
className="fixed inset-0 bg-black bg-opacity-50 backdrop-blur-sm"
onClick={onClose}
/>
<div
className={classNames(
'dark:bg-nero-900 relative h-full w-80 transform space-y-6 bg-white p-6 shadow-lg transition-transform',
{
'translate-x-0': isOpen,
'translate-x-full': !isOpen,
},
)}
>
{children}
<Button onClick={onClose} color={ButtonColor.Secondary}>
Cancel
</Button>
</div>
</div>
);
};
export default Drawer;

View File

@ -8,7 +8,7 @@ const Input = ({
label?: string; label?: string;
}) => { }) => {
return ( return (
<> <div>
{label && ( {label && (
<label className="block text-sm font-medium leading-6">{label}</label> <label className="block text-sm font-medium leading-6">{label}</label>
)} )}
@ -18,7 +18,7 @@ const Input = ({
{...props} {...props}
/> />
</div> </div>
</> </div>
); );
}; };

View File

@ -1,8 +1,16 @@
import { useForm } from '@tanstack/react-form'; import { useForm } from '@tanstack/react-form';
import { useEstimationSessions } from '../lib/context/estimationSession'; import { useEstimationSessions } from '../../lib/context/estimationSession';
import { useUser } from '../lib/context/user'; 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 user = useUser();
const estimationSessions = useEstimationSessions(); const estimationSessions = useEstimationSessions();
const form = useForm({ const form = useForm({
@ -14,13 +22,17 @@ const CreateEstimationSession = () => {
Name: value.name, Name: value.name,
UserId: user.current?.$id, UserId: user.current?.$id,
}); });
onCreated();
}, },
}); });
return ( return (
<> <>
<h1>Create Estimation Session</h1> <h2 className="mb-4 text-xl font-bold">
Create a New Estimation Session
</h2>
<form <form
className="space-y-6"
onSubmit={(e) => { onSubmit={(e) => {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
@ -30,8 +42,8 @@ const CreateEstimationSession = () => {
<form.Field <form.Field
name="name" name="name"
children={(field) => ( children={(field) => (
<input <Input
placeholder="Name" label="Name"
name={field.name} name={field.name}
value={field.state.value} value={field.state.value}
onBlur={field.handleBlur} onBlur={field.handleBlur}
@ -39,10 +51,10 @@ const CreateEstimationSession = () => {
/> />
)} )}
/> />
<button type="submit">Submit</button> <Button type="submit">Create</Button>
</form> </form>
</> </>
); );
}; };
export default CreateEstimationSession; export default CreateEstimationSessionForm;

View File

@ -2,5 +2,15 @@ import Input from './Input';
import Button, { ButtonColor } from './Button'; import Button, { ButtonColor } from './Button';
import GridList from './GridList'; import GridList from './GridList';
import Card from './Card'; 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,
};

View File

@ -11,7 +11,6 @@ import Home from './pages/Home';
import { UserProvider } from './lib/context/user'; import { UserProvider } from './lib/context/user';
import Login from './pages/Login'; import Login from './pages/Login';
import EstimationSession from './pages/EstimationSession'; import EstimationSession from './pages/EstimationSession';
import CreateEstimationSession from './pages/CreateEstimationSession';
import { EstimationSessionProvider } from './lib/context/estimationSession'; import { EstimationSessionProvider } from './lib/context/estimationSession';
const rootRoute = createRootRoute(); const rootRoute = createRootRoute();
@ -28,12 +27,6 @@ const loginRoute = createRoute({
getParentRoute: () => rootRoute, getParentRoute: () => rootRoute,
}); });
const createEstimationSessionRoute = createRoute({
path: 'estimate/new',
component: CreateEstimationSession,
getParentRoute: () => rootRoute,
});
const estimationSessionRoute = createRoute({ const estimationSessionRoute = createRoute({
path: 'estimate/session/$sessionId', path: 'estimate/session/$sessionId',
component: EstimationSession, component: EstimationSession,
@ -44,7 +37,6 @@ const router = createRouter({
routeTree: rootRoute.addChildren([ routeTree: rootRoute.addChildren([
indexRoute, indexRoute,
loginRoute, loginRoute,
createEstimationSessionRoute,
estimationSessionRoute, estimationSessionRoute,
]), ]),
}); });

View File

@ -4,7 +4,13 @@ import './Home.css';
import { getRouteApi, Link } from '@tanstack/react-router'; import { getRouteApi, Link } from '@tanstack/react-router';
import { useUser } from '../lib/context/user'; import { useUser } from '../lib/context/user';
import { useEstimationSessions } from '../lib/context/estimationSession'; 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('/'); const route = getRouteApi('/');
@ -12,6 +18,7 @@ function Home() {
const user = useUser(); const user = useUser();
const navigate = route.useNavigate(); const navigate = route.useNavigate();
const estimationSessions = useEstimationSessions(); const estimationSessions = useEstimationSessions();
const [isDrawerOpen, setDrawerOpen] = useState(false);
return ( return (
<> <>
@ -29,9 +36,6 @@ function Home() {
<li> <li>
<Link to="/login">Login</Link> <Link to="/login">Login</Link>
</li> </li>
<li>
<Link to="/estimate/new">Create Estimation Session</Link>
</li>
</ul> </ul>
<pre>User Id: {user.current?.$id}</pre> <pre>User Id: {user.current?.$id}</pre>
@ -53,13 +57,13 @@ function Home() {
}} }}
/> />
)} )}
onAddItem={() => onAddItem={() => setDrawerOpen(true)}
navigate({
to: '/estimate/new',
})
}
/> />
</div> </div>
<Drawer isOpen={isDrawerOpen} onClose={() => setDrawerOpen(false)}>
<CreateEstimationSessionForm onCreated={() => setDrawerOpen(false)} />
</Drawer>
</> </>
); );
} }

View File

@ -25,7 +25,6 @@ const Login = () => {
<div className="mt-10 sm:mx-auto sm:w-full sm:max-w-sm"> <div className="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
<form className="space-y-6"> <form className="space-y-6">
<div>
<form.Field <form.Field
name="email" name="email"
children={(field) => ( children={(field) => (
@ -41,9 +40,7 @@ const Login = () => {
/> />
)} )}
/> />
</div>
<div>
<form.Field <form.Field
name="password" name="password"
children={(field) => ( children={(field) => (
@ -59,7 +56,6 @@ const Login = () => {
/> />
)} )}
/> />
</div>
<div> <div>
<div className="flex items-center justify-between gap-4"> <div className="flex items-center justify-between gap-4">