61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
import { useEffect } from 'react'
|
|
import { useRecoilState, useRecoilValue } from 'recoil'
|
|
import { commonCodeState } from '@/store/commonCodeAtom'
|
|
import { globalLocaleStore } from '@/store/localeAtom'
|
|
import { isObjectNotEmpty } from '@/util/common-utils'
|
|
import { useAxios } from '../useAxios'
|
|
|
|
/**
|
|
* 공통코드 관리를 위한 커스텀 훅
|
|
*
|
|
* @description
|
|
* - 공통코드를 전역 상태로 관리하고 필요한 곳에서 사용할 수 있도록 함
|
|
* - 최초 1회만 API를 호출하여 공통코드를 가져옴
|
|
* - 언어 설정에 따라 한국어/일본어 코드명을 자동으로 변환
|
|
*
|
|
* @returns {Object}
|
|
* - commonCode: 전체 공통코드 객체(recoil state)
|
|
* - findCommonCode: 특정 공통코드 그룹을 조회하는 함수
|
|
*
|
|
* @example
|
|
* const { commonCode, findCommonCode } = useCommonCode();
|
|
* const honorificCodes = findCommonCode(200800);
|
|
*/
|
|
export const useCommonCode = () => {
|
|
const [commonCode, setCommonCode] = useRecoilState(commonCodeState)
|
|
const globalLocale = useRecoilValue(globalLocaleStore)
|
|
const { promiseGet } = useAxios()
|
|
|
|
const findCommonCode = (key) => {
|
|
const resultCodes = commonCode[key]?.map((code) => {
|
|
const result = {
|
|
clHeadCd: code.clHeadCd,
|
|
clCode: code.clCode,
|
|
clCodeNm: globalLocale === 'ko' ? code.clCodeNm : code.clCodeJp,
|
|
clPriority: code.clPriority,
|
|
clRefChr1: code.clRefChr1,
|
|
clRefChr2: code.clRefChr2,
|
|
}
|
|
return result
|
|
})
|
|
return resultCodes
|
|
}
|
|
|
|
useEffect(() => {
|
|
const getCommonCode = async () => {
|
|
await promiseGet({ url: '/api/commcode/qc-comm-code' }).then((res) => {
|
|
setCommonCode(Object.groupBy(res.data, ({ clHeadCd }) => clHeadCd))
|
|
})
|
|
}
|
|
|
|
if (!isObjectNotEmpty(commonCode)) {
|
|
getCommonCode()
|
|
}
|
|
}, [])
|
|
|
|
return {
|
|
commonCode,
|
|
findCommonCode,
|
|
}
|
|
}
|