-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathCreateTaskDialog.jsx
More file actions
181 lines (159 loc) · 8.9 KB
/
Copy pathCreateTaskDialog.jsx
File metadata and controls
181 lines (159 loc) · 8.9 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { useState } from "react";
import { Calendar as CalendarIcon } from "lucide-react";
import { useDispatch, useSelector } from "react-redux";
import toast from "react-hot-toast";
import { addTask } from "../features/workspaceSlice";
import { format } from "date-fns";
export default function CreateTaskDialog({ showCreateTask, setShowCreateTask, projectId }) {
const currentWorkspace = useSelector((state) => state.workspace?.currentWorkspace || null);
const dispatch = useDispatch();
const project = currentWorkspace?.projects.find((p) => p.id === projectId);
const teamMembers = project?.members || [];
const [isSubmitting, setIsSubmitting] = useState(false);
const [formData, setFormData] = useState({
title: "",
description: "",
type: "TASK",
status: "TODO",
priority: "MEDIUM",
assigneeId: "",
due_date: "",
});
const handleSubmit = async (e) => {
e.preventDefault();
if (!formData.title.trim()) {
toast.error("Task title is required");
return;
}
if (!formData.due_date) {
toast.error("Due date is required");
return;
}
try {
setIsSubmitting(true);
const selectedAssignee =
teamMembers.find(
(member) => member?.user?.id === formData.assigneeId
)?.user || null;
const now = new Date().toISOString();
const newTask = {
id: crypto.randomUUID(),
projectId,
title: formData.title.trim(),
description: formData.description.trim(),
type: formData.type,
status: formData.status,
priority: formData.priority,
assigneeId: formData.assigneeId || null,
due_date: new Date(
`${formData.due_date}T00:00:00`
).toISOString(),
createdAt: now,
updatedAt: now,
assignee: selectedAssignee,
comments: [],
};
dispatch(addTask(newTask));
toast.success("Task created successfully");
setFormData({
title: "",
description: "",
type: "TASK",
status: "TODO",
priority: "MEDIUM",
assigneeId: "",
due_date: "",
});
setShowCreateTask(false);
} catch (error) {
console.error("Failed to create task:", error);
toast.error("Unable to create task");
} finally {
setIsSubmitting(false);
}
};
return showCreateTask ? (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/20 dark:bg-black/60 backdrop-blur">
<div className="bg-white dark:bg-zinc-950 border border-zinc-300 dark:border-zinc-800 rounded-lg shadow-lg w-full max-w-md p-6 text-zinc-900 dark:text-white">
<h2 className="text-xl font-bold mb-4">Create New Task</h2>
<form onSubmit={handleSubmit} className="space-y-4">
{/* Title */}
<div className="space-y-1">
<label htmlFor="title" className="text-sm font-medium">Title</label>
<input value={formData.title} onChange={(e) => setFormData({ ...formData, title: e.target.value })} placeholder="Task title" className="w-full rounded dark:bg-zinc-900 border border-zinc-300 dark:border-zinc-700 px-3 py-2 text-zinc-900 dark:text-zinc-200 text-sm mt-1 focus:outline-none focus:ring-2 focus:ring-blue-500" required />
</div>
{/* Description */}
<div className="space-y-1">
<label htmlFor="description" className="text-sm font-medium">Description</label>
<textarea value={formData.description} onChange={(e) => setFormData({ ...formData, description: e.target.value })} placeholder="Describe the task" className="w-full rounded dark:bg-zinc-900 border border-zinc-300 dark:border-zinc-700 px-3 py-2 text-zinc-900 dark:text-zinc-200 text-sm mt-1 h-24 focus:outline-none focus:ring-2 focus:ring-blue-500" />
</div>
{/* Type & Priority */}
<div className="grid grid-cols-2 gap-4">
<div className="space-y-1">
<label className="text-sm font-medium">Type</label>
<select value={formData.type} onChange={(e) => setFormData({ ...formData, type: e.target.value })} className="w-full rounded dark:bg-zinc-900 border border-zinc-300 dark:border-zinc-700 px-3 py-2 text-zinc-900 dark:text-zinc-200 text-sm mt-1" >
<option value="BUG">Bug</option>
<option value="FEATURE">Feature</option>
<option value="TASK">Task</option>
<option value="IMPROVEMENT">Improvement</option>
<option value="OTHER">Other</option>
</select>
</div>
<div className="space-y-1">
<label className="text-sm font-medium">Priority</label>
<select value={formData.priority} onChange={(e) => setFormData({ ...formData, priority: e.target.value })} className="w-full rounded dark:bg-zinc-900 border border-zinc-300 dark:border-zinc-700 px-3 py-2 text-zinc-900 dark:text-zinc-200 text-sm mt-1" >
<option value="LOW">Low</option>
<option value="MEDIUM">Medium</option>
<option value="HIGH">High</option>
</select>
</div>
</div>
{/* Assignee and Status */}
<div className="grid grid-cols-2 gap-4">
<div className="space-y-1">
<label className="text-sm font-medium">Assignee</label>
<select value={formData.assigneeId} onChange={(e) => setFormData({ ...formData, assigneeId: e.target.value })} className="w-full rounded dark:bg-zinc-900 border border-zinc-300 dark:border-zinc-700 px-3 py-2 text-zinc-900 dark:text-zinc-200 text-sm mt-1" >
<option value="">Unassigned</option>
{teamMembers.map((member) => (
<option key={member?.user.id} value={member?.user.id}>
{member?.user.email}
</option>
))}
</select>
</div>
<div className="space-y-1">
<label className="text-sm font-medium">Status</label>
<select value={formData.status} onChange={(e) => setFormData({ ...formData, status: e.target.value })} className="w-full rounded dark:bg-zinc-900 border border-zinc-300 dark:border-zinc-700 px-3 py-2 text-zinc-900 dark:text-zinc-200 text-sm mt-1" >
<option value="TODO">To Do</option>
<option value="IN_PROGRESS">In Progress</option>
<option value="DONE">Done</option>
</select>
</div>
</div>
{/* Due Date */}
<div className="space-y-1">
<label className="text-sm font-medium">Due Date</label>
<div className="flex items-center gap-2">
<CalendarIcon className="size-5 text-zinc-500 dark:text-zinc-400" />
<input type="date" value={formData.due_date} onChange={(e) => setFormData({ ...formData, due_date: e.target.value })} min={new Date().toISOString().split('T')[0]} className="w-full rounded dark:bg-zinc-900 border border-zinc-300 dark:border-zinc-700 px-3 py-2 text-zinc-900 dark:text-zinc-200 text-sm mt-1" />
</div>
{formData.due_date && (
<p className="text-xs text-zinc-500 dark:text-zinc-400">
{format(new Date(formData.due_date), "PPP")}
</p>
)}
</div>
{/* Footer */}
<div className="flex justify-end gap-2 pt-2">
<button type="button" onClick={() => setShowCreateTask(false)} className="rounded border border-zinc-300 dark:border-zinc-700 px-5 py-2 text-sm hover:bg-zinc-100 dark:hover:bg-zinc-800 transition" >
Cancel
</button>
<button type="submit" disabled={isSubmitting} className="rounded px-5 py-2 text-sm bg-gradient-to-br from-blue-500 to-blue-600 hover:opacity-90 text-white dark:text-zinc-200 transition" >
{isSubmitting ? "Creating..." : "Create Task"}
</button>
</div>
</form>
</div>
</div>
) : null;
}