From 4605938f8e45eb148f0ead64d5d4b88d98b220ec Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Thu, 16 Feb 2023 22:50:08 +0800 Subject: [PATCH] Refactor FetchFilter.js code --- .../{FetchInterceptor.js => FetchFilter.js} | 32 +++++++------------ web/src/index.js | 2 +- web/src/locales/de/data.json | 2 +- web/src/locales/en/data.json | 2 +- web/src/locales/es/data.json | 2 +- web/src/locales/fr/data.json | 2 +- web/src/locales/ja/data.json | 2 +- web/src/locales/ko/data.json | 2 +- web/src/locales/ru/data.json | 2 +- web/src/locales/zh/data.json | 2 +- 10 files changed, 21 insertions(+), 29 deletions(-) rename web/src/backend/{FetchInterceptor.js => FetchFilter.js} (60%) diff --git a/web/src/backend/FetchInterceptor.js b/web/src/backend/FetchFilter.js similarity index 60% rename from web/src/backend/FetchInterceptor.js rename to web/src/backend/FetchFilter.js index a6509e83..cac26ea8 100644 --- a/web/src/backend/FetchInterceptor.js +++ b/web/src/backend/FetchFilter.js @@ -1,4 +1,4 @@ -// Copyright 2022 The Casdoor Authors. All Rights Reserved. +// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,52 +12,44 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {ExclamationCircleFilled} from "@ant-design/icons"; import {Modal} from "antd"; +import {ExclamationCircleFilled} from "@ant-design/icons"; import i18next from "i18next"; import * as Conf from "../Conf"; +import * as Setting from "../Setting"; const {confirm} = Modal; const {fetch: originalFetch} = window; -/** - * When modify data, prompt it's read-only and ask whether to go writable site - */ -const demoModePrompt = async(url, option) => { +const demoModeCallback = async(url, option) => { if (option.method === "POST") { confirm({ title: i18next.t("general:This is a read-only demo site!"), icon: , - content: i18next.t("general:Go Writable demo site?"), + content: i18next.t("general:Go to writable demo site?"), okText: i18next.t("user:OK"), cancelText: i18next.t("general:Cancel"), onOk() { - const fullURL = document.location.toString(); - window.open("https://demo.casdoor.com" + fullURL.substring(fullURL.lastIndexOf("/")) + "?username=built-in/admin&password=123", "_blank"); + Setting.openLink(`https://demo.casdoor.com${location.path}${location.search}?username=built-in/admin&password=123`); }, onCancel() {}, }); } + return option; }; -const requsetInterceptors = []; -const responseInterceptors = []; +const requestFilters = []; +const responseFilters = []; -// when it's in DemoMode, demoModePrompt() should run before fetch if (Conf.IsDemoMode) { - requsetInterceptors.push(demoModePrompt); + requestFilters.push(demoModeCallback); } -/** - * rewrite fetch to support interceptors - */ window.fetch = async(url, option = {}) => { - for (const fn of requsetInterceptors) { - fn(url, option); - } + requestFilters.forEach(filter => filter(url, option)); const response = await originalFetch(url, option); - responseInterceptors.forEach(fn => (response) => fn(response)); + responseFilters.forEach(filter => (response) => filter(response)); return response; }; diff --git a/web/src/index.js b/web/src/index.js index 2f88f3ba..4a08381e 100644 --- a/web/src/index.js +++ b/web/src/index.js @@ -22,7 +22,7 @@ import "./App.less"; import App from "./App"; import * as serviceWorker from "./serviceWorker"; import {BrowserRouter} from "react-router-dom"; -import "./backend/FetchInterceptor"; +import "./backend/FetchFilter"; const container = document.getElementById("root"); diff --git a/web/src/locales/de/data.json b/web/src/locales/de/data.json index ea3a4ccc..ac35671c 100644 --- a/web/src/locales/de/data.json +++ b/web/src/locales/de/data.json @@ -181,7 +181,7 @@ "First name": "First name", "Forget URL": "URL vergessen", "Forget URL - Tooltip": "Unique string-style identifier", - "Go Writable demo site?": "Go Writable demo site?", + "Go to writable demo site?": "Go to writable demo site?", "Home": "Zuhause", "Home - Tooltip": "Application homepage", "ID": "ID", diff --git a/web/src/locales/en/data.json b/web/src/locales/en/data.json index 03bc9381..feaf267c 100644 --- a/web/src/locales/en/data.json +++ b/web/src/locales/en/data.json @@ -181,7 +181,7 @@ "First name": "First name", "Forget URL": "Forget URL", "Forget URL - Tooltip": "Forget URL - Tooltip", - "Go Writable demo site?": "Go Writable demo site?", + "Go to writable demo site?": "Go to writable demo site?", "Home": "Home", "Home - Tooltip": "Home - Tooltip", "ID": "ID", diff --git a/web/src/locales/es/data.json b/web/src/locales/es/data.json index 61c1f50e..27e198a0 100644 --- a/web/src/locales/es/data.json +++ b/web/src/locales/es/data.json @@ -181,7 +181,7 @@ "First name": "Nombre", "Forget URL": "URL de olvido", "Forget URL - Tooltip": "URL de olvido - Tooltip", - "Go Writable demo site?": "Go Writable demo site?", + "Go to writable demo site?": "Go to writable demo site?", "Home": "Inicio", "Home - Tooltip": "Inicio - Tooltip", "ID": "ID", diff --git a/web/src/locales/fr/data.json b/web/src/locales/fr/data.json index eeffbbf4..d25d1aa9 100644 --- a/web/src/locales/fr/data.json +++ b/web/src/locales/fr/data.json @@ -181,7 +181,7 @@ "First name": "First name", "Forget URL": "Oublier l'URL", "Forget URL - Tooltip": "Unique string-style identifier", - "Go Writable demo site?": "Go Writable demo site?", + "Go to writable demo site?": "Go to writable demo site?", "Home": "Domicile", "Home - Tooltip": "Application homepage", "ID": "ID", diff --git a/web/src/locales/ja/data.json b/web/src/locales/ja/data.json index 62b1cbe1..4a713bf1 100644 --- a/web/src/locales/ja/data.json +++ b/web/src/locales/ja/data.json @@ -181,7 +181,7 @@ "First name": "First name", "Forget URL": "URLを忘れた", "Forget URL - Tooltip": "Unique string-style identifier", - "Go Writable demo site?": "Go Writable demo site?", + "Go to writable demo site?": "Go to writable demo site?", "Home": "ホーム", "Home - Tooltip": "Application homepage", "ID": "ID", diff --git a/web/src/locales/ko/data.json b/web/src/locales/ko/data.json index aa151874..5c3ab123 100644 --- a/web/src/locales/ko/data.json +++ b/web/src/locales/ko/data.json @@ -181,7 +181,7 @@ "First name": "First name", "Forget URL": "Forget URL", "Forget URL - Tooltip": "Unique string-style identifier", - "Go Writable demo site?": "Go Writable demo site?", + "Go to writable demo site?": "Go to writable demo site?", "Home": "Home", "Home - Tooltip": "Application homepage", "ID": "ID", diff --git a/web/src/locales/ru/data.json b/web/src/locales/ru/data.json index 0a084a47..a74d02a3 100644 --- a/web/src/locales/ru/data.json +++ b/web/src/locales/ru/data.json @@ -181,7 +181,7 @@ "First name": "Имя", "Forget URL": "Забыть URL", "Forget URL - Tooltip": "Unique string-style identifier", - "Go Writable demo site?": "Go Writable demo site?", + "Go to writable demo site?": "Go to writable demo site?", "Home": "Домашний", "Home - Tooltip": "Application homepage", "ID": "ID", diff --git a/web/src/locales/zh/data.json b/web/src/locales/zh/data.json index e60cad39..0cb16fe3 100644 --- a/web/src/locales/zh/data.json +++ b/web/src/locales/zh/data.json @@ -181,7 +181,7 @@ "First name": "名字", "Forget URL": "忘记密码URL", "Forget URL - Tooltip": "忘记密码URL", - "Go Writable demo site?": "跳转至可写演示站点?", + "Go to writable demo site?": "跳转至可写演示站点?", "Home": "首页", "Home - Tooltip": "应用的首页", "ID": "ID",