File size: 1,381 Bytes
3eb5a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from "react";
import type { Priority, Task, TaskPatch } from "../types";
import { Modal } from "./Modal";

export function EditTaskDialog({
  task,
  onSave,
  onClose,
}: {
  task: Task;
  onSave: (patch: TaskPatch) => Promise<void>;
  onClose: () => void;
}) {
  const [title, setTitle] = useState(task.title);
  const [priority, setPriority] = useState<Priority>(task.priority);
  const [saving, setSaving] = useState(false);

  const save = async () => {
    setSaving(true);
    try {
      await onSave({ title: title.trim(), priority });
    } finally {
      setSaving(false);
    }
  };

  return (
    <Modal title="Edit task" onClose={onClose}>
      <label className="field">
        Title
        <input value={title} onChange={(e) => setTitle(e.target.value)} autoFocus />
      </label>
      <label className="field">
        Priority
        <select value={priority} onChange={(e) => setPriority(e.target.value as Priority)}>
          <option value="low">Low</option>
          <option value="medium">Medium</option>
          <option value="high">High</option>
        </select>
      </label>
      <div className="actions">
        <button onClick={onClose}>Cancel</button>
        <button className="primary" onClick={() => void save()} disabled={!title.trim() || saving}>
          Save
        </button>
      </div>
    </Modal>
  );
}