|
| 1 | +import { useState, useId } from 'react' |
| 2 | +import { X, Save, Plus, Trash2, Loader2, AlertCircle } from 'lucide-react' |
| 3 | +import { useUpdateFeature } from '../hooks/useProjects' |
| 4 | +import type { Feature } from '../lib/types' |
| 5 | + |
| 6 | +interface Step { |
| 7 | + id: string |
| 8 | + value: string |
| 9 | +} |
| 10 | + |
| 11 | +interface EditFeatureFormProps { |
| 12 | + feature: Feature |
| 13 | + projectName: string |
| 14 | + onClose: () => void |
| 15 | + onSaved: () => void |
| 16 | +} |
| 17 | + |
| 18 | +export function EditFeatureForm({ feature, projectName, onClose, onSaved }: EditFeatureFormProps) { |
| 19 | + const formId = useId() |
| 20 | + const [category, setCategory] = useState(feature.category) |
| 21 | + const [name, setName] = useState(feature.name) |
| 22 | + const [description, setDescription] = useState(feature.description) |
| 23 | + const [priority, setPriority] = useState(String(feature.priority)) |
| 24 | + const [steps, setSteps] = useState<Step[]>(() => |
| 25 | + feature.steps.length > 0 |
| 26 | + ? feature.steps.map((step, i) => ({ id: `${formId}-step-${i}`, value: step })) |
| 27 | + : [{ id: `${formId}-step-0`, value: '' }] |
| 28 | + ) |
| 29 | + const [error, setError] = useState<string | null>(null) |
| 30 | + const [stepCounter, setStepCounter] = useState(feature.steps.length || 1) |
| 31 | + |
| 32 | + const updateFeature = useUpdateFeature(projectName) |
| 33 | + |
| 34 | + const handleAddStep = () => { |
| 35 | + setSteps([...steps, { id: `${formId}-step-${stepCounter}`, value: '' }]) |
| 36 | + setStepCounter(stepCounter + 1) |
| 37 | + } |
| 38 | + |
| 39 | + const handleRemoveStep = (id: string) => { |
| 40 | + setSteps(steps.filter(step => step.id !== id)) |
| 41 | + } |
| 42 | + |
| 43 | + const handleStepChange = (id: string, value: string) => { |
| 44 | + setSteps(steps.map(step => |
| 45 | + step.id === id ? { ...step, value } : step |
| 46 | + )) |
| 47 | + } |
| 48 | + |
| 49 | + const handleSubmit = async (e: React.FormEvent) => { |
| 50 | + e.preventDefault() |
| 51 | + setError(null) |
| 52 | + |
| 53 | + const filteredSteps = steps |
| 54 | + .map(s => s.value.trim()) |
| 55 | + .filter(s => s.length > 0) |
| 56 | + |
| 57 | + try { |
| 58 | + await updateFeature.mutateAsync({ |
| 59 | + featureId: feature.id, |
| 60 | + update: { |
| 61 | + category: category.trim(), |
| 62 | + name: name.trim(), |
| 63 | + description: description.trim(), |
| 64 | + steps: filteredSteps, |
| 65 | + priority: parseInt(priority, 10), |
| 66 | + }, |
| 67 | + }) |
| 68 | + onSaved() |
| 69 | + } catch (err) { |
| 70 | + setError(err instanceof Error ? err.message : 'Failed to update feature') |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + const isValid = category.trim() && name.trim() && description.trim() |
| 75 | + |
| 76 | + // Check if any changes were made |
| 77 | + const currentSteps = steps.map(s => s.value.trim()).filter(s => s) |
| 78 | + const hasChanges = |
| 79 | + category.trim() !== feature.category || |
| 80 | + name.trim() !== feature.name || |
| 81 | + description.trim() !== feature.description || |
| 82 | + parseInt(priority, 10) !== feature.priority || |
| 83 | + JSON.stringify(currentSteps) !== JSON.stringify(feature.steps) |
| 84 | + |
| 85 | + return ( |
| 86 | + <div className="neo-modal-backdrop" onClick={onClose}> |
| 87 | + <div |
| 88 | + className="neo-modal w-full max-w-2xl" |
| 89 | + onClick={(e) => e.stopPropagation()} |
| 90 | + > |
| 91 | + {/* Header */} |
| 92 | + <div className="flex items-center justify-between p-6 border-b-3 border-[var(--color-neo-border)]"> |
| 93 | + <h2 className="font-display text-2xl font-bold"> |
| 94 | + Edit Feature |
| 95 | + </h2> |
| 96 | + <button |
| 97 | + onClick={onClose} |
| 98 | + className="neo-btn neo-btn-ghost p-2" |
| 99 | + > |
| 100 | + <X size={24} /> |
| 101 | + </button> |
| 102 | + </div> |
| 103 | + |
| 104 | + {/* Form */} |
| 105 | + <form onSubmit={handleSubmit} className="p-6 space-y-4"> |
| 106 | + {/* Error Message */} |
| 107 | + {error && ( |
| 108 | + <div className="flex items-center gap-3 p-4 bg-[var(--color-neo-danger)] text-white border-3 border-[var(--color-neo-border)]"> |
| 109 | + <AlertCircle size={20} /> |
| 110 | + <span>{error}</span> |
| 111 | + <button |
| 112 | + type="button" |
| 113 | + onClick={() => setError(null)} |
| 114 | + className="ml-auto" |
| 115 | + > |
| 116 | + <X size={16} /> |
| 117 | + </button> |
| 118 | + </div> |
| 119 | + )} |
| 120 | + |
| 121 | + {/* Category & Priority Row */} |
| 122 | + <div className="flex gap-4"> |
| 123 | + <div className="flex-1"> |
| 124 | + <label className="block font-display font-bold mb-2 uppercase text-sm"> |
| 125 | + Category |
| 126 | + </label> |
| 127 | + <input |
| 128 | + type="text" |
| 129 | + value={category} |
| 130 | + onChange={(e) => setCategory(e.target.value)} |
| 131 | + placeholder="e.g., Authentication, UI, API" |
| 132 | + className="neo-input" |
| 133 | + required |
| 134 | + /> |
| 135 | + </div> |
| 136 | + <div className="w-32"> |
| 137 | + <label className="block font-display font-bold mb-2 uppercase text-sm"> |
| 138 | + Priority |
| 139 | + </label> |
| 140 | + <input |
| 141 | + type="number" |
| 142 | + value={priority} |
| 143 | + onChange={(e) => setPriority(e.target.value)} |
| 144 | + min="1" |
| 145 | + className="neo-input" |
| 146 | + required |
| 147 | + /> |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + |
| 151 | + {/* Name */} |
| 152 | + <div> |
| 153 | + <label className="block font-display font-bold mb-2 uppercase text-sm"> |
| 154 | + Feature Name |
| 155 | + </label> |
| 156 | + <input |
| 157 | + type="text" |
| 158 | + value={name} |
| 159 | + onChange={(e) => setName(e.target.value)} |
| 160 | + placeholder="e.g., User login form" |
| 161 | + className="neo-input" |
| 162 | + required |
| 163 | + /> |
| 164 | + </div> |
| 165 | + |
| 166 | + {/* Description */} |
| 167 | + <div> |
| 168 | + <label className="block font-display font-bold mb-2 uppercase text-sm"> |
| 169 | + Description |
| 170 | + </label> |
| 171 | + <textarea |
| 172 | + value={description} |
| 173 | + onChange={(e) => setDescription(e.target.value)} |
| 174 | + placeholder="Describe what this feature should do..." |
| 175 | + className="neo-input min-h-[100px] resize-y" |
| 176 | + required |
| 177 | + /> |
| 178 | + </div> |
| 179 | + |
| 180 | + {/* Steps */} |
| 181 | + <div> |
| 182 | + <label className="block font-display font-bold mb-2 uppercase text-sm"> |
| 183 | + Test Steps |
| 184 | + </label> |
| 185 | + <div className="space-y-2"> |
| 186 | + {steps.map((step, index) => ( |
| 187 | + <div key={step.id} className="flex gap-2"> |
| 188 | + <span className="neo-input w-12 text-center flex-shrink-0 flex items-center justify-center"> |
| 189 | + {index + 1} |
| 190 | + </span> |
| 191 | + <input |
| 192 | + type="text" |
| 193 | + value={step.value} |
| 194 | + onChange={(e) => handleStepChange(step.id, e.target.value)} |
| 195 | + placeholder="Describe this step..." |
| 196 | + className="neo-input flex-1" |
| 197 | + /> |
| 198 | + {steps.length > 1 && ( |
| 199 | + <button |
| 200 | + type="button" |
| 201 | + onClick={() => handleRemoveStep(step.id)} |
| 202 | + className="neo-btn neo-btn-ghost p-2" |
| 203 | + > |
| 204 | + <Trash2 size={18} /> |
| 205 | + </button> |
| 206 | + )} |
| 207 | + </div> |
| 208 | + ))} |
| 209 | + </div> |
| 210 | + <button |
| 211 | + type="button" |
| 212 | + onClick={handleAddStep} |
| 213 | + className="neo-btn neo-btn-ghost mt-2 text-sm" |
| 214 | + > |
| 215 | + <Plus size={16} /> |
| 216 | + Add Step |
| 217 | + </button> |
| 218 | + </div> |
| 219 | + |
| 220 | + {/* Actions */} |
| 221 | + <div className="flex gap-3 pt-4 border-t-3 border-[var(--color-neo-border)]"> |
| 222 | + <button |
| 223 | + type="submit" |
| 224 | + disabled={!isValid || !hasChanges || updateFeature.isPending} |
| 225 | + className="neo-btn neo-btn-success flex-1" |
| 226 | + > |
| 227 | + {updateFeature.isPending ? ( |
| 228 | + <Loader2 size={18} className="animate-spin" /> |
| 229 | + ) : ( |
| 230 | + <> |
| 231 | + <Save size={18} /> |
| 232 | + Save Changes |
| 233 | + </> |
| 234 | + )} |
| 235 | + </button> |
| 236 | + <button |
| 237 | + type="button" |
| 238 | + onClick={onClose} |
| 239 | + className="neo-btn neo-btn-ghost" |
| 240 | + > |
| 241 | + Cancel |
| 242 | + </button> |
| 243 | + </div> |
| 244 | + </form> |
| 245 | + </div> |
| 246 | + </div> |
| 247 | + ) |
| 248 | +} |
0 commit comments