Files
sam-react-prod/src/components/items/DynamicItemForm/hooks/useFormStructure.ts
유병철 0db6302652 refactor(WEB): 코드 품질 개선 및 불필요 코드 제거
- 미사용 import/변수/console.log 대량 정리 (100+개 파일)
- ItemMasterContext 간소화 (미사용 로직 제거)
- IntegratedListTemplateV2 / UniversalListPage 개선
- 결재 컴포넌트(ApprovalBox, DraftBox, ReferenceBox) 정리
- HR 컴포넌트(급여/휴가/부서) 코드 간소화
- globals.css 스타일 정리 및 개선
- AuthenticatedLayout 개선
- middleware CSP 정리
- proxy route 불필요 로깅 제거

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-10 20:55:11 +09:00

94 lines
2.7 KiB
TypeScript

/**
* 품목기준관리 API에서 폼 구조를 로드하는 훅
*
* - init API에서 페이지 목록 조회
* - 품목 유형(itemType)에 해당하는 페이지 찾기
* - 해당 페이지의 전체 구조(섹션, 필드) 로드
*/
'use client';
import { useState, useEffect, useCallback } from 'react';
import { itemMasterApi } from '@/lib/api/item-master';
import type {
DynamicFormStructure,
UseFormStructureResult,
SimpleUnitOption,
} from '../types';
import { convertToFormStructure } from '../types';
export function useFormStructure(
itemType: 'FG' | 'PT' | 'SM' | 'RM' | 'CS'
): UseFormStructureResult {
const [structure, setStructure] = useState<DynamicFormStructure | null>(null);
const [unitOptions, setUnitOptions] = useState<SimpleUnitOption[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const loadStructure = useCallback(async () => {
setIsLoading(true);
setError(null);
try {
// 1. init API에서 전체 데이터 로드
const initData = await itemMasterApi.init();
// 단위 옵션 저장 (SimpleUnitOption 형식으로 변환)
const simpleUnitOptions: SimpleUnitOption[] = (initData.unitOptions || []).map((u) => ({
label: u.unit_name,
value: u.unit_code,
}));
setUnitOptions(simpleUnitOptions);
// 2. 품목 유형에 해당하는 페이지 찾기
const page = initData.pages.find((p) => p.item_type === itemType);
if (!page) {
// 페이지가 없으면 빈 구조 반환 (품목기준관리에서 아직 설정 안된 경우)
setStructure({
page: {
id: 0,
tenant_id: 0,
page_name: `${itemType} 기본 페이지`,
item_type: itemType,
description: null,
absolute_path: null,
is_active: true,
order_no: 0,
created_by: null,
updated_by: null,
created_at: '',
updated_at: '',
},
sections: [],
directFields: [],
});
return;
}
// 3. 페이지 전체 구조 로드
const pageStructure = await itemMasterApi.pages.getStructure(page.id);
const formStructure = convertToFormStructure(pageStructure);
setStructure(formStructure);
} catch (err) {
console.error('품목 폼 구조 로드 실패:', err);
setError(err instanceof Error ? err.message : '폼 구조를 불러올 수 없습니다.');
} finally {
setIsLoading(false);
}
}, [itemType]);
useEffect(() => {
loadStructure();
}, [loadStructure]);
return {
structure,
isLoading,
error,
unitOptions,
refetch: loadStructure,
};
}