diff --git a/src/core/components/Config.jsx b/src/core/components/Config.jsx index 7c0c9ef..adbbefe 100644 --- a/src/core/components/Config.jsx +++ b/src/core/components/Config.jsx @@ -2,8 +2,11 @@ import {useEffect, useState} from "react"; import axios from "axios"; import {GET_CONFIG} from "@/core/data/apiRoutes"; import {ConfigProvider} from "@/lib/app/contexts/config"; +import {isValidJson} from "@/core/utils/isValidJson"; +import {useRouter} from "next/router"; const ConfigApp = ({children}) => { + const router = useRouter() const [error, setError] = useState(false) const [config, setConfig] = useState() @@ -11,7 +14,11 @@ const ConfigApp = ({children}) => { const fetchConfig = async () => { try { const response = await axios(GET_CONFIG) - setConfig(response.data) + let _config = {} + response.data.data.forEach(item => { + _config[item.name] = isValidJson(item.value) ? JSON.parse(item.value) : item.value + }) + setConfig(_config) } catch (error) { setError(true) } @@ -19,7 +26,7 @@ const ConfigApp = ({children}) => { fetchConfig() - }, []); + }, [router]); return ( {error ? <>Error request config : config && children}) diff --git a/src/core/utils/isValidJson.js b/src/core/utils/isValidJson.js new file mode 100644 index 0000000..1ba1b86 --- /dev/null +++ b/src/core/utils/isValidJson.js @@ -0,0 +1,9 @@ +export const isValidJson = (value) => { + try { + JSON.parse(value) + return true + } catch (e) { + return false + } + +} \ No newline at end of file