260115_03_ENH : 템플릿 저장/수정 전 데이터 검증 추가
This commit is contained in:
@@ -21,10 +21,11 @@ import {Input} from "@/components/ui/input";
|
||||
import {Checkbox} from "@/components/ui/checkbox";
|
||||
import {KriEditor, KriEditorCommand} from "@/components/kri-editor";
|
||||
import {Button} from "@/components/ui/button";
|
||||
import {cn} from "@/lib/utils";
|
||||
import {cn, isBlank} from "@/lib/utils";
|
||||
import _ from "lodash";
|
||||
import {ProjectMainTemplateUpdateRequest} from "@/types/project";
|
||||
import {toast} from "sonner";
|
||||
import { DataVerification } from "@/types/common";
|
||||
|
||||
interface MessageTemplateProps {
|
||||
pageTitle: string;
|
||||
@@ -103,41 +104,70 @@ const MessageTemplate = ({
|
||||
};
|
||||
|
||||
const handleCreate = async () => {
|
||||
//Todo 데이터 검증 필요
|
||||
|
||||
nextApi.put<Template>(`/message-template`, selectedTemplate)
|
||||
.then((res) => {
|
||||
settingTemplateList();
|
||||
|
||||
setSelectedTemplate(res);
|
||||
updateMainTemplate(res.tmplatIdx!);
|
||||
|
||||
alert("저장되었습니다.");
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log(e);
|
||||
alert("저장에 실패했습니다.");
|
||||
});
|
||||
await submitTemplate("put");
|
||||
};
|
||||
|
||||
const handleUpdate = async () => {
|
||||
if(!selectedTemplate.tmplatIdx) return;
|
||||
if (!selectedTemplate.tmplatIdx) return;
|
||||
await submitTemplate("patch");
|
||||
};
|
||||
|
||||
//Todo 데이터 검증 필요
|
||||
const submitTemplate = async (method: "put" | "patch") => {
|
||||
const v = validateTemplate(selectedTemplate);
|
||||
|
||||
nextApi.patch<Template>(`/message-template`, selectedTemplate)
|
||||
.then((res) => {
|
||||
settingTemplateList();
|
||||
if (!v.success) {
|
||||
alert(v.errMessage);
|
||||
// 필요 시: 특정 필드 focus 처리도 가능
|
||||
// focusField(v.fieldName);
|
||||
return;
|
||||
}
|
||||
|
||||
setSelectedTemplate(res);
|
||||
updateMainTemplate(res.tmplatIdx!);
|
||||
try {
|
||||
const res =
|
||||
method === "put"
|
||||
? await nextApi.put<Template>(`/message-template`, selectedTemplate)
|
||||
: await nextApi.patch<Template>(`/message-template`, selectedTemplate);
|
||||
|
||||
alert("수정되었습니다.");
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log(e);
|
||||
alert("저장에 실패했습니다.");
|
||||
});
|
||||
settingTemplateList();
|
||||
setSelectedTemplate(res);
|
||||
updateMainTemplate(res.tmplatIdx!);
|
||||
|
||||
alert(method === "put" ? "저장되었습니다." : "수정되었습니다.");
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
alert(method === "put" ? "저장에 실패했습니다." : "수정에 실패했습니다.");
|
||||
}
|
||||
};
|
||||
|
||||
const validateTemplate = (tpl: Template): DataVerification => {
|
||||
const ok = (): DataVerification => ({
|
||||
success: true,
|
||||
fieldName: "",
|
||||
errMessage: "",
|
||||
});
|
||||
|
||||
const fail = (fieldName: string, errMessage: string): DataVerification => ({
|
||||
success: false,
|
||||
fieldName,
|
||||
errMessage,
|
||||
});
|
||||
|
||||
if (isBlank(tpl.itrntPrjctCd)) return fail("itrntPrjctCd", "인트라넷 프로젝트 코드가 없습니다.");
|
||||
if (isBlank(tpl.tmplatNm)) return fail("tmplatNm", "템플릿명을 입력해 주세요.");
|
||||
if (isBlank(tpl.tmplatBody)) return fail("tmplatBody", "본문을 입력해 주세요.");
|
||||
if (isBlank(tpl.sendNm)) return fail("sendNm", "발신자명을 입력해 주세요.");
|
||||
|
||||
// Email 전용
|
||||
if (tpl.tmplatTy === MessageType.EMAIL) {
|
||||
if (isBlank(tpl.tmplatTitle)) return fail("tmplatTitle", "이메일 제목을 입력해 주세요.");
|
||||
if (isBlank(tpl.sendEml)) return fail("sendEml", "발신 이메일을 입력해 주세요.");
|
||||
return ok();
|
||||
}
|
||||
|
||||
// SMS 전용
|
||||
if (isBlank(tpl.sendTel)) return fail("sendTel", "발신 전화번호를 입력해 주세요.");
|
||||
|
||||
return ok();
|
||||
};
|
||||
|
||||
const updateMainTemplate = (templatIdx:number):void => {
|
||||
|
||||
Reference in New Issue
Block a user