From b3b30ee6194ed621d30b7a7a18e6830bb975a7d8 Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Sat, 20 Jan 2024 14:40:18 +0330 Subject: [PATCH] add config app --- .gitignore | 4 ++- example.env.local | 2 ++ public/config.json.example | 51 +++++++++++++++++++++++++++++++++ src/core/components/Config.jsx | 28 ++++++++++++++++++ src/core/data/apiRoutes.js | 2 ++ src/lib/app/contexts/config.jsx | 7 +++++ src/lib/app/hooks/useConfig.jsx | 12 ++++++++ src/pages/_app.jsx | 7 ++--- 8 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 public/config.json.example create mode 100644 src/core/components/Config.jsx create mode 100644 src/lib/app/contexts/config.jsx create mode 100644 src/lib/app/hooks/useConfig.jsx diff --git a/.gitignore b/.gitignore index 1e62a6d..2b2856c 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,6 @@ cypress/screenshots .env .idea package-lock.json -ecosystem.config.js \ No newline at end of file +ecosystem.config.js + +public/config.json \ No newline at end of file diff --git a/example.env.local b/example.env.local index 06adaec..0c37acb 100644 --- a/example.env.local +++ b/example.env.local @@ -9,6 +9,8 @@ NEXT_PUBLIC_SECONDARY_MAIN = "#FF4E00" NEXT_PUBLIC_BASE_URL = "https://loan.witel.ir" NEXT_PUBLIC_POWERED_BY_URL = "https://witel.ir" +NEXT_PUBLIC_CONFIG_APP_URL = "http://localhost:3000/config.json" + #["NEXT_PUBLIC_HAS_WIDGET" , "NEXT_PUBLIC_HAS_NOTIFICATION"] NEXT_PUBLIC_HAS_VALUE=["NEXT_PUBLIC_HAS_NOTIFICATION"] diff --git a/public/config.json.example b/public/config.json.example new file mode 100644 index 0000000..ac50d76 --- /dev/null +++ b/public/config.json.example @@ -0,0 +1,51 @@ +{ + "middlewares": { + "/dashboard/navgan/add-request-loan":{ + "status" : false, + "title":"اطلاع رسانی", + "message": "ثبت نام به زودی آغاز خواهد شد." + } + }, + "add_request_loan_birthday_range": { + "min": 1327, + "max": 1380 + }, + "deadlines": { + "register": { + "date": { + "from": "1402/10/02", + "to": "1402/10/15" + }, + "messages": { + "before": "ثبت نام هنوز شروع نشده", + "after": "زمان ثبت نام به اتمام رسید" + }, + "timeline_label": { + "primary": "ثبت نام متقاضی", + "secondary": "ثبت نام متقاضیان واجد شرایط" + } + }, + "validation": { + "date": { + "from": "1402/10/15", + "to": "1402/10/30" + }, + "messages": {}, + "timeline_label": { + "primary": "صحت سنجی متقاضیان", + "secondary": "بررسی و کارشناسی درخواست ها" + } + }, + "done": { + "date": { + "from": "1402/11/01", + "to": "1402/11/20" + }, + "messages": {}, + "timeline_label": { + "primary": "ارجاع به بانک", + "secondary": "فرآیند معرفی ضامن و پرداخت" + } + } + } +} \ No newline at end of file diff --git a/src/core/components/Config.jsx b/src/core/components/Config.jsx new file mode 100644 index 0000000..7c0c9ef --- /dev/null +++ b/src/core/components/Config.jsx @@ -0,0 +1,28 @@ +import {useEffect, useState} from "react"; +import axios from "axios"; +import {GET_CONFIG} from "@/core/data/apiRoutes"; +import {ConfigProvider} from "@/lib/app/contexts/config"; + +const ConfigApp = ({children}) => { + const [error, setError] = useState(false) + const [config, setConfig] = useState() + + useEffect(() => { + const fetchConfig = async () => { + try { + const response = await axios(GET_CONFIG) + setConfig(response.data) + } catch (error) { + setError(true) + } + } + + fetchConfig() + + }, []); + + return ( + {error ? <>Error request config : config && children}) +} + +export default ConfigApp \ No newline at end of file diff --git a/src/core/data/apiRoutes.js b/src/core/data/apiRoutes.js index 0201cfd..39868f6 100644 --- a/src/core/data/apiRoutes.js +++ b/src/core/data/apiRoutes.js @@ -1,5 +1,7 @@ const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL; +export const GET_CONFIG = process.env.NEXT_PUBLIC_CONFIG_APP_URL; + //login export const GET_USER_TOKEN = BASE_URL + "/dashboard/login"; //end login diff --git a/src/lib/app/contexts/config.jsx b/src/lib/app/contexts/config.jsx new file mode 100644 index 0000000..9505b5f --- /dev/null +++ b/src/lib/app/contexts/config.jsx @@ -0,0 +1,7 @@ +import {createContext} from "react"; + +export const ConfigContext = createContext() + +export const ConfigProvider = ({children, config}) => { + return {children} +} \ No newline at end of file diff --git a/src/lib/app/hooks/useConfig.jsx b/src/lib/app/hooks/useConfig.jsx new file mode 100644 index 0000000..42d5a78 --- /dev/null +++ b/src/lib/app/hooks/useConfig.jsx @@ -0,0 +1,12 @@ +import {useContext} from "react"; +import {ConfigContext} from "@/lib/app/contexts/config"; + +export const useConfig = () => { + const context = useContext(ConfigContext); + + if (!context) { + throw new Error("useConfig must be used within a ConfigProvider"); + } + + return {config: context.config}; +}; diff --git a/src/pages/_app.jsx b/src/pages/_app.jsx index 9824687..e572fe6 100644 --- a/src/pages/_app.jsx +++ b/src/pages/_app.jsx @@ -10,11 +10,11 @@ import TitlePage from "@/core/components/TitlePage"; import Layout from "@/layouts"; import {ToastProvider} from "@/lib/app/contexts/toast"; import {PrintProvider} from "@/lib/app/contexts/print"; +import ConfigApp from "@/core/components/Config"; const App = ({Component, pageProps}) => { - return ( - <> + return ( @@ -35,8 +35,7 @@ const App = ({Component, pageProps}) => { - - ); + ); }; export default App;