Files
colanode/apps/desktop/src/renderer/components/spaces/space-create-dialog.tsx

141 lines
3.8 KiB
TypeScript
Raw Normal View History

import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
2024-08-04 11:16:51 +02:00
DialogTitle,
} from '@/renderer/components/ui/dialog';
2024-08-04 11:16:51 +02:00
import { z } from 'zod';
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/renderer/components/ui/form';
import { Input } from '@/renderer/components/ui/input';
import { Textarea } from '@/renderer/components/ui/textarea';
import { Button } from '@/renderer/components/ui/button';
import { Spinner } from '@/renderer/components/ui/spinner';
2024-08-04 11:16:51 +02:00
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
2024-10-03 17:34:11 +02:00
import { useMutation } from '@/renderer/hooks/use-mutation';
2024-10-03 17:35:28 +02:00
import { useWorkspace } from '@/renderer/contexts/workspace';
const formSchema = z.object({
name: z.string().min(3, 'Name must be at least 3 characters long.'),
description: z.string(),
});
2024-08-01 16:17:59 +02:00
interface SpaceCreateDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
2024-08-01 16:17:59 +02:00
}
export const SpaceCreateDialog = ({
2024-08-04 11:16:51 +02:00
open,
onOpenChange,
2024-08-01 16:17:59 +02:00
}: SpaceCreateDialogProps) => {
2024-10-03 17:11:39 +02:00
const workspace = useWorkspace();
const { mutate, isPending } = useMutation();
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
name: '',
description: '',
},
});
2024-08-01 16:30:10 +02:00
const handleCancel = () => {
form.reset();
onOpenChange(false);
2024-08-04 11:16:51 +02:00
};
2024-08-01 16:30:10 +02:00
const handleSubmit = async (values: z.infer<typeof formSchema>) => {
2024-10-26 19:09:45 +02:00
if (isPending) {
return;
}
if (values.name.length < 3) {
return;
}
2024-10-03 17:11:39 +02:00
mutate({
input: {
type: 'space_create',
name: values.name,
description: values.description,
userId: workspace.userId,
workspaceId: workspace.id,
2024-08-25 02:11:59 +02:00
},
2024-10-03 17:11:39 +02:00
onSuccess() {
onOpenChange(false);
form.reset();
},
});
2024-08-04 11:16:51 +02:00
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent>
<DialogHeader>
<DialogTitle>Create space</DialogTitle>
<DialogDescription>
Create a new space to collaborate with your peers
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form
className="flex flex-col"
onSubmit={form.handleSubmit(handleSubmit)}
>
<div className="flex-grow space-y-4 py-2 pb-4">
<FormField
control={form.control}
name="name"
2024-08-04 11:16:51 +02:00
render={({ field }) => (
<FormItem className="flex-1">
<FormLabel>Name *</FormLabel>
<FormControl>
<Input placeholder="Name" {...field} />
</FormControl>
2024-08-04 11:16:51 +02:00
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="description"
2024-08-04 11:16:51 +02:00
render={({ field }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
<Textarea
placeholder="Write a short description about the workspace"
{...field}
/>
</FormControl>
2024-08-04 11:16:51 +02:00
<FormMessage />
</FormItem>
)}
/>
</div>
<DialogFooter>
2024-10-26 19:09:45 +02:00
<Button type="button" variant="outline" onClick={handleCancel}>
Cancel
</Button>
2024-08-02 08:43:44 +02:00
<Button type="submit" disabled={isPending}>
{isPending && <Spinner className="mr-1" />}
Create
</Button>
</DialogFooter>
</form>
</Form>
</DialogContent>
</Dialog>
);
2024-08-04 11:16:51 +02:00
};