fix(dashboard): cannot edit and save policy settings

This commit is contained in:
HFO4 2022-12-18 12:45:51 +08:00
parent f0a1835ce1
commit 807c617c4f
11 changed files with 96 additions and 226 deletions

View File

@ -14,6 +14,7 @@ import QiniuGuide from "./Guid/QiniuGuide";
import RemoteGuide from "./Guid/RemoteGuide";
import UpyunGuide from "./Guid/UpyunGuide";
import S3Guide from "./Guid/S3Guide";
import { transformResponse } from "./utils";
const useStyles = makeStyles((theme) => ({
root: {
@ -45,43 +46,7 @@ export default function EditPolicyPreload() {
setType("");
API.get("/admin/policy/" + id)
.then((response) => {
response.data.IsOriginLinkEnable = response.data
.IsOriginLinkEnable
? "true"
: "false";
response.data.AutoRename = response.data.AutoRename
? "true"
: "false";
response.data.MaxSize = response.data.MaxSize.toString();
response.data.OptionsSerialized.chunk_size = response.data
.OptionsSerialized.chunk_size
? response.data.OptionsSerialized.chunk_size.toString()
: 0;
response.data.IsPrivate = response.data.IsPrivate
? "true"
: "false";
response.data.OptionsSerialized.placeholder_with_size = response
.data.OptionsSerialized.placeholder_with_size
? "true"
: "false";
response.data.OptionsSerialized.s3_path_style = response.data
.OptionsSerialized.s3_path_style
? "true"
: "false";
response.data.OptionsSerialized.file_type = response.data
.OptionsSerialized.file_type
? response.data.OptionsSerialized.file_type.join(",")
: "";
response.data.OptionsSerialized.tps_limit = response.data
.OptionsSerialized.tps_limit
? response.data.OptionsSerialized.tps_limit.toString()
: 0;
response.data.OptionsSerialized.tps_limit_burst = response.data
.OptionsSerialized.tps_limit_burst
? response.data.OptionsSerialized.tps_limit_burst.toString()
: 0;
response = transformResponse(response);
setPolicy(response.data);
setType(response.data.Type);
})

View File

@ -24,6 +24,7 @@ import DomainInput from "../../Common/DomainInput";
import SizeInput from "../../Common/SizeInput";
import MagicVar from "../../Dialogs/MagicVar";
import { Trans, useTranslation } from "react-i18next";
import { transformPolicyRequest } from "../utils";
const useStyles = makeStyles((theme) => ({
stepContent: {
@ -181,7 +182,7 @@ export default function COSGuide(props) {
e.preventDefault();
setLoading(true);
const policyCopy = { ...policy };
let policyCopy = { ...policy };
policyCopy.OptionsSerialized = { ...policyCopy.OptionsSerialized };
if (useCDN === "false") {
@ -189,24 +190,7 @@ export default function COSGuide(props) {
}
// 类型转换
policyCopy.AutoRename = policyCopy.AutoRename === "true";
policyCopy.IsOriginLinkEnable =
policyCopy.IsOriginLinkEnable === "true";
policyCopy.IsPrivate = policyCopy.IsPrivate === "true";
policyCopy.MaxSize = parseInt(policyCopy.MaxSize);
policyCopy.OptionsSerialized.chunk_size = parseInt(
policyCopy.OptionsSerialized.chunk_size
);
policyCopy.OptionsSerialized.placeholder_with_size =
policyCopy.OptionsSerialized.placeholder_with_size === "true";
policyCopy.OptionsSerialized.file_type =
policyCopy.OptionsSerialized.file_type.split(",");
if (
policyCopy.OptionsSerialized.file_type.length === 1 &&
policyCopy.OptionsSerialized.file_type[0] === ""
) {
policyCopy.OptionsSerialized.file_type = [];
}
policyCopy = transformPolicyRequest(policyCopy);
API.post("/admin/policy", {
policy: policyCopy,

View File

@ -16,6 +16,7 @@ import { useDispatch } from "react-redux";
import { toggleSnackbar } from "../../../../redux/explorer";
import API from "../../../../middleware/Api";
import { useTranslation } from "react-i18next";
import { transformPolicyRequest } from "../utils";
export default function EditPro(props) {
const { t } = useTranslation("dashboard", { keyPrefix: "policy" });
@ -50,36 +51,11 @@ export default function EditPro(props) {
e.preventDefault();
setLoading(true);
const policyCopy = { ...policy };
let policyCopy = { ...policy };
policyCopy.OptionsSerialized = { ...policyCopy.OptionsSerialized };
// 类型转换
policyCopy.AutoRename = policyCopy.AutoRename === "true";
policyCopy.IsPrivate = policyCopy.IsPrivate === "true";
policyCopy.IsOriginLinkEnable =
policyCopy.IsOriginLinkEnable === "true";
policyCopy.MaxSize = parseInt(policyCopy.MaxSize);
policyCopy.OptionsSerialized.chunk_size = parseInt(
policyCopy.OptionsSerialized.chunk_size
);
policyCopy.OptionsSerialized.tps_limit = parseFloat(
policyCopy.OptionsSerialized.tps_limit
);
policyCopy.OptionsSerialized.tps_limit_burst = parseInt(
policyCopy.OptionsSerialized.tps_limit_burst
);
policyCopy.OptionsSerialized.placeholder_with_size =
policyCopy.OptionsSerialized.placeholder_with_size === "true";
policyCopy.OptionsSerialized.s3_path_style =
policyCopy.OptionsSerialized.s3_path_style === "true";
policyCopy.OptionsSerialized.file_type =
policyCopy.OptionsSerialized.file_type.split(",");
if (
policyCopy.OptionsSerialized.file_type.length === 1 &&
policyCopy.OptionsSerialized.file_type[0] === ""
) {
policyCopy.OptionsSerialized.file_type = [];
}
policyCopy = transformPolicyRequest(policyCopy);
API.post("/admin/policy", {
policy: policyCopy,

View File

@ -22,6 +22,7 @@ import SizeInput from "../../Common/SizeInput";
import MagicVar from "../../Dialogs/MagicVar";
import { getNumber } from "../../../../utils";
import { Trans, useTranslation } from "react-i18next";
import { transformPolicyRequest } from "../utils";
const useStyles = makeStyles((theme) => ({
stepContent: {
@ -167,7 +168,7 @@ export default function LocalGuide(props) {
e.preventDefault();
setLoading(true);
const policyCopy = { ...policy };
let policyCopy = { ...policy };
policyCopy.OptionsSerialized = { ...policyCopy.OptionsSerialized };
// 处理存储策略
@ -176,25 +177,7 @@ export default function LocalGuide(props) {
}
// 类型转换
policyCopy.AutoRename = policyCopy.AutoRename === "true";
policyCopy.IsOriginLinkEnable =
policyCopy.IsOriginLinkEnable === "true";
policyCopy.MaxSize = parseInt(policyCopy.MaxSize);
policyCopy.OptionsSerialized.chunk_size = parseInt(
policyCopy.OptionsSerialized.chunk_size
);
policyCopy.OptionsSerialized.placeholder_with_size =
policyCopy.OptionsSerialized.placeholder_with_size === "true";
policyCopy.IsPrivate = policyCopy.IsPrivate === "true";
policyCopy.OptionsSerialized.file_type = policyCopy.OptionsSerialized.file_type.split(
","
);
if (
policyCopy.OptionsSerialized.file_type.length === 1 &&
policyCopy.OptionsSerialized.file_type[0] === ""
) {
policyCopy.OptionsSerialized.file_type = [];
}
policyCopy = transformPolicyRequest(policyCopy);
API.post("/admin/policy", {
policy: policyCopy,

View File

@ -22,6 +22,7 @@ import DomainInput from "../../Common/DomainInput";
import SizeInput from "../../Common/SizeInput";
import MagicVar from "../../Dialogs/MagicVar";
import { Trans, useTranslation } from "react-i18next";
import { transformPolicyRequest } from "../utils";
const useStyles = makeStyles((theme) => ({
stepContent: {
@ -181,7 +182,7 @@ export default function OSSGuide(props) {
e.preventDefault();
setLoading(true);
const policyCopy = { ...policy };
let policyCopy = { ...policy };
policyCopy.OptionsSerialized = { ...policyCopy.OptionsSerialized };
if (useCDN === "false") {
@ -193,24 +194,7 @@ export default function OSSGuide(props) {
}
// 类型转换
policyCopy.AutoRename = policyCopy.AutoRename === "true";
policyCopy.IsOriginLinkEnable =
policyCopy.IsOriginLinkEnable === "true";
policyCopy.IsPrivate = policyCopy.IsPrivate === "true";
policyCopy.MaxSize = parseInt(policyCopy.MaxSize);
policyCopy.OptionsSerialized.chunk_size = parseInt(
policyCopy.OptionsSerialized.chunk_size
);
policyCopy.OptionsSerialized.placeholder_with_size =
policyCopy.OptionsSerialized.placeholder_with_size === "true";
policyCopy.OptionsSerialized.file_type =
policyCopy.OptionsSerialized.file_type.split(",");
if (
policyCopy.OptionsSerialized.file_type.length === 1 &&
policyCopy.OptionsSerialized.file_type[0] === ""
) {
policyCopy.OptionsSerialized.file_type = [];
}
policyCopy = transformPolicyRequest(policyCopy);
API.post("/admin/policy", {
policy: policyCopy,

View File

@ -24,6 +24,7 @@ import DomainInput from "../../Common/DomainInput";
import { getNumber } from "../../../../utils";
import FormHelperText from "@material-ui/core/FormHelperText";
import { Trans, useTranslation } from "react-i18next";
import { transformPolicyRequest } from "../utils";
const useStyles = makeStyles((theme) => ({
stepContent: {
@ -231,7 +232,7 @@ export default function OneDriveGuide(props) {
e.preventDefault();
setLoading(true);
const policyCopy = { ...policy };
let policyCopy = { ...policy };
policyCopy.OptionsSerialized = { ...policyCopy.OptionsSerialized };
// baseURL处理
@ -251,30 +252,7 @@ export default function OneDriveGuide(props) {
}
// 类型转换
policyCopy.AutoRename = policyCopy.AutoRename === "true";
policyCopy.IsOriginLinkEnable =
policyCopy.IsOriginLinkEnable === "true";
policyCopy.IsPrivate = policyCopy.IsPrivate === "true";
policyCopy.MaxSize = parseInt(policyCopy.MaxSize);
policyCopy.OptionsSerialized.chunk_size = parseInt(
policyCopy.OptionsSerialized.chunk_size
);
policyCopy.OptionsSerialized.tps_limit = parseFloat(
policyCopy.OptionsSerialized.tps_limit
);
policyCopy.OptionsSerialized.tps_limit_burst = parseInt(
policyCopy.OptionsSerialized.tps_limit_burst
);
policyCopy.OptionsSerialized.placeholder_with_size =
policyCopy.OptionsSerialized.placeholder_with_size === "true";
policyCopy.OptionsSerialized.file_type =
policyCopy.OptionsSerialized.file_type.split(",");
if (
policyCopy.OptionsSerialized.file_type.length === 1 &&
policyCopy.OptionsSerialized.file_type[0] === ""
) {
policyCopy.OptionsSerialized.file_type = [];
}
policyCopy = transformPolicyRequest(policyCopy);
API.post("/admin/policy", {
policy: policyCopy,

View File

@ -22,6 +22,7 @@ import DomainInput from "../../Common/DomainInput";
import SizeInput from "../../Common/SizeInput";
import MagicVar from "../../Dialogs/MagicVar";
import { Trans, useTranslation } from "react-i18next";
import { transformPolicyRequest } from "../utils";
const useStyles = makeStyles((theme) => ({
@ -157,28 +158,11 @@ export default function RemoteGuide(props) {
e.preventDefault();
setLoading(true);
const policyCopy = { ...policy };
let policyCopy = { ...policy };
policyCopy.OptionsSerialized = { ...policyCopy.OptionsSerialized };
// 类型转换
policyCopy.AutoRename = policyCopy.AutoRename === "true";
policyCopy.IsOriginLinkEnable =
policyCopy.IsOriginLinkEnable === "true";
policyCopy.IsPrivate = policyCopy.IsPrivate === "true";
policyCopy.MaxSize = parseInt(policyCopy.MaxSize);
policyCopy.OptionsSerialized.chunk_size = parseInt(
policyCopy.OptionsSerialized.chunk_size
);
policyCopy.OptionsSerialized.placeholder_with_size =
policyCopy.OptionsSerialized.placeholder_with_size === "true";
policyCopy.OptionsSerialized.file_type =
policyCopy.OptionsSerialized.file_type.split(",");
if (
policyCopy.OptionsSerialized.file_type.length === 1 &&
policyCopy.OptionsSerialized.file_type[0] === ""
) {
policyCopy.OptionsSerialized.file_type = [];
}
policyCopy = transformPolicyRequest(policyCopy);
API.post("/admin/policy", {
policy: policyCopy,

View File

@ -23,6 +23,7 @@ import DomainInput from "../../Common/DomainInput";
import SizeInput from "../../Common/SizeInput";
import MagicVar from "../../Dialogs/MagicVar";
import { Trans, useTranslation } from "react-i18next";
import { transformPolicyRequest } from "../utils";
const useStyles = makeStyles((theme) => ({
stepContent: {
@ -195,7 +196,7 @@ export default function RemoteGuide(props) {
e.preventDefault();
setLoading(true);
const policyCopy = { ...policy };
let policyCopy = { ...policy };
policyCopy.OptionsSerialized = { ...policyCopy.OptionsSerialized };
// 处理存储策略
@ -204,25 +205,7 @@ export default function RemoteGuide(props) {
}
// 类型转换
policyCopy.AutoRename = policyCopy.AutoRename === "true";
policyCopy.IsOriginLinkEnable =
policyCopy.IsOriginLinkEnable === "true";
policyCopy.MaxSize = parseInt(policyCopy.MaxSize);
policyCopy.OptionsSerialized.chunk_size = parseInt(
policyCopy.OptionsSerialized.chunk_size
);
policyCopy.OptionsSerialized.placeholder_with_size =
policyCopy.OptionsSerialized.placeholder_with_size === "true";
policyCopy.IsPrivate = policyCopy.IsPrivate === "true";
policyCopy.OptionsSerialized.file_type = policyCopy.OptionsSerialized.file_type.split(
","
);
if (
policyCopy.OptionsSerialized.file_type.length === 1 &&
policyCopy.OptionsSerialized.file_type[0] === ""
) {
policyCopy.OptionsSerialized.file_type = [];
}
policyCopy = transformPolicyRequest(policyCopy);
API.post("/admin/policy", {
policy: policyCopy,

View File

@ -25,6 +25,7 @@ import TextField from "@material-ui/core/TextField";
import AlertDialog from "../../Dialogs/Alert";
import { toggleSnackbar } from "../../../../redux/explorer";
import { Trans, useTranslation } from "react-i18next";
import { transformPolicyRequest } from "../utils";
const useStyles = makeStyles((theme) => ({
stepContent: {
@ -216,7 +217,7 @@ export default function S3Guide(props) {
e.preventDefault();
setLoading(true);
const policyCopy = { ...policy };
let policyCopy = { ...policy };
policyCopy.OptionsSerialized = { ...policyCopy.OptionsSerialized };
if (useCDN === "false") {
@ -224,26 +225,7 @@ export default function S3Guide(props) {
}
// 类型转换
policyCopy.AutoRename = policyCopy.AutoRename === "true";
policyCopy.IsOriginLinkEnable =
policyCopy.IsOriginLinkEnable === "true";
policyCopy.IsPrivate = policyCopy.IsPrivate === "true";
policyCopy.MaxSize = parseInt(policyCopy.MaxSize);
policyCopy.OptionsSerialized.chunk_size = parseInt(
policyCopy.OptionsSerialized.chunk_size
);
policyCopy.OptionsSerialized.placeholder_with_size =
policyCopy.OptionsSerialized.placeholder_with_size === "true";
policyCopy.OptionsSerialized.s3_path_style =
policyCopy.OptionsSerialized.s3_path_style === "true";
policyCopy.OptionsSerialized.file_type =
policyCopy.OptionsSerialized.file_type.split(",");
if (
policyCopy.OptionsSerialized.file_type.length === 1 &&
policyCopy.OptionsSerialized.file_type[0] === ""
) {
policyCopy.OptionsSerialized.file_type = [];
}
policyCopy = transformPolicyRequest(policyCopy);
API.post("/admin/policy", {
policy: policyCopy,

View File

@ -21,6 +21,7 @@ import DomainInput from "../../Common/DomainInput";
import SizeInput from "../../Common/SizeInput";
import MagicVar from "../../Dialogs/MagicVar";
import { Trans, useTranslation } from "react-i18next";
import { transformPolicyRequest } from "../utils";
const useStyles = makeStyles((theme) => ({
stepContent: {
@ -152,28 +153,11 @@ export default function UpyunGuide(props) {
e.preventDefault();
setLoading(true);
const policyCopy = { ...policy };
let policyCopy = { ...policy };
policyCopy.OptionsSerialized = { ...policyCopy.OptionsSerialized };
// 类型转换
policyCopy.AutoRename = policyCopy.AutoRename === "true";
policyCopy.IsOriginLinkEnable =
policyCopy.IsOriginLinkEnable === "true";
policyCopy.IsPrivate = policyCopy.IsPrivate === "true";
policyCopy.MaxSize = parseInt(policyCopy.MaxSize);
policyCopy.OptionsSerialized.chunk_size = parseInt(
policyCopy.OptionsSerialized.chunk_size
);
policyCopy.OptionsSerialized.placeholder_with_size =
policyCopy.OptionsSerialized.placeholder_with_size === "true";
policyCopy.OptionsSerialized.file_type =
policyCopy.OptionsSerialized.file_type.split(",");
if (
policyCopy.OptionsSerialized.file_type.length === 1 &&
policyCopy.OptionsSerialized.file_type[0] === ""
) {
policyCopy.OptionsSerialized.file_type = [];
}
policyCopy = transformPolicyRequest(policyCopy);
API.post("/admin/policy", {
policy: policyCopy,

View File

@ -0,0 +1,67 @@
const boolFields = ["IsOriginLinkEnable", "AutoRename", "IsPrivate"];
const numberFields = ["MaxSize"];
const boolFieldsInOptions = ["placeholder_with_size", "s3_path_style"];
const numberFieldsInOptions = ["chunk_size", "tps_limit", "tps_limit_burst"];
export const transformResponse = (response) => {
boolFields.forEach(
(field) =>
(response.data[field] = response.data[field] ? "true" : "false")
);
numberFields.forEach(
(field) => (response.data[field] = response.data[field].toString())
);
boolFieldsInOptions.forEach(
(field) =>
(response.data.OptionsSerialized[field] = response.data
.OptionsSerialized[field]
? "true"
: "false")
);
numberFieldsInOptions.forEach(
(field) =>
(response.data.OptionsSerialized[field] = response.data
.OptionsSerialized[field]
? response.data.OptionsSerialized[field].toString()
: 0)
);
response.data.OptionsSerialized.file_type = response.data.OptionsSerialized
.file_type
? response.data.OptionsSerialized.file_type.join(",")
: "";
return response;
};
export const transformPolicyRequest = (policyCopy) => {
boolFields.forEach(
(field) => (policyCopy[field] = policyCopy[field] === "true")
);
numberFields.forEach(
(field) => (policyCopy[field] = parseInt(policyCopy[field]))
);
boolFieldsInOptions.forEach(
(field) =>
(policyCopy.OptionsSerialized[field] =
policyCopy.OptionsSerialized[field] === "true")
);
numberFieldsInOptions.forEach(
(field) =>
(policyCopy.OptionsSerialized[field] = parseInt(
policyCopy.OptionsSerialized[field]
))
);
policyCopy.OptionsSerialized.file_type =
policyCopy.OptionsSerialized.file_type.split(",");
if (
policyCopy.OptionsSerialized.file_type.length === 1 &&
policyCopy.OptionsSerialized.file_type[0] === ""
) {
policyCopy.OptionsSerialized.file_type = [];
}
return policyCopy;
};