Files
ims/components/capa/capa-owner-notes-form.tsx

57 lines
1.7 KiB
TypeScript

'use client'
import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { Field, Textarea } from '@/components/ui/field'
interface Props {
capaId: string
initialNotes: string | null
}
export function CapaOwnerNotesForm({ capaId, initialNotes }: Props) {
const [notes, setNotes] = useState(initialNotes ?? '')
const [saving, setSaving] = useState(false)
const [saved, setSaved] = useState(false)
const [error, setError] = useState<string | null>(null)
async function handleSave() {
setSaving(true)
setSaved(false)
setError(null)
const res = await fetch(`/api/capa/${capaId}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ owner_notes: notes }),
})
setSaving(false)
if (!res.ok) {
setError('Failed to save. Please try again.')
return
}
setSaved(true)
setTimeout(() => setSaved(false), 2000)
}
return (
<div className="mt-3 space-y-2">
<Field label="Remarks / Notes" htmlFor={`notes-${capaId}`}>
<Textarea
id={`notes-${capaId}`}
value={notes}
onChange={e => { setNotes(e.target.value); setSaved(false) }}
rows={2}
placeholder="Add any notes or updates for this action…"
/>
</Field>
{error && <p role="alert" className="text-xs text-danger">{error}</p>}
<div className="flex items-center gap-2">
<Button variant="secondary" onClick={handleSave} disabled={saving}>
{saving ? 'Saving…' : 'Save Notes'}
</Button>
{saved && <span className="text-xs text-st-green-fg">Saved</span>}
</div>
</div>
)
}