49 lines
1.3 KiB
JavaScript
49 lines
1.3 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'
|
|
|
|
export const useCommonCode = () => {
|
|
const [commonCode, setCommonCode] = useRecoilState(commonCodeState)
|
|
const globalLocale = useRecoilValue(globalLocaleStore)
|
|
const { promiseGet } = useAxios()
|
|
|
|
const findCommonCode = (key) => {
|
|
// const arr = commonCode[key]
|
|
// return arr.sort((a, b) => a.clPriority - b.clPriority)
|
|
const resultCodes = commonCode[key]?.map((code) => {
|
|
const result = {
|
|
clHeadCd: code.clHeadCd,
|
|
clCode: code.clCode,
|
|
clCodeNm: globalLocale === 'ko' ? code.clCodeNm : code.clCodeJp,
|
|
clPriority: code.clPriority,
|
|
}
|
|
return result
|
|
})
|
|
return resultCodes
|
|
}
|
|
|
|
useEffect(() => {
|
|
findCommonCode()
|
|
}, [globalLocale])
|
|
|
|
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,
|
|
}
|
|
}
|