|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useEffect } from "react"; |
| 4 | +import Modal from "@/app/components/Modal"; |
| 5 | +import { Button, Field } from "@/app/components"; |
| 6 | +import { |
| 7 | + EditProjectModalProps, |
| 8 | + ProjectResponse, |
| 9 | +} from "@/app/lib/types/onboarding"; |
| 10 | +import { apiFetch } from "@/app/lib/apiClient"; |
| 11 | +import { useToast } from "@/app/components/Toast"; |
| 12 | + |
| 13 | +export default function EditProjectModal({ |
| 14 | + open, |
| 15 | + onClose, |
| 16 | + project, |
| 17 | + apiKey, |
| 18 | + onProjectUpdated, |
| 19 | +}: EditProjectModalProps) { |
| 20 | + const toast = useToast(); |
| 21 | + const [name, setName] = useState(project.name); |
| 22 | + const [description, setDescription] = useState(project.description); |
| 23 | + const [isActive, setIsActive] = useState(project.is_active); |
| 24 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 25 | + const [nameError, setNameError] = useState(""); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + setName(project.name); |
| 29 | + setDescription(project.description); |
| 30 | + setIsActive(project.is_active); |
| 31 | + setNameError(""); |
| 32 | + }, [project]); |
| 33 | + |
| 34 | + const handleClose = () => { |
| 35 | + setName(project.name); |
| 36 | + setDescription(project.description); |
| 37 | + setIsActive(project.is_active); |
| 38 | + setNameError(""); |
| 39 | + onClose(); |
| 40 | + }; |
| 41 | + |
| 42 | + const handleSubmit = async () => { |
| 43 | + if (!name.trim()) { |
| 44 | + setNameError("Project name is required"); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + setNameError(""); |
| 49 | + setIsSubmitting(true); |
| 50 | + |
| 51 | + try { |
| 52 | + await apiFetch<ProjectResponse>(`/api/projects/${project.id}`, apiKey, { |
| 53 | + method: "PATCH", |
| 54 | + body: JSON.stringify({ |
| 55 | + name: name.trim(), |
| 56 | + description: description.trim(), |
| 57 | + is_active: isActive, |
| 58 | + }), |
| 59 | + }); |
| 60 | + |
| 61 | + toast.success("Project updated"); |
| 62 | + onProjectUpdated(); |
| 63 | + onClose(); |
| 64 | + } catch (err) { |
| 65 | + toast.error( |
| 66 | + err instanceof Error ? err.message : "Failed to update project", |
| 67 | + ); |
| 68 | + } finally { |
| 69 | + setIsSubmitting(false); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + return ( |
| 74 | + <Modal |
| 75 | + open={open} |
| 76 | + onClose={handleClose} |
| 77 | + title="Edit Project" |
| 78 | + maxWidth="max-w-md" |
| 79 | + > |
| 80 | + <div className="px-6 pb-6 space-y-4"> |
| 81 | + <Field |
| 82 | + label="Name *" |
| 83 | + value={name} |
| 84 | + onChange={(val) => { |
| 85 | + setName(val); |
| 86 | + if (nameError) setNameError(""); |
| 87 | + }} |
| 88 | + placeholder="Enter project name" |
| 89 | + error={nameError} |
| 90 | + /> |
| 91 | + |
| 92 | + <Field |
| 93 | + label="Description" |
| 94 | + value={description} |
| 95 | + onChange={setDescription} |
| 96 | + placeholder="Enter project description (optional)" |
| 97 | + /> |
| 98 | + |
| 99 | + <div> |
| 100 | + <label className="flex items-center gap-2.5 cursor-pointer"> |
| 101 | + <input |
| 102 | + type="checkbox" |
| 103 | + checked={isActive} |
| 104 | + onChange={(e) => setIsActive(e.target.checked)} |
| 105 | + className="w-4 h-4 rounded" |
| 106 | + /> |
| 107 | + <span className="text-sm text-text-primary">Active</span> |
| 108 | + </label> |
| 109 | + <p className="text-xs text-text-secondary mt-1 ml-6.5"> |
| 110 | + Inactive projects won't be available for use. |
| 111 | + </p> |
| 112 | + </div> |
| 113 | + |
| 114 | + <div className="flex items-center justify-end gap-2 pt-4 border-t border-border"> |
| 115 | + <Button variant="outline" size="md" onClick={handleClose}> |
| 116 | + Cancel |
| 117 | + </Button> |
| 118 | + <Button |
| 119 | + onClick={handleSubmit} |
| 120 | + disabled={isSubmitting || !name.trim()} |
| 121 | + size="md" |
| 122 | + > |
| 123 | + {isSubmitting ? "Saving..." : "Save Changes"} |
| 124 | + </Button> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </Modal> |
| 128 | + ); |
| 129 | +} |
0 commit comments