-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskForm.js
More file actions
84 lines (79 loc) · 2.61 KB
/
Copy pathTaskForm.js
File metadata and controls
84 lines (79 loc) · 2.61 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
import React, { useState } from 'react';
export default function TaskForm({ task, onSave, onCancel }) {
const [form, setForm] = useState({
title: task?.title || '',
description: task?.description || '',
priority: task?.priority || 'medium',
dueDate: task?.dueDate ? task.dueDate.slice(0, 10) : '',
tags: task?.tags?.join(', ') || '',
});
const [saving, setSaving] = useState(false);
const [err, setErr] = useState('');
const set = (key) => (e) => setForm((f) => ({ ...f, [key]: e.target.value }));
const handleSubmit = async (e) => {
e.preventDefault();
if (!form.title.trim()) { setErr('Title is required'); return; }
setSaving(true);
setErr('');
try {
const payload = {
title: form.title.trim(),
description: form.description.trim(),
priority: form.priority,
dueDate: form.dueDate || null,
tags: form.tags ? form.tags.split(',').map((t) => t.trim()).filter(Boolean) : [],
};
await onSave(payload);
} catch (error) {
setErr(error.message);
} finally {
setSaving(false);
}
};
return (
<form className="task-form" onSubmit={handleSubmit}>
<div className="form-title">{task ? 'Edit Task' : 'New Task'}</div>
{err && <div className="form-error">{err}</div>}
<input
className="form-input"
placeholder="Task title *"
value={form.title}
onChange={set('title')}
autoFocus
/>
<textarea
className="form-input form-textarea"
placeholder="Description (optional)"
value={form.description}
onChange={set('description')}
rows={2}
/>
<div className="form-row">
<select className="form-input form-select" value={form.priority} onChange={set('priority')}>
<option value="low">◇ Low Priority</option>
<option value="medium">◈ Medium Priority</option>
<option value="high">⚑ High Priority</option>
</select>
<input
className="form-input"
type="date"
value={form.dueDate}
onChange={set('dueDate')}
placeholder="Due date"
/>
</div>
<input
className="form-input"
placeholder="Tags (comma separated)"
value={form.tags}
onChange={set('tags')}
/>
<div className="form-actions">
<button type="button" className="btn-cancel" onClick={onCancel}>Cancel</button>
<button type="submit" className="btn-save" disabled={saving}>
{saving ? 'Saving…' : task ? 'Update Task' : 'Create Task'}
</button>
</div>
</form>
);
}