From 84bbe3e637b15c2395f44ee5970d5f28734c4424 Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Tue, 23 Jan 2024 11:42:46 +0330 Subject: [PATCH] change structure config app --- public/config.json.example | 5 +++-- src/components/first/Banner.jsx | 12 +++++------- src/core/components/Config.jsx | 12 ++++++++++-- src/core/data/apiRoutes.js | 2 +- src/core/utils/isValidJson.js | 9 +++++++++ 5 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 src/core/utils/isValidJson.js diff --git a/public/config.json.example b/public/config.json.example index ac50d76..7dce39a 100644 --- a/public/config.json.example +++ b/public/config.json.example @@ -14,7 +14,7 @@ "register": { "date": { "from": "1402/10/02", - "to": "1402/10/15" + "to": "1402/11/15" }, "messages": { "before": "ثبت نام هنوز شروع نشده", @@ -47,5 +47,6 @@ "secondary": "فرآیند معرفی ضامن و پرداخت" } } - } + }, + "banner":"سلام" } \ No newline at end of file diff --git a/src/components/first/Banner.jsx b/src/components/first/Banner.jsx index 3a8a26a..0c4ef35 100644 --- a/src/components/first/Banner.jsx +++ b/src/components/first/Banner.jsx @@ -4,13 +4,11 @@ import {useConfig} from "@witel/webapp-builder"; const Banner = () => { const {config} = useConfig(); const bannerHTML = config.banner; - return ( - - -
- - - ); + return ( + {config.banner && config.banner !== "" && ( +
+ )} + ); }; export default Banner; diff --git a/src/core/components/Config.jsx b/src/core/components/Config.jsx index 3c1d833..a137364 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 "@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 ( {error ? <>Error request config : config && children}) diff --git a/src/core/data/apiRoutes.js b/src/core/data/apiRoutes.js index f2e42fc..1e69d2d 100644 --- a/src/core/data/apiRoutes.js +++ b/src/core/data/apiRoutes.js @@ -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"; 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