|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { StyledInput } from "@/components/ui/styled-input" |
| 4 | + |
| 5 | +export interface AnnouncementFormData { |
| 6 | + title: string |
| 7 | + content: string |
| 8 | + priority: number |
| 9 | + isActive: boolean |
| 10 | + expiresAt: string |
| 11 | +} |
| 12 | + |
| 13 | +interface AnnouncementFormModalProps { |
| 14 | + isOpen: boolean |
| 15 | + isEditing: boolean |
| 16 | + formData: AnnouncementFormData |
| 17 | + submitting: boolean |
| 18 | + onFormChange: (data: AnnouncementFormData) => void |
| 19 | + onSubmit: (e: React.FormEvent) => void |
| 20 | + onClose: () => void |
| 21 | +} |
| 22 | + |
| 23 | +export function AnnouncementFormModal({ |
| 24 | + isOpen, |
| 25 | + isEditing, |
| 26 | + formData, |
| 27 | + submitting, |
| 28 | + onFormChange, |
| 29 | + onSubmit, |
| 30 | + onClose, |
| 31 | +}: AnnouncementFormModalProps) { |
| 32 | + if (!isOpen) return null |
| 33 | + |
| 34 | + return ( |
| 35 | + <div |
| 36 | + className="fixed inset-0 bg-black/50 backdrop-blur-sm flex items-center justify-center z-50 p-4" |
| 37 | + role="presentation" |
| 38 | + onClick={onClose} |
| 39 | + > |
| 40 | + <div |
| 41 | + className="bg-slate-900 border border-slate-700 rounded-xl w-full max-w-lg shadow-2xl max-h-[90vh] overflow-y-auto" |
| 42 | + role="dialog" |
| 43 | + aria-modal="true" |
| 44 | + aria-labelledby="modal-title" |
| 45 | + onClick={(e) => e.stopPropagation()} |
| 46 | + > |
| 47 | + {/* Modal Header */} |
| 48 | + <div className="flex items-center justify-between p-4 border-b border-slate-700"> |
| 49 | + <h2 id="modal-title" className="text-lg font-semibold text-white"> |
| 50 | + {isEditing ? "Edit Announcement" : "New Announcement"} |
| 51 | + </h2> |
| 52 | + <button |
| 53 | + onClick={onClose} |
| 54 | + className="p-1 text-slate-400 hover:text-white rounded-lg hover:bg-slate-800 transition-colors" |
| 55 | + data-testid="announcement-modal-close" |
| 56 | + > |
| 57 | + <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 58 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" /> |
| 59 | + </svg> |
| 60 | + </button> |
| 61 | + </div> |
| 62 | + |
| 63 | + {/* Modal Body */} |
| 64 | + <form onSubmit={onSubmit} className="p-4 space-y-4"> |
| 65 | + {/* Title */} |
| 66 | + <div> |
| 67 | + <label htmlFor="title" className="block text-sm font-medium text-slate-300 mb-1"> |
| 68 | + Title |
| 69 | + </label> |
| 70 | + <StyledInput |
| 71 | + id="title" |
| 72 | + name="title" |
| 73 | + value={formData.title} |
| 74 | + onChange={(e) => onFormChange({ ...formData, title: e.target.value })} |
| 75 | + placeholder="Announcement title" |
| 76 | + required |
| 77 | + data-testid="announcement-title-input" |
| 78 | + /> |
| 79 | + </div> |
| 80 | + |
| 81 | + {/* Content */} |
| 82 | + <div> |
| 83 | + <label htmlFor="content" className="block text-sm font-medium text-slate-300 mb-1"> |
| 84 | + Content (Markdown supported) |
| 85 | + </label> |
| 86 | + <textarea |
| 87 | + id="content" |
| 88 | + name="content" |
| 89 | + value={formData.content} |
| 90 | + onChange={(e) => onFormChange({ ...formData, content: e.target.value })} |
| 91 | + placeholder="Announcement content... You can use **bold**, *italic*, and [links](url)" |
| 92 | + required |
| 93 | + rows={5} |
| 94 | + className="w-full bg-slate-800/50 border border-slate-600 hover:border-slate-500 rounded-lg px-4 py-2 text-sm text-white placeholder-slate-400 focus:outline-none focus:border-cyan-400 focus:ring-cyan-400 focus:ring-1 transition-colors resize-none" |
| 95 | + data-testid="announcement-content-input" |
| 96 | + /> |
| 97 | + </div> |
| 98 | + |
| 99 | + {/* Priority and Active Row */} |
| 100 | + <div className="grid grid-cols-2 gap-4"> |
| 101 | + <div> |
| 102 | + <label htmlFor="priority" className="block text-sm font-medium text-slate-300 mb-1"> |
| 103 | + Priority |
| 104 | + </label> |
| 105 | + <StyledInput |
| 106 | + id="priority" |
| 107 | + name="priority" |
| 108 | + type="number" |
| 109 | + min={0} |
| 110 | + max={100} |
| 111 | + value={formData.priority} |
| 112 | + onChange={(e) => onFormChange({ ...formData, priority: parseInt(e.target.value) || 0 })} |
| 113 | + data-testid="announcement-priority-input" |
| 114 | + /> |
| 115 | + <p className="text-xs text-slate-500 mt-1">Higher = shown first</p> |
| 116 | + </div> |
| 117 | + <div> |
| 118 | + <label className="block text-sm font-medium text-slate-300 mb-1">Status</label> |
| 119 | + <label className="flex items-center gap-2 cursor-pointer mt-2"> |
| 120 | + <input |
| 121 | + type="checkbox" |
| 122 | + checked={formData.isActive} |
| 123 | + onChange={(e) => onFormChange({ ...formData, isActive: e.target.checked })} |
| 124 | + className="w-4 h-4 rounded border-slate-600 bg-slate-800 text-cyan-500 focus:ring-cyan-500 focus:ring-offset-0" |
| 125 | + data-testid="announcement-active-checkbox" |
| 126 | + /> |
| 127 | + <span className="text-sm text-slate-300">Active</span> |
| 128 | + </label> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + |
| 132 | + {/* Expiration Date */} |
| 133 | + <div> |
| 134 | + <label htmlFor="expiresAt" className="block text-sm font-medium text-slate-300 mb-1"> |
| 135 | + Expiration Date (Optional) |
| 136 | + </label> |
| 137 | + <StyledInput |
| 138 | + id="expiresAt" |
| 139 | + name="expiresAt" |
| 140 | + type="datetime-local" |
| 141 | + value={formData.expiresAt} |
| 142 | + onChange={(e) => onFormChange({ ...formData, expiresAt: e.target.value })} |
| 143 | + data-testid="announcement-expires-input" |
| 144 | + /> |
| 145 | + <p className="text-xs text-slate-500 mt-1">Leave empty for no expiration</p> |
| 146 | + </div> |
| 147 | + |
| 148 | + {/* Modal Footer */} |
| 149 | + <div className="flex items-center justify-end gap-3 pt-4 border-t border-slate-700"> |
| 150 | + <button |
| 151 | + type="button" |
| 152 | + onClick={onClose} |
| 153 | + className="px-4 py-2 text-sm font-medium text-slate-300 hover:text-white bg-slate-800 hover:bg-slate-700 border border-slate-600 rounded-lg transition-colors" |
| 154 | + > |
| 155 | + Cancel |
| 156 | + </button> |
| 157 | + <button |
| 158 | + type="submit" |
| 159 | + disabled={submitting} |
| 160 | + className="px-4 py-2 text-sm font-medium text-white bg-cyan-600 hover:bg-cyan-700 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed inline-flex items-center gap-2" |
| 161 | + data-testid="announcement-submit-button" |
| 162 | + > |
| 163 | + {submitting && ( |
| 164 | + <svg className="animate-spin h-4 w-4" fill="none" viewBox="0 0 24 24"> |
| 165 | + <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" /> |
| 166 | + <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" /> |
| 167 | + </svg> |
| 168 | + )} |
| 169 | + {isEditing ? "Save Changes" : "Create"} |
| 170 | + </button> |
| 171 | + </div> |
| 172 | + </form> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + ) |
| 176 | +} |
0 commit comments