feat: Add getCommonCode hook

This commit is contained in:
yoosangwook 2024-10-21 16:23:15 +09:00
parent e3ad0fc9cb
commit f8bdce6177
3 changed files with 45 additions and 11 deletions

View File

@ -1,24 +1,19 @@
'use client'
import { ErrorBoundary } from 'next/dist/client/components/error-boundary'
import { useCommonCode } from '@/hooks/common/useCommonCode'
import ServerError from './error'
import '@/styles/common.scss'
// import KO from '@/locales/ko.json'
// import JA from '@/locales/ja.json'
export const QcastProvider = ({ children }) => {
// const globalLocale = useRecoilValue(globalLocaleStore)
// const [appMessageState, setAppMessageState] = useRecoilState(appMessageStore)
const { commonCode, findCommonCode } = useCommonCode()
// useEffect(() => {
// if (globalLocale === 'ko') {
// setAppMessageState(KO)
// } else {
// setAppMessageState(JA)
// }
// }, [globalLocale])
// console.log('commonCode', commonCode)
// console.log(findCommonCode(100200))
// console.log(findCommonCode(115800))
// }, [commonCode])
return (
<>

View File

@ -0,0 +1,33 @@
import { useEffect } from 'react'
import { useRecoilState } from 'recoil'
import { commonCodeState } from '@/store/commonCodeAtom'
import { isObjectNotEmpty } from '@/util/common-utils'
import { useAxios } from '../useAxios'
export const useCommonCode = () => {
const [commonCode, setCommonCode] = useRecoilState(commonCodeState)
const { get, promiseGet } = useAxios()
const findCommonCode = (key) => {
// const arr = commonCode[key]
// return arr.sort((a, b) => a.clPriority - b.clPriority)
return commonCode[key]
}
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,
}
}

View File

@ -0,0 +1,6 @@
import { atom } from 'recoil'
export const commonCodeState = atom({
key: 'commonCode',
default: {},
})