feat: Implement edit mode detection in SelectedItemsDetailInputComponent
- Added logic to detect edit mode based on URL parameters and existing data IDs. - Enhanced value retrieval for form fields to prioritize original data in edit mode, ensuring accurate updates. - Removed redundant edit mode detection comments to streamline the code and improve clarity.
This commit is contained in:
parent
36bc33860f
commit
d686c385e0
|
|
@ -568,6 +568,12 @@ export const SelectedItemsDetailInputComponent: React.FC<SelectedItemsDetailInpu
|
||||||
if (hasParentMapping) {
|
if (hasParentMapping) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
// 수정 모드 감지 (parentKeys 구성 전에 필요)
|
||||||
|
const urlParams = typeof window !== "undefined" ? new URLSearchParams(window.location.search) : null;
|
||||||
|
const urlEditMode = urlParams?.get("mode") === "edit";
|
||||||
|
const dataHasDbId = items.some(item => !!item.originalData?.id);
|
||||||
|
const isEditMode = urlEditMode || dataHasDbId;
|
||||||
|
|
||||||
// 부모 키 추출 (parentDataMapping에서)
|
// 부모 키 추출 (parentDataMapping에서)
|
||||||
const parentKeys: Record<string, any> = {};
|
const parentKeys: Record<string, any> = {};
|
||||||
|
|
||||||
|
|
@ -581,11 +587,19 @@ export const SelectedItemsDetailInputComponent: React.FC<SelectedItemsDetailInpu
|
||||||
}
|
}
|
||||||
|
|
||||||
componentConfig.parentDataMapping.forEach((mapping) => {
|
componentConfig.parentDataMapping.forEach((mapping) => {
|
||||||
// 1차: formData(sourceData)에서 찾기
|
let value: any;
|
||||||
let value = getFieldValue(sourceData, mapping.sourceField);
|
|
||||||
|
// 수정 모드: originalData의 targetField 값 우선 사용
|
||||||
|
// 로드(editFilters)와 동일한 방식으로 FK 값을 가져와야
|
||||||
|
// 백엔드에서 기존 레코드를 정확히 매칭하여 UPDATE 수행 가능
|
||||||
|
if (isEditMode && items.length > 0 && items[0].originalData) {
|
||||||
|
value = items[0].originalData[mapping.targetField];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 신규 모드 또는 originalData에 값 없으면 기존 로직
|
||||||
|
if (value === undefined || value === null) {
|
||||||
|
value = getFieldValue(sourceData, mapping.sourceField);
|
||||||
|
|
||||||
// 2차: formData에 없으면 dataRegistry[sourceTable]에서 찾기
|
|
||||||
// v2-split-panel-layout에서 좌측 항목 선택 시 dataRegistry에 저장한 데이터 활용
|
|
||||||
if ((value === undefined || value === null) && mapping.sourceTable) {
|
if ((value === undefined || value === null) && mapping.sourceTable) {
|
||||||
const registryData = dataRegistry[mapping.sourceTable];
|
const registryData = dataRegistry[mapping.sourceTable];
|
||||||
if (registryData && registryData.length > 0) {
|
if (registryData && registryData.length > 0) {
|
||||||
|
|
@ -593,6 +607,7 @@ export const SelectedItemsDetailInputComponent: React.FC<SelectedItemsDetailInpu
|
||||||
value = registryItem[mapping.sourceField];
|
value = registryItem[mapping.sourceField];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (value !== undefined && value !== null) {
|
if (value !== undefined && value !== null) {
|
||||||
parentKeys[mapping.targetField] = value;
|
parentKeys[mapping.targetField] = value;
|
||||||
|
|
@ -646,15 +661,6 @@ export const SelectedItemsDetailInputComponent: React.FC<SelectedItemsDetailInpu
|
||||||
const additionalFields = componentConfig.additionalFields || [];
|
const additionalFields = componentConfig.additionalFields || [];
|
||||||
const mainTable = componentConfig.targetTable!;
|
const mainTable = componentConfig.targetTable!;
|
||||||
|
|
||||||
// 수정 모드 감지 (2가지 방법으로 확인)
|
|
||||||
// 1. URL에 mode=edit 파라미터 확인
|
|
||||||
// 2. 로드된 데이터에 DB id(PK)가 존재하는지 확인
|
|
||||||
// 수정 모드에서는 항상 deleteOrphans=true (기존 레코드 교체, 복제 방지)
|
|
||||||
const urlParams = typeof window !== "undefined" ? new URLSearchParams(window.location.search) : null;
|
|
||||||
const urlEditMode = urlParams?.get("mode") === "edit";
|
|
||||||
const dataHasDbId = items.some(item => !!item.originalData?.id);
|
|
||||||
const isEditMode = urlEditMode || dataHasDbId;
|
|
||||||
|
|
||||||
console.log("[SelectedItemsDetailInput] 수정 모드 감지:", {
|
console.log("[SelectedItemsDetailInput] 수정 모드 감지:", {
|
||||||
urlEditMode,
|
urlEditMode,
|
||||||
dataHasDbId,
|
dataHasDbId,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue