mirror of
https://github.com/colanode/colanode.git
synced 2025-12-23 15:09:27 +01:00
Add foreign key constraints for nodes
This commit is contained in:
@@ -1,23 +1,30 @@
|
||||
import React from "react";
|
||||
import {useWorkspace} from "@/contexts/workspace";
|
||||
import React from 'react';
|
||||
import { useWorkspace } from '@/contexts/workspace';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle
|
||||
} from "@/components/ui/dialog";
|
||||
import {z} from "zod";
|
||||
import {Form, FormControl, FormField, FormItem, FormLabel, FormMessage} from "@/components/ui/form";
|
||||
import {Input} from "@/components/ui/input";
|
||||
import {Textarea} from "@/components/ui/textarea";
|
||||
import {Button} from "@/components/ui/button";
|
||||
import {Spinner} from "@/components/ui/spinner";
|
||||
import {useForm} from "react-hook-form";
|
||||
import {zodResolver} from "@hookform/resolvers/zod";
|
||||
import {Node} from "@/types/nodes";
|
||||
import {NeuronId} from "@/lib/id";
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog';
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from '@/components/ui/form';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Spinner } from '@/components/ui/spinner';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { Node } from '@/types/nodes';
|
||||
import { NeuronId } from '@/lib/id';
|
||||
|
||||
const formSchema = z.object({
|
||||
name: z.string().min(3, 'Name must be at least 3 characters long.'),
|
||||
@@ -30,8 +37,8 @@ interface SpaceCreateDialogProps {
|
||||
}
|
||||
|
||||
export const SpaceCreateDialog = ({
|
||||
open,
|
||||
onOpenChange,
|
||||
open,
|
||||
onOpenChange,
|
||||
}: SpaceCreateDialogProps) => {
|
||||
const workspace = useWorkspace();
|
||||
const [isPending, setIsPending] = React.useState(false);
|
||||
@@ -46,7 +53,7 @@ export const SpaceCreateDialog = ({
|
||||
const handleCancel = () => {
|
||||
form.reset();
|
||||
onOpenChange(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async (values: z.infer<typeof formSchema>) => {
|
||||
setIsPending(true);
|
||||
@@ -56,31 +63,32 @@ export const SpaceCreateDialog = ({
|
||||
id: NeuronId.generate(NeuronId.Type.Page),
|
||||
type: 'page',
|
||||
attrs: {
|
||||
name: 'Home'
|
||||
name: 'Home',
|
||||
},
|
||||
workspaceId: workspace.id,
|
||||
parentId: spaceId,
|
||||
createdAt: new Date(),
|
||||
createdBy: workspace.userNodeId,
|
||||
versionId: NeuronId.generate(NeuronId.Type.Version)
|
||||
versionId: NeuronId.generate(NeuronId.Type.Version),
|
||||
};
|
||||
|
||||
const channelNode: Node = {
|
||||
id: NeuronId.generate(NeuronId.Type.Channel),
|
||||
type: 'channel',
|
||||
attrs: {
|
||||
name: 'Discussions'
|
||||
name: 'Discussions',
|
||||
},
|
||||
workspaceId: workspace.id,
|
||||
parentId: spaceId,
|
||||
createdAt: new Date(),
|
||||
createdBy: workspace.userNodeId,
|
||||
versionId: NeuronId.generate(NeuronId.Type.Version)
|
||||
versionId: NeuronId.generate(NeuronId.Type.Version),
|
||||
};
|
||||
|
||||
const spaceNode: Node = {
|
||||
id: NeuronId.generate(NeuronId.Type.Space),
|
||||
id: spaceId,
|
||||
type: 'space',
|
||||
parentId: null,
|
||||
attrs: {
|
||||
name: values.name,
|
||||
description: values.description,
|
||||
@@ -88,23 +96,23 @@ export const SpaceCreateDialog = ({
|
||||
content: [
|
||||
{
|
||||
type: 'page',
|
||||
id: pageNode.id
|
||||
id: pageNode.id,
|
||||
},
|
||||
{
|
||||
type: 'channel',
|
||||
id: channelNode.id
|
||||
}
|
||||
id: channelNode.id,
|
||||
},
|
||||
],
|
||||
workspaceId: workspace.id,
|
||||
createdAt: new Date(),
|
||||
createdBy: workspace.userNodeId,
|
||||
versionId: NeuronId.generate(NeuronId.Type.Version)
|
||||
versionId: NeuronId.generate(NeuronId.Type.Version),
|
||||
};
|
||||
|
||||
await workspace.addNodes([spaceNode, pageNode, channelNode]);
|
||||
setIsPending(false);
|
||||
onOpenChange(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
@@ -124,20 +132,20 @@ export const SpaceCreateDialog = ({
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
render={({field}) => (
|
||||
render={({ field }) => (
|
||||
<FormItem className="flex-1">
|
||||
<FormLabel>Name *</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="Name" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage/>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="description"
|
||||
render={({field}) => (
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Description</FormLabel>
|
||||
<FormControl>
|
||||
@@ -146,7 +154,7 @@ export const SpaceCreateDialog = ({
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage/>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
@@ -165,4 +173,4 @@ export const SpaceCreateDialog = ({
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user