-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateProjectModal.tsx
More file actions
109 lines (102 loc) · 4.33 KB
/
CreateProjectModal.tsx
File metadata and controls
109 lines (102 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Textarea } from "../../../components/ui/textarea";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Label } from "@/components/ui/label";
import { useAuth } from "@/features/Login/context/AuthContext";
import { projectService } from "@/features/project/services/projectService";
import { Loader2 } from "lucide-react";
interface CreateProjectModalProps {
isOpen: boolean;
onClose: () => void;
onSuccess: () => void;
}
export function CreateProjectModal({ isOpen, onClose, onSuccess }: CreateProjectModalProps) {
const { user } = useAuth();
const [name, setName] = useState("");
const [desc, setDesc] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError(null);
if (!user) {
setError("User not found");
return;
}
setIsLoading(true);
try {
await projectService.createProject({
name,
desc,
admin_id: user.id
});
onSuccess();
onClose();
// Reset form
setName("");
setDesc("");
} catch (err: any) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setError((err as any).response?.data?.message || "Failed to create project");
} finally {
setIsLoading(false);
}
};
return (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Create New Project</DialogTitle>
<DialogDescription>
Enter the details for your new project. Click create when you're done.
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSubmit}>
<div className="grid gap-4 py-4">
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="name" className="text-right">
Name
</Label>
<Input
id="name"
value={name}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setName(e.target.value)}
className="col-span-3"
placeholder="My Awesome Project"
required
/>
</div>
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="desc" className="text-right">
Description
</Label>
<Textarea
id="desc"
value={desc}
onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => setDesc(e.target.value)}
className="col-span-3"
placeholder="A brief description..."
/>
</div>
{error && (
<div className="text-red-500 text-sm text-center">
{error}
</div>
)}
</div>
<DialogFooter>
<Button type="button" variant="outline" onClick={onClose} disabled={isLoading}>
Cancel
</Button>
<Button type="submit" disabled={isLoading}>
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Create Project
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}