1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
"use client";
import * as React from "react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Loader2, FileText, AlertCircle } from "lucide-react";
import { toast } from "sonner";
import { Separator } from "@/components/ui/separator";
import ApprovalLineSelector, {
type ApprovalLineItem,
} from "@/components/knox/approval/ApprovalLineSelector";
import { getApprovalTemplateByName, replaceTemplateVariables } from "@/lib/approval/template-utils";
import { debugLog, debugError, debugSuccess } from "@/lib/debug-utils";
interface ApprovalPreviewDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
templateName: string;
variables: Record<string, string>;
title: string;
description?: string;
currentUser: { id: number; epId: string | null; name?: string | null; email?: string };
onSubmit: (approvers: ApprovalLineItem[]) => Promise<void>;
}
export function ApprovalPreviewDialog({
open,
onOpenChange,
templateName,
variables,
title,
description,
currentUser,
onSubmit,
}: ApprovalPreviewDialogProps) {
const [approvalLines, setApprovalLines] = React.useState<ApprovalLineItem[]>([]);
const [isSubmitting, setIsSubmitting] = React.useState(false);
const [templateContent, setTemplateContent] = React.useState<string>("");
const [isLoadingTemplate, setIsLoadingTemplate] = React.useState(false);
const [templateError, setTemplateError] = React.useState<string | null>(null);
// 상신자 초기화
React.useEffect(() => {
if (open && currentUser.epId) {
const drafterLine: ApprovalLineItem = {
id: `drafter-${Date.now()}`,
epId: currentUser.epId,
userId: currentUser.id.toString(),
emailAddress: currentUser.email,
name: currentUser.name || undefined,
role: "0", // 기안
seq: "0",
opinion: "",
};
setApprovalLines([drafterLine]);
}
}, [open, currentUser]);
// 템플릿 로드 및 변수 치환
React.useEffect(() => {
if (!open) return;
const loadTemplate = async () => {
setIsLoadingTemplate(true);
setTemplateError(null);
try {
const template = await getApprovalTemplateByName(templateName);
if (!template) {
setTemplateError(`템플릿 "${templateName}"을 찾을 수 없습니다.`);
setTemplateContent(`<p class="text-gray-500">${description || "결재 요청"}</p>`);
} else {
// 변수 치환
const replaced = await replaceTemplateVariables(template.content, variables);
setTemplateContent(replaced);
}
} catch (error) {
console.error("Template load error:", error);
setTemplateError("템플릿을 불러오는 중 오류가 발생했습니다.");
setTemplateContent(`<p class="text-gray-500">${description || "결재 요청"}</p>`);
} finally {
setIsLoadingTemplate(false);
}
};
loadTemplate();
}, [open, templateName, variables, description]);
const handleSubmit = async () => {
debugLog('[ApprovalPreviewDialog] 결재 제출 시작', {
templateName,
approvalLineCount: approvalLines.length,
});
// 결재자가 있는지 확인 (상신자 제외)
const approvers = approvalLines.filter((line) => line.seq !== "0");
if (approvers.length === 0) {
debugError('[ApprovalPreviewDialog] 결재자가 없음');
toast.error("결재자를 최소 1명 이상 추가해주세요.");
return;
}
setIsSubmitting(true);
try {
debugLog('[ApprovalPreviewDialog] onSubmit 호출', {
approversCount: approvers.length,
});
await onSubmit(approvalLines);
debugSuccess('[ApprovalPreviewDialog] 결재 요청 성공');
toast.success("결재가 성공적으로 요청되었습니다.");
onOpenChange(false);
} catch (error) {
debugError('[ApprovalPreviewDialog] 결재 요청 실패', error);
const errorMessage = error instanceof Error ? error.message : "결재 요청에 실패했습니다.";
toast.error(errorMessage);
// 에러 발생 시 dialog를 닫지 않음
} finally {
setIsSubmitting(false);
}
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-5xl max-h-[90vh] overflow-y-auto">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<FileText className="w-5 h-5" />
{title}
</DialogTitle>
{description && <DialogDescription>{description}</DialogDescription>}
</DialogHeader>
<div className="space-y-6">
{/* 템플릿 미리보기 */}
<Card>
<CardHeader>
<CardTitle>결재 내용 미리보기</CardTitle>
<CardDescription>
템플릿: <span className="font-mono text-sm">{templateName}</span>
</CardDescription>
</CardHeader>
<CardContent>
{isLoadingTemplate ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="w-6 h-6 animate-spin text-gray-400" />
<span className="ml-2 text-gray-500">템플릿을 불러오는 중...</span>
</div>
) : templateError ? (
<div className="p-4 bg-yellow-50 border border-yellow-200 rounded-lg">
<div className="flex items-center gap-2 text-yellow-700">
<AlertCircle className="w-4 h-4" />
<span className="font-medium">경고</span>
</div>
<p className="text-sm text-yellow-600 mt-1">{templateError}</p>
<p className="text-xs text-yellow-500 mt-2">
기본 내용으로 대체되었습니다. 결재는 정상적으로 진행됩니다.
</p>
</div>
) : null}
<div
className="border rounded-lg p-4 min-h-[200px] [&_table]:w-full [&_table]:border-collapse [&_table]:border [&_table]:border-border [&_th]:border [&_th]:border-border [&_th]:bg-muted/50 [&_th]:px-3 [&_th]:py-2 [&_th]:text-left [&_th]:font-semibold [&_td]:border [&_td]:border-border [&_td]:px-3 [&_td]:py-2 [&_tr:hover]:bg-muted/30"
dangerouslySetInnerHTML={{ __html: templateContent }}
/>
</CardContent>
</Card>
<Separator />
{/* 결재선 선택 */}
<div>
<ApprovalLineSelector
value={approvalLines}
onChange={setApprovalLines}
placeholder="결재자를 검색하세요..."
maxSelections={10}
/>
</div>
</div>
<DialogFooter className="gap-2">
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={isSubmitting}
>
취소
</Button>
<Button type="button" onClick={handleSubmit} disabled={isSubmitting || isLoadingTemplate}>
{isSubmitting && <Loader2 className="mr-2 w-4 h-4 animate-spin" />}
결재 요청
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
|