import { useCallback, useEffect, useState } from 'react' import { addReMemo, deleteReMemo, getReMemos, updateReMemo, type Org, type ReMemo, } from '../api/client' /** * 기관관리 상세의 RE 탭. * * 요구사항 [4]: 별도 양식 없이 메모장처럼 등록한다. 메모 1개가 RE 1개다. * 예) ooo 계약서 및 제안요청서 송부 요청, 장영익, 2026.07.08 * * 완료 체크는 회신에 없던 항목이다. 대시보드 '미해결 RE' 숫자를 세려면 해결된 건과 아닌 건을 * 가릴 수단이 필요해 한 칸 두었다. */ function today(): string { const d = new Date() const pad = (n: number) => String(n).padStart(2, '0') return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}` } export default function ReMemos({ org }: { org: Org }) { const [memos, setMemos] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [busy, setBusy] = useState(false) const [content, setContent] = useState('') const [author, setAuthor] = useState('') const [memoDate, setMemoDate] = useState(today()) const [editingId, setEditingId] = useState(null) const load = useCallback(async () => { setLoading(true) try { setMemos(await getReMemos(org.id)) setError(null) } catch (e) { setError((e as Error).message) } finally { setLoading(false) } }, [org.id]) useEffect(() => { void load() }, [load]) function resetForm() { setContent('') setAuthor('') setMemoDate(today()) setEditingId(null) } async function handleSave() { if (!content.trim() || !author.trim() || busy) { return } setBusy(true) setError(null) try { const body = { content: content.trim(), author: author.trim(), memoDate, resolved: false } const next = editingId === null ? await addReMemo(org.id, body) : await updateReMemo(org.id, editingId, { ...body, resolved: memos.find((m) => m.id === editingId)?.resolved ?? false, }) setMemos(next) resetForm() } catch (e) { setError((e as Error).message) } finally { setBusy(false) } } async function toggleResolved(memo: ReMemo) { setBusy(true) try { setMemos(await updateReMemo(org.id, memo.id, { content: memo.content, author: memo.author, memoDate: memo.memoDate, resolved: !memo.resolved, })) } catch (e) { setError((e as Error).message) } finally { setBusy(false) } } async function handleDelete(memo: ReMemo) { if (!window.confirm('이 RE를 지울까요?')) { return } setBusy(true) try { await deleteReMemo(org.id, memo.id) await load() if (editingId === memo.id) { resetForm() } } finally { setBusy(false) } } function startEdit(memo: ReMemo) { setEditingId(memo.id) setContent(memo.content) setAuthor(memo.author) setMemoDate(memo.memoDate) } const unresolved = memos.filter((m) => !m.resolved).length return (

RE 미해결 {unresolved}건 / 전체 {memos.length}건