ericchang00's picture
add screenshot42-taskmanager-projectdetail task (build 2ecba08)
160f5ce verified
Raw History Blame Contribute Delete
6.71 kB
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">
&larr; 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>
);
}