File size: 6,711 Bytes
160f5ce | 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import { useState, useEffect } from 'react';
import { useParams, useNavigate, Link } from 'react-router-dom';
import { getProjectDetail, createTask, getTags } from '../api';
export default function AddTask() {
const { projectId } = useParams();
const navigate = useNavigate();
const [project, setProject] = useState(null);
const [allTags, setAllTags] = useState([]);
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [priority, setPriority] = useState('medium');
const [assignedTo, setAssignedTo] = useState('');
const [createdBy, setCreatedBy] = useState('');
const [dueDate, setDueDate] = useState('');
const [selectedTagIds, setSelectedTagIds] = useState([]);
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);
useEffect(() => {
Promise.all([getProjectDetail(projectId), getTags()])
.then(([p, tags]) => {
setProject(p);
setAllTags(tags);
const members = p.members || [];
if (members.length > 0) setCreatedBy(String(members[0].id));
})
.catch((e) => setError(e.message));
}, [projectId]);
function toggleTag(tagId) {
setSelectedTagIds((prev) =>
prev.includes(tagId) ? prev.filter((id) => id !== tagId) : [...prev, tagId]
);
}
async function handleSubmit(e) {
e.preventDefault();
setError('');
if (!title.trim() || !createdBy) {
setError('Title and creator are required');
return;
}
setLoading(true);
try {
const data = {
title: title.trim(),
description: description.trim(),
priority,
created_by: parseInt(createdBy),
tag_ids: selectedTagIds,
};
if (assignedTo) data.assigned_to = parseInt(assignedTo);
if (dueDate) data.due_date = dueDate;
await createTask(projectId, data);
navigate(`/projects/${projectId}`);
} catch (e) {
setError(e.message);
} finally {
setLoading(false);
}
}
if (!project) {
return error ? (
<div className="error-msg">{error}</div>
) : (
<div className="loading">Loading...</div>
);
}
const members = project.members || [];
return (
<div className="page-form">
<Link to={`/projects/${projectId}`} className="back-link">
← Back to {project.name}
</Link>
<h1>Add Task</h1>
<div className="card">
<div className="card-body">
{error && <div className="error-msg">{error}</div>}
<form onSubmit={handleSubmit}>
<div className="form-group">
<label>Title</label>
<input
className="form-input"
type="text"
placeholder="e.g. Design homepage mockup"
value={title}
onChange={(e) => setTitle(e.target.value)}
required
/>
</div>
<div className="form-group">
<label>Description</label>
<textarea
className="form-input"
placeholder="Describe the task..."
value={description}
onChange={(e) => setDescription(e.target.value)}
/>
</div>
<div className="form-group">
<label>Priority</label>
<select
className="form-input"
value={priority}
onChange={(e) => setPriority(e.target.value)}
>
<option value="low">Low</option>
<option value="medium">Medium</option>
<option value="high">High</option>
</select>
</div>
<div className="form-group">
<label>Created by</label>
<select
className="form-input"
value={createdBy}
onChange={(e) => setCreatedBy(e.target.value)}
required
>
<option value="">Select creator...</option>
{members.map((m) => (
<option key={m.id} value={m.id}>
{m.name}
</option>
))}
</select>
</div>
<div className="form-group">
<label>Assigned to</label>
<select
className="form-input"
value={assignedTo}
onChange={(e) => setAssignedTo(e.target.value)}
>
<option value="">Unassigned</option>
{members.map((m) => (
<option key={m.id} value={m.id}>
{m.name}
</option>
))}
</select>
</div>
<div className="form-group">
<label>Due Date</label>
<input
className="form-input"
type="date"
value={dueDate}
onChange={(e) => setDueDate(e.target.value)}
/>
</div>
<div className="form-group">
<label>Tags</label>
{allTags.length === 0 ? (
<p style={{ color: 'var(--text-light)', fontSize: 14 }}>
No tags available. Create tags from the dashboard.
</p>
) : (
<div className="checkbox-list">
{allTags.map((t) => (
<label
key={t.id}
className={`checkbox-item ${selectedTagIds.includes(t.id) ? 'checked' : ''}`}
>
<input
type="checkbox"
checked={selectedTagIds.includes(t.id)}
onChange={() => toggleTag(t.id)}
/>
<span
className="tag-badge-sm"
style={{ backgroundColor: t.color + '22', color: t.color }}
>
{t.name}
</span>
</label>
))}
</div>
)}
</div>
<div className="form-actions">
<Link to={`/projects/${projectId}`} className="btn btn-secondary">
Cancel
</Link>
<button
type="submit"
className="btn btn-primary"
disabled={loading}
>
{loading ? 'Adding...' : 'Add Task'}
</button>
</div>
</form>
</div>
</div>
</div>
);
}
|