|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { useFocusTrap } from "../hooks/useFocusTrap"; |
| 3 | +import styles from "./AcceptDialog.module.css"; |
| 4 | + |
| 5 | +export interface AcceptStampInput { |
| 6 | + quality: number; |
| 7 | + reliability?: number; |
| 8 | + severity?: string; |
| 9 | + message?: string; |
| 10 | +} |
| 11 | + |
| 12 | +interface AcceptDialogProps { |
| 13 | + label: string; |
| 14 | + submitting: boolean; |
| 15 | + onCancel: () => void; |
| 16 | + onSubmit: (stamp: AcceptStampInput) => Promise<void>; |
| 17 | +} |
| 18 | + |
| 19 | +export function AcceptDialog({ label, submitting, onCancel, onSubmit }: AcceptDialogProps) { |
| 20 | + const trapRef = useFocusTrap(true); |
| 21 | + const titleId = "accept-submission-title"; |
| 22 | + const [quality, setQuality] = useState("5"); |
| 23 | + const [reliability, setReliability] = useState(""); |
| 24 | + const [severity, setSeverity] = useState("leaf"); |
| 25 | + const [message, setMessage] = useState(""); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + const handler = (e: KeyboardEvent) => { |
| 29 | + if (e.key === "Escape" && !submitting) onCancel(); |
| 30 | + }; |
| 31 | + window.addEventListener("keydown", handler); |
| 32 | + return () => window.removeEventListener("keydown", handler); |
| 33 | + }, [onCancel, submitting]); |
| 34 | + |
| 35 | + const submit = async () => { |
| 36 | + await onSubmit({ |
| 37 | + quality: Number(quality), |
| 38 | + reliability: reliability ? Number(reliability) : undefined, |
| 39 | + severity, |
| 40 | + message: message.trim() || undefined, |
| 41 | + }); |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <div className={styles.overlay} onClick={() => !submitting && onCancel()}> |
| 46 | + <div |
| 47 | + ref={trapRef} |
| 48 | + className={styles.dialog} |
| 49 | + role="dialog" |
| 50 | + aria-modal="true" |
| 51 | + aria-labelledby={titleId} |
| 52 | + onClick={(e) => e.stopPropagation()} |
| 53 | + > |
| 54 | + <h3 id={titleId} className={styles.title}> |
| 55 | + Accept Submission |
| 56 | + </h3> |
| 57 | + <p className={styles.message}>Add a stamp for {label} before marking it complete.</p> |
| 58 | + |
| 59 | + <label className={styles.field}> |
| 60 | + <span className={styles.label}>Quality</span> |
| 61 | + <select |
| 62 | + className={styles.select} |
| 63 | + value={quality} |
| 64 | + onChange={(e) => setQuality(e.target.value)} |
| 65 | + disabled={submitting} |
| 66 | + > |
| 67 | + {[1, 2, 3, 4, 5].map((value) => ( |
| 68 | + <option key={value} value={value}> |
| 69 | + {value} |
| 70 | + </option> |
| 71 | + ))} |
| 72 | + </select> |
| 73 | + </label> |
| 74 | + |
| 75 | + <label className={styles.field}> |
| 76 | + <span className={styles.label}>Reliability</span> |
| 77 | + <select |
| 78 | + className={styles.select} |
| 79 | + value={reliability} |
| 80 | + onChange={(e) => setReliability(e.target.value)} |
| 81 | + disabled={submitting} |
| 82 | + > |
| 83 | + <option value="">Match quality</option> |
| 84 | + {[1, 2, 3, 4, 5].map((value) => ( |
| 85 | + <option key={value} value={value}> |
| 86 | + {value} |
| 87 | + </option> |
| 88 | + ))} |
| 89 | + </select> |
| 90 | + </label> |
| 91 | + |
| 92 | + <label className={styles.field}> |
| 93 | + <span className={styles.label}>Severity</span> |
| 94 | + <select |
| 95 | + className={styles.select} |
| 96 | + value={severity} |
| 97 | + onChange={(e) => setSeverity(e.target.value)} |
| 98 | + disabled={submitting} |
| 99 | + > |
| 100 | + <option value="leaf">Leaf</option> |
| 101 | + <option value="branch">Branch</option> |
| 102 | + <option value="root">Root</option> |
| 103 | + </select> |
| 104 | + </label> |
| 105 | + |
| 106 | + <label className={styles.field}> |
| 107 | + <span className={styles.label}>Message</span> |
| 108 | + <textarea |
| 109 | + className={styles.textarea} |
| 110 | + value={message} |
| 111 | + onChange={(e) => setMessage(e.target.value)} |
| 112 | + placeholder="Optional feedback for the completion" |
| 113 | + disabled={submitting} |
| 114 | + rows={3} |
| 115 | + /> |
| 116 | + </label> |
| 117 | + |
| 118 | + <div className={styles.actions}> |
| 119 | + <button type="button" className={styles.cancelBtn} onClick={onCancel} disabled={submitting}> |
| 120 | + Cancel |
| 121 | + </button> |
| 122 | + <button type="button" className={styles.confirmBtn} onClick={submit} disabled={submitting}> |
| 123 | + {submitting ? "Accepting..." : "Accept"} |
| 124 | + </button> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + ); |
| 129 | +} |
0 commit comments