change structure config app

This commit is contained in:
AmirHossein Mahmoodi
2024-01-23 11:42:46 +03:30
parent 0c2448e70d
commit 84bbe3e637
5 changed files with 28 additions and 12 deletions

View File

@@ -2,8 +2,11 @@ import {useEffect, useState} from "react";
import axios from "axios";
import {GET_CONFIG} from "@/core/data/apiRoutes";
import {ConfigProvider} from "@witel/webapp-builder";
import {useRouter} from "next/router";
import {isValidJson} from "@/core/utils/isValidJson";
const ConfigApp = ({children}) => {
const router = useRouter()
const [error, setError] = useState(false)
const [config, setConfig] = useState()
@@ -11,7 +14,12 @@ 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
})
console.log(_config)
setConfig(_config)
} catch (error) {
setError(true)
}
@@ -19,7 +27,7 @@ const ConfigApp = ({children}) => {
fetchConfig()
}, []);
}, [router]);
return (
<ConfigProvider config={config || {}}>{error ? <>Error request config</> : config && children}</ConfigProvider>)

View File

@@ -1,6 +1,6 @@
const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL + "/api";
export const GET_CONFIG = process.env.NEXT_PUBLIC_CONFIG_APP_URL;
export const GET_CONFIG = process.env.NEXT_PUBLIC_BASE_URL + "/dashboard/settings/list";
// export const LOGIN_SEND_OTP_TOKEN = BASE_URL + "/login/send_otp";
export const LOGIN = BASE_URL + "/login";

View File

@@ -0,0 +1,9 @@
export const isValidJson = (value) => {
try {
JSON.parse(value)
return true
} catch (e) {
return false
}
}